| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.io; |
| 14 |
|
| 15 |
import java.io.ByteArrayOutputStream; |
| 16 |
import java.io.IOException; |
| 17 |
import java.io.OutputStream; |
| 18 |
|
| 19 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 20 |
import uk.me.parabola.imgfmt.app.labelenc.CharacterEncoder; |
| 21 |
import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public abstract class FileBlock { |
| 30 |
private final byte blockId; |
| 31 |
|
| 32 |
private final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 33 |
|
| 34 |
public FileBlock(int blockId) { |
| 35 |
this.blockId = (byte) blockId; |
| 36 |
|
| 37 |
fillHeader(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected abstract void writeBody(StructuredOutputStream output) throws IOException; |
| 44 |
|
| 45 |
private StructuredOutputStream getStructuredOutput(int codePage) { |
| 46 |
CharacterEncoder enc = CodeFunctions.createEncoderForLBL(0, codePage).getEncoder(); |
| 47 |
return new StructuredOutputStream(output, enc); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public void writeTo(ImgFileWriter w, int codePage) throws IOException { |
| 56 |
byte[] b = writeToCommon(codePage); |
| 57 |
w.put(b); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public void writeTo(OutputStream os, int codePage) throws IOException { |
| 66 |
byte[] b = writeToCommon(codePage); |
| 67 |
os.write(b); |
| 68 |
} |
| 69 |
|
| 70 |
private byte[] writeToCommon(int codePage) throws IOException { |
| 71 |
writeBody(getStructuredOutput(codePage)); |
| 72 |
|
| 73 |
byte[] b = this.output.toByteArray(); |
| 74 |
|
| 75 |
|
| 76 |
int blockLength = b.length - 3; |
| 77 |
b[1] = (byte) (blockLength & 0xff); |
| 78 |
b[2] = (byte) ((blockLength >> 8) & 0xff); |
| 79 |
return b; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private void fillHeader() { |
| 86 |
try { |
| 87 |
byte[] b = new byte[3]; |
| 88 |
b[0] = this.blockId; |
| 89 |
|
| 90 |
output.write(b); |
| 91 |
} catch (IOException ignore) { |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
} |