Debian Codegraph @

src/main/java/org/freehep/util/io/ByteOrderOutputStream.java

raw
1 // Copyright 2001-2005, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.DataOutput;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.io.UTFDataFormatException;
8
9 /**
10 * Class to write bytes and pairs of bytes in both little and big endian order.
11 *
12 * @author Mark Donszelmann
13 * @author Charles Loomis
14 * @version $Id: ByteOrderOutputStream.java 8584 2006-08-10 23:06:37Z duns $
15 */
16 public class ByteOrderOutputStream extends BitOutputStream implements
17 DataOutput {
18
19 protected boolean little;
20
21 protected int written;
22
23 /**
24 * Create a (Big Endian) Byte Order output stream from given stream
25 *
26 * @param out stream to write to
27 */
28 public ByteOrderOutputStream(OutputStream out) {
29 this(out, false);
30 }
31
32 /**
33 * Create a Byte Order output stream from the given stream
34 *
35 * @param out stream to write to
36 * @param littleEndian true if stream should be little endian
37 */
38 public ByteOrderOutputStream(OutputStream out, boolean littleEndian) {
39 super(out);
40 little = littleEndian;
41 written = 0;
42 }
43
44 /**
45 * @return number of written bytes
46 * @throws IOException if write fails FIXME, should this throw an exception ?
47 */
48 public int size() throws IOException {
49 return written;
50 }
51
52 public synchronized void write(int b) throws IOException {
53 super.write(b);
54 written++;
55 }
56
57 public void writeBoolean(boolean b) throws IOException {
58 if (b) {
59 write(1);
60 } else {
61 write(0);
62 }
63 }
64
65 public void writeChar(int c) throws IOException {
66 if (little) {
67 write(c & 0xFF);
68 write((c >>> 8) & 0xFF);
69 } else {
70 write((c >>> 8) & 0xFF);
71 write(c & 0xFF);
72 }
73 }
74
75 /**
76 * Write a signed byte.
77 */
78 public void writeByte(int b) throws IOException {
79
80 byteAlign();
81 write(b);
82 }
83
84 /**
85 * Writes array of bytes
86 *
87 * @param bytes byte array to be written
88 * @throws IOException if write fails
89 */
90 public void writeByte(byte[] bytes) throws IOException {
91
92 byteAlign();
93 for (int i = 0; i < bytes.length; i++) {
94 write(bytes[i]);
95 }
96 }
97
98 /**
99 * Write an unsigned byte.
100 *
101 * @param ub byte to write
102 * @throws IOException if write fails
103 */
104 public void writeUnsignedByte(int ub) throws IOException {
105
106 byteAlign();
107 write(ub);
108 }
109
110 /**
111 * Write an array of unsigned bytes.
112 *
113 * @param bytes int array to write as bytes
114 * @throws IOException if write fails
115 */
116 public void writeUnsignedByte(int[] bytes) throws IOException {
117
118 byteAlign();
119 for (int i = 0; i < bytes.length; i++) {
120 write(bytes[i]);
121 }
122 }
123
124 /**
125 * Write a signed short.
126 */
127 public void writeShort(int s) throws IOException {
128
129 byteAlign();
130 if (little) {
131 write(s & 0xFF);
132 write((s >>> 8) & 0xFF);
133 } else {
134 write((s >>> 8) & 0xFF);
135 write(s & 0xFF);
136 }
137 }
138
139 /**
140 * Write an array of shorts.
141 *
142 * @param shorts short array to write
143 * @throws IOException if write fails
144 */
145 public void writeShort(short[] shorts) throws IOException {
146
147 for (int i = 0; i < shorts.length; i++) {
148 writeShort(shorts[i]);
149 }
150 }
151
152 /**
153 * Write an unsigned short.
154 *
155 * @param s int to write as unsigned short
156 * @throws IOException if write fails
157 */
158 public void writeUnsignedShort(int s) throws IOException {
159
160 byteAlign();
161 if (little) {
162 write(s & 0xFF);
163 write((s >>> 8) & 0xFF);
164 } else {
165 write((s >>> 8) & 0xFF);
166 write(s & 0xFF);
167 }
168 }
169
170 /**
171 * Write an array of unsigned shorts.
172 *
173 * @param shorts int array to write as unsigned shorts
174 * @throws IOException if write fails
175 */
176 public void writeUnsignedShort(int[] shorts) throws IOException {
177
178 for (int i = 0; i < shorts.length; i++) {
179 writeUnsignedShort(shorts[i]);
180 }
181 }
182
183 /**
184 * Write a signed integer.
185 */
186 public void writeInt(int i) throws IOException {
187
188 if (little) {
189 write(i & 0xFF);
190 write((i >>> 8) & 0xFF);
191 write((i >>> 16) & 0xFF);
192 write((i >>> 24) & 0xFF);
193 } else {
194 write((i >>> 24) & 0xFF);
195 write((i >>> 16) & 0xFF);
196 write((i >>> 8) & 0xFF);
197 write(i & 0xFF);
198 }
199 }
200
201 /**
202 * Write an array of ints
203 *
204 * @param ints int array to write
205 * @throws IOException if write fails
206 */
207 public void writeInt(int[] ints) throws IOException {
208
209 for (int i = 0; i < ints.length; i++) {
210 writeInt(ints[i]);
211 }
212 }
213
214 /**
215 * Write an unsigned integer.
216 *
217 * @param i long to write as unsigned int
218 * @throws IOException if write fails
219 */
220 public void writeUnsignedInt(long i) throws IOException {
221
222 if (little) {
223 write((int) (i & 0xFF));
224 write((int) ((i >>> 8) & 0xFF));
225 write((int) ((i >>> 16) & 0xFF));
226 write((int) ((i >>> 24) & 0xFF));
227 } else {
228 write((int) ((i >>> 24) & 0xFF));
229 write((int) ((i >>> 16) & 0xFF));
230 write((int) ((i >>> 8) & 0xFF));
231 write((int) (i & 0xFF));
232 }
233 }
234
235 /**
236 * Write an array of unsigned ints
237 *
238 * @param ints long array to write as unsigned ints
239 * @throws IOException if write fails
240 */
241 public void writeUnsignedInt(long[] ints) throws IOException {
242
243 for (int i = 0; i < ints.length; i++) {
244 writeUnsignedInt(ints[i]);
245 }
246 }
247
248 public void writeLong(long l) throws IOException {
249 if (little) {
250 write((int) (l & 0xFF));
251 write((int) ((l >>> 8) & 0xFF));
252 write((int) ((l >>> 16) & 0xFF));
253 write((int) ((l >>> 24) & 0xFF));
254 write((int) ((l >>> 32) & 0xFF));
255 write((int) ((l >>> 40) & 0xFF));
256 write((int) ((l >>> 48) & 0xFF));
257 write((int) ((l >>> 56) & 0xFF));
258 } else {
259 write((int) ((l >>> 56) & 0xFF));
260 write((int) ((l >>> 48) & 0xFF));
261 write((int) ((l >>> 40) & 0xFF));
262 write((int) ((l >>> 32) & 0xFF));
263 write((int) ((l >>> 24) & 0xFF));
264 write((int) ((l >>> 16) & 0xFF));
265 write((int) ((l >>> 8) & 0xFF));
266 write((int) (l & 0xFF));
267 }
268 }
269
270 public void writeFloat(float f) throws IOException {
271 writeInt(Float.floatToIntBits(f));
272 }
273
274 public void writeDouble(double d) throws IOException {
275 writeLong(Double.doubleToLongBits(d));
276 }
277
278 public void writeBytes(String s) throws IOException {
279 for (int i = 0; i < s.length(); i++) {
280 writeByte(s.charAt(i));
281 }
282 }
283
284 public void writeChars(String s) throws IOException {
285 for (int i = 0; i < s.length(); i++) {
286 writeChar(s.charAt(i));
287 }
288 }
289
290 /**
291 * Write a string (UTF)
292 *
293 * @param s string to write
294 * @throws IOException if write fails
295 */
296 public void writeString(String s) throws IOException {
297 writeUTF(s);
298 }
299
300 public void writeUTF(String s) throws IOException {
301 writeUTF(s, this);
302 }
303
304 /**
305 * Write an ascii-z (0 terminated c-string).
306 *
307 * @param s string to write
308 * @throws IOException if write fails
309 */
310 public void writeAsciiZString(String s) throws IOException {
311 writeBytes(s);
312 writeByte(0);
313 }
314
315 /**
316 * Write a UTF string to the data output stream. This method should have
317 * been in DataOutputStream, but is not visible.
318 *
319 * @param s string to write
320 * @param dos stream to write to
321 * @throws IOException if write fails
322 */
323 //
324 public static void writeUTF(String s, DataOutput dos) throws IOException {
325 int strlen = s.length();
326 int utflen = 0;
327 char[] charr = new char[strlen];
328 int c, count = 0;
329
330 s.getChars(0, strlen, charr, 0);
331
332 for (int i = 0; i < strlen; i++) {
333 c = charr[i];
334 if ((c >= 0x0001) && (c <= 0x007F)) {
335 utflen++;
336 } else if (c > 0x07FF) {
337 utflen += 3;
338 } else {
339 utflen += 2;
340 }
341 }
342
343 if (utflen > 65535)
344 throw new UTFDataFormatException();
345
346 byte[] bytearr = new byte[utflen + 2];
347 bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
348 bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
349 for (int i = 0; i < strlen; i++) {
350 c = charr[i];
351 if ((c >= 0x0001) && (c <= 0x007F)) {
352 bytearr[count++] = (byte) c;
353 } else if (c > 0x07FF) {
354 bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
355 bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
356 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
357 } else {
358 bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
359 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
360 }
361 }
362 dos.write(bytearr);
363 }
364 }