Debian Codegraph @

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

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.DataInput;
5 import java.io.DataInputStream;
6 import java.io.EOFException;
7 import java.io.IOException;
8 import java.io.InputStream;
9
10 /**
11 * Class to read bytes and pairs of bytes in both little and big endian order.
12 *
13 * @author Mark Donszelmann
14 * @author Charles Loomis
15 * @version $Id: ByteOrderInputStream.java 8584 2006-08-10 23:06:37Z duns $
16 */
17 public class ByteOrderInputStream extends BitInputStream implements DataInput {
18
19 protected boolean little;
20
21 /**
22 * Create a byte order (big-endian) input stream from given stream.
23 *
24 * @param in stream to read from
25 */
26 public ByteOrderInputStream(InputStream in) {
27 this(in, false);
28 }
29
30 /**
31 * Create a byte order input stream from given stream.
32 *
33 * @param in stream to read from
34 * @param littleEndian true if stream should be little endian.
35 */
36 public ByteOrderInputStream(InputStream in, boolean littleEndian) {
37 super(in);
38 little = littleEndian;
39 }
40
41 public void readFully(byte b[]) throws IOException {
42
43 readFully(b, 0, b.length);
44 }
45
46 public void readFully(byte b[], int off, int len) throws IOException {
47
48 if (len < 0)
49 throw new IndexOutOfBoundsException();
50 int n = 0;
51 while (n < len) {
52 int count = read(b, off + n, len - n);
53 if (count < 0)
54 throw new EOFException();
55 n += count;
56 }
57 }
58
59 public int skipBytes(int n) throws IOException {
60 int total = 0;
61 int cur = 0;
62
63 while ((total < n) && ((cur = (int) skip(n - total)) > 0)) {
64 total += cur;
65 }
66
67 return total;
68 }
69
70 public boolean readBoolean() throws IOException {
71 int b = readUnsignedByte();
72 return (b != 0);
73 }
74
75 public char readChar() throws IOException {
76 int b1 = readUnsignedByte();
77 int b2 = readUnsignedByte();
78 return (little) ? (char) ((b1 << 8) + b2) : (char) ((b2 << 8) + b1);
79 }
80
81 /**
82 * Read a signed byte.
83 */
84 public byte readByte() throws IOException {
85
86 byteAlign();
87 int b = read();
88 if (b < 0)
89 throw new EOFException();
90 return (byte) b;
91 }
92
93 /**
94 * Read n bytes and return in byte array.
95 *
96 * @param n number of bytes to read
97 * @return byte array
98 * @throws IOException if read fails
99 */
100 public byte[] readByte(int n) throws IOException {
101
102 byteAlign();
103 byte[] bytes = new byte[n];
104 for (int i = 0; i < n; i++) {
105 int b = read();
106 if (b < 0)
107 throw new EOFException();
108 bytes[i] = (byte) b;
109 }
110 return bytes;
111 }
112
113 /**
114 * Read an unsigned byte.
115 */
116 public int readUnsignedByte() throws IOException {
117
118 byteAlign();
119 int ub = read();
120 if (ub < 0)
121 throw new EOFException();
122 return ub;
123 }
124
125 /**
126 * Read n unsigned bytes and return in int array.
127 *
128 * @param n number of bytes to read
129 * @return int array
130 * @throws IOException if read fails
131 */
132 public int[] readUnsignedByte(int n) throws IOException {
133
134 byteAlign();
135 int[] bytes = new int[n];
136 for (int i = 0; i < n; i++) {
137 int ub = read();
138 if (ub < 0)
139 throw new EOFException();
140 bytes[i] = ub;
141 }
142 return bytes;
143 }
144
145 /**
146 * Read a signed short.
147 */
148 public short readShort() throws IOException {
149
150 int i1 = readUnsignedByte();
151 int i2 = readUnsignedByte();
152 return (little) ? (short) ((i2 << 8) + i1) : (short) ((i1 << 8) + i2);
153 }
154
155 /**
156 * Read n shorts and return in short array
157 *
158 * @param n number of shorts to read
159 * @return short array
160 * @throws IOException if read fails
161 */
162 public short[] readShort(int n) throws IOException {
163
164 short[] shorts = new short[n];
165 for (int i = 0; i < n; i++) {
166 shorts[i] = readShort();
167 }
168 return shorts;
169 }
170
171 /**
172 * Read an unsigned short.
173 */
174 public int readUnsignedShort() throws IOException {
175
176 byteAlign();
177 int i1 = readUnsignedByte();
178 int i2 = readUnsignedByte();
179 return (little) ? (i2 << 8) + i1 : (i1 << 8) + i2;
180 }
181
182 /**
183 * Read n unsigned shorts and return in int array
184 *
185 * @param n number of shorts to read
186 * @return int array
187 * @throws IOException if read fails
188 */
189 public int[] readUnsignedShort(int n) throws IOException {
190
191 int[] shorts = new int[n];
192 for (int i = 0; i < n; i++) {
193 shorts[i] = readUnsignedShort();
194 }
195 return shorts;
196 }
197
198 /**
199 * Read a signed integer.
200 */
201 public int readInt() throws IOException {
202
203 int i1 = readUnsignedByte();
204 int i2 = readUnsignedByte();
205 int i3 = readUnsignedByte();
206 int i4 = readUnsignedByte();
207 return (little) ? (i4 << 24) + (i3 << 16) + (i2 << 8) + i1 : (i1 << 24)
208 + (i2 << 16) + (i3 << 8) + i4;
209 }
210
211 /**
212 * Read n ints and return in int array.
213 *
214 * @param n number of ints to read
215 * @return int array
216 * @throws IOException if read fails
217 */
218 public int[] readInt(int n) throws IOException {
219
220 int[] ints = new int[n];
221 for (int i = 0; i < n; i++) {
222 ints[i] = readInt();
223 }
224 return ints;
225 }
226
227 /**
228 * Read an unsigned integer.
229 *
230 * @return long
231 * @throws IOException if read fails
232 */
233 public long readUnsignedInt() throws IOException {
234
235 long i1 = readUnsignedByte();
236 long i2 = readUnsignedByte();
237 long i3 = readUnsignedByte();
238 long i4 = readUnsignedByte();
239 return (little) ? (i4 << 24) + (i3 << 16) + (i2 << 8) + i1 : (i1 << 24)
240 + (i2 << 16) + (i3 << 8) + i4;
241 }
242
243 /**
244 * Read n unsigned ints and return in long array.
245 *
246 * @param n number of ints to read
247 * @return long array
248 * @throws IOException if read fails
249 */
250 public long[] readUnsignedInt(int n) throws IOException {
251
252 long[] ints = new long[n];
253 for (int i = 0; i < n; i++) {
254 ints[i] = readUnsignedInt();
255 }
256 return ints;
257 }
258
259 public long readLong() throws IOException {
260 long i1 = readInt();
261 long i2 = readInt();
262 return (little) ? (i2 << 32) + (i1 & 0xFFFFFFFFL) : (i1 << 32)
263 + (i2 & 0xFFFFFFFFL);
264 }
265
266 public float readFloat() throws IOException {
267 return Float.intBitsToFloat(readInt());
268 }
269
270 public double readDouble() throws IOException {
271 return Double.longBitsToDouble(readLong());
272 }
273
274 /**
275 * @deprecated
276 */
277 public String readLine() throws IOException {
278 throw new IOException(
279 "ByteOrderInputStream.readLine() is deprecated and not implemented.");
280 }
281
282 /**
283 * Read a string (UTF).
284 *
285 * @return string
286 * @throws IOException if read fails
287 */
288 public String readString() throws IOException {
289 return readUTF();
290 }
291
292 public String readUTF() throws IOException {
293 return DataInputStream.readUTF(this);
294 }
295
296 /**
297 * Read an ascii-z (0 terminated c-string).
298 *
299 * @return string
300 * @throws IOException if read fails
301 */
302 public String readAsciiZString() throws IOException {
303 StringBuffer buffer = new StringBuffer();
304 char c = (char) readUnsignedByte();
305 while (c != 0) {
306 buffer.append(c);
307 c = (char) readUnsignedByte();
308 }
309 return buffer.toString();
310 }
311 }