| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.imgfmt.app.mdr; |
| 14 |
|
| 15 |
import java.io.BufferedOutputStream; |
| 16 |
import java.io.File; |
| 17 |
import java.io.FileInputStream; |
| 18 |
import java.io.FileOutputStream; |
| 19 |
import java.io.IOException; |
| 20 |
import java.io.OutputStream; |
| 21 |
import java.nio.ByteBuffer; |
| 22 |
import java.nio.channels.FileChannel; |
| 23 |
import java.nio.charset.Charset; |
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.Map; |
| 26 |
|
| 27 |
import uk.me.parabola.imgfmt.ExitException; |
| 28 |
import uk.me.parabola.imgfmt.MapFailedException; |
| 29 |
import uk.me.parabola.imgfmt.Utils; |
| 30 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class Mdr15 extends MdrSection { |
| 41 |
private final OutputStream stringFile; |
| 42 |
private int nextOffset; |
| 43 |
|
| 44 |
private Map<String, Integer> strings = new HashMap<>(); |
| 45 |
private final Charset charset; |
| 46 |
private final File tempFile; |
| 47 |
|
| 48 |
public Mdr15(MdrConfig config) { |
| 49 |
setConfig(config); |
| 50 |
|
| 51 |
charset = config.getSort().getCharset(); |
| 52 |
if (config.isForDevice()) { |
| 53 |
tempFile = null; |
| 54 |
stringFile = null; |
| 55 |
return; |
| 56 |
} |
| 57 |
try { |
| 58 |
tempFile = File.createTempFile("strings", null, config.getOutputDir()); |
| 59 |
tempFile.deleteOnExit(); |
| 60 |
|
| 61 |
stringFile = new BufferedOutputStream(new FileOutputStream(tempFile), 64 * 1024); |
| 62 |
|
| 63 |
|
| 64 |
stringFile.write(0); |
| 65 |
nextOffset = 1; |
| 66 |
|
| 67 |
} catch (IOException e) { |
| 68 |
throw new ExitException("Could not create temporary file"); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public int createString(String str) { |
| 73 |
Integer offset = strings.get(str); |
| 74 |
if (offset != null) |
| 75 |
return offset; |
| 76 |
|
| 77 |
int off; |
| 78 |
try { |
| 79 |
off = nextOffset; |
| 80 |
|
| 81 |
byte[] bytes = str.getBytes(charset); |
| 82 |
stringFile.write(bytes); |
| 83 |
stringFile.write(0); |
| 84 |
|
| 85 |
|
| 86 |
nextOffset += bytes.length + 1; |
| 87 |
} catch (IOException e) { |
| 88 |
|
| 89 |
off = 0; |
| 90 |
} |
| 91 |
|
| 92 |
strings.put(str, off); |
| 93 |
return off; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@Override |
| 102 |
public void releaseMemory() { |
| 103 |
strings = null; |
| 104 |
try { |
| 105 |
if (stringFile != null) { |
| 106 |
stringFile.close(); |
| 107 |
} |
| 108 |
} catch (IOException e) { |
| 109 |
throw new MapFailedException("Could not close string temporary file"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public void writeSectData(ImgFileWriter writer) { |
| 114 |
try (FileInputStream stream = new FileInputStream(tempFile)) { |
| 115 |
FileChannel channel = stream.getChannel(); |
| 116 |
ByteBuffer buf = ByteBuffer.allocate(32 * 1024); |
| 117 |
while (channel.read(buf) > 0) { |
| 118 |
buf.flip(); |
| 119 |
writer.put(buf); |
| 120 |
buf.compact(); |
| 121 |
} |
| 122 |
} catch (IOException e) { |
| 123 |
throw new ExitException("Could not write string section of index"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public int getItemSize() { |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@Override |
| 138 |
public int getSizeForRecord() { |
| 139 |
return Utils.numberToPointerSize(nextOffset); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected int numberOfItems() { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
} |