Debian Codegraph @

src/uk/me/parabola/io/StructuredOutputStream.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 23-Sep-2007
16 */
17 package uk.me.parabola.io;
18
19 import java.io.FilterOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStream;
22
23 import uk.me.parabola.imgfmt.ExitException;
24 import uk.me.parabola.imgfmt.app.labelenc.CharacterEncoder;
25 import uk.me.parabola.imgfmt.app.labelenc.EncodedText;
26
27 import static uk.me.parabola.imgfmt.app.labelenc.BaseEncoder.NO_TEXT;
28
29 /**
30 * An output stream that has methods for writing strings and little endian
31 * integers. Its a bit like DataOutput, but for little endian.
32 *
33 * @author Steve Ratcliffe
34 */
35 public class StructuredOutputStream extends FilterOutputStream {
36
37 private final CharacterEncoder encoder;
38
39 public StructuredOutputStream(OutputStream out, CharacterEncoder encoder) {
40 super(out);
41 this.encoder = encoder;
42 }
43
44 public void write(int b) throws IOException {
45 out.write(b);
46 }
47
48 public void write2(int b) throws IOException {
49 out.write(b);
50 out.write(b >> 8);
51 }
52
53 public void write3(int i) throws IOException {
54 out.write(i);
55 out.write(i >> 8);
56 out.write(i >> 16);
57 }
58
59 public void write4(int b) throws IOException {
60 out.write(b);
61 out.write(b >> 8);
62 out.write(b >> 16);
63 out.write(b >> 24);
64 }
65
66 /**
67 * Writes a string including a terminating null byte.
68 *
69 * For each character in the string the low-order byte is written.
70 *
71 * @param s The string to write.
72 * @throws IOException If the write fails.
73 */
74 public void writeString(String s) throws IOException {
75 if (encoder == null)
76 throw new ExitException("tdbfile: character encoding is null");
77
78 EncodedText encodedText = encoder.encodeText(s);
79 if (encodedText == NO_TEXT)
80 return;
81
82 out.write(encodedText.getCtext(), 0, encodedText.getLength());
83 }
84 }