1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.io;
import java.nio.charset.ModifiedUtf8;
/**
* Wraps an existing {@link InputStream} and reads typed data from it.
* Typically, this stream has been written by a DataOutputStream. Types that can
* be read include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long,
* 64-bit double, byte strings, and strings encoded in
* {@link DataInput modified UTF-8}.
*
* @see DataOutputStream
*/
public class DataInputStream extends FilterInputStream implements DataInput {
byte[] buff;
/**
* Constructs a new DataInputStream on the InputStream {@code in}. All
* reads are then filtered through this stream. Note that data read by this
* stream is not in a human readable format and was most likely created by a
* DataOutputStream.
*
* <p><strong>Warning:</strong> passing a null source creates an invalid
* {@code DataInputStream}. All operations on such a stream will fail.
*
* @param in
* the source InputStream the filter reads from.
* @see DataOutputStream
* @see RandomAccessFile
*/
public DataInputStream(InputStream in) {
super(in);
buff = new byte[8];
}
/**
* Reads bytes from this stream into the byte array {@code buffer}. Returns
* the number of bytes that have been read.
*
* @param buffer
* the buffer to read bytes into.
* @return the number of bytes that have been read or -1 if the end of the
* stream has been reached.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#write(byte[])
* @see DataOutput#write(byte[], int, int)
*/
@Override
public final int read(byte[] buffer) throws IOException {
return in.read(buffer, 0, buffer.length);
}
/**
* Reads at most {@code length} bytes from this stream and stores them in
* the byte array {@code buffer} starting at {@code offset}. Returns the
* number of bytes that have been read or -1 if no bytes have been read and
* the end of the stream has been reached.
*
* @param buffer
* the byte array in which to store the bytes read.
* @param offset
* the initial position in {@code buffer} to store the bytes
* read from this stream.
* @param length
* the maximum number of bytes to store in {@code buffer}.
* @return the number of bytes that have been read or -1 if the end of the
* stream has been reached.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#write(byte[])
* @see DataOutput#write(byte[], int, int)
*/
@Override
public final int read(byte[] buffer, int offset, int length)
throws IOException {
return in.read(buffer, offset, length);
}
/**
* Reads a boolean from this stream.
*
* @return the next boolean value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before one byte
* has been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeBoolean(boolean)
*/
public final boolean readBoolean() throws IOException {
int temp = in.read();
if (temp < 0) {
throw new EOFException();
}
return temp != 0;
}
/**
* Reads an 8-bit byte value from this stream.
*
* @return the next byte value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before one byte
* has been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeByte(int)
*/
public final byte readByte() throws IOException {
int temp = in.read();
if (temp < 0) {
throw new EOFException();
}
return (byte) temp;
}
/**
* Reads a 16-bit character value from this stream.
*
* @return the next char value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before two bytes
* have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeChar(int)
*/
public final char readChar() throws IOException {
if (readToBuff(2) < 0){
throw new EOFException();
}
return (char) (((buff[0] & 0xff) << 8) | (buff[1] & 0xff));
}
private int readToBuff(int count) throws IOException {
int offset = 0;
while(offset < count) {
int bytesRead = in.read(buff, offset, count - offset);
if(bytesRead == -1) return bytesRead;
offset += bytesRead;
}
return offset;
}
/**
* Reads a 64-bit double value from this stream.
*
* @return the next double value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before eight
* bytes have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeDouble(double)
*/
public final double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
}
/**
* Reads a 32-bit float value from this stream.
*
* @return the next float value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before four
* bytes have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeFloat(float)
*/
public final float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
}
/**
* Reads bytes from this stream into the byte array {@code buffer}. This
* method will block until {@code buffer.length} number of bytes have been
* read.
*
* @param buffer
* to read bytes into.
* @throws EOFException
* if the end of the source stream is reached before enough
* bytes have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#write(byte[])
* @see DataOutput#write(byte[], int, int)
*/
public final void readFully(byte[] buffer) throws IOException {
readFully(buffer, 0, buffer.length);
}
/**
* Reads bytes from this stream and stores them in the byte array {@code
* buffer} starting at the position {@code offset}. This method blocks until
* {@code length} bytes have been read. If {@code length} is zero, then this
* method returns without reading any bytes.
*
* @param buffer
* the byte array into which the data is read.
* @param offset
* the offset in {@code buffer} from where to store the bytes
* read.
* @param length
* the maximum number of bytes to read.
* @throws EOFException
* if the end of the source stream is reached before enough
* bytes have been read.
* @throws IndexOutOfBoundsException
* if {@code offset < 0} or {@code length < 0}, or if {@code
* offset + length} is greater than the size of {@code buffer}.
* @throws IOException
* if a problem occurs while reading from this stream.
* @throws NullPointerException
* if {@code buffer} or the source stream are null.
* @see java.io.DataInput#readFully(byte[], int, int)
*/
public final void readFully(byte[] buffer, int offset, int length) throws IOException {
// BEGIN android-removed
// if (length < 0) {
// throw new IndexOutOfBoundsException();
// }
// END android-removed
if (length == 0) {
return;
}
if (in == null) {
throw new NullPointerException("in == null");
}
if (buffer == null) {
throw new NullPointerException("buffer == null");
}
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// used (offset | length) < 0 instead of separate (offset < 0) and
// (length < 0) check to safe one operation
if ((offset | length) < 0 || offset > buffer.length - length) {
throw new IndexOutOfBoundsException();
}
// END android-changed
while (length > 0) {
int result = in.read(buffer, offset, length);
if (result < 0) {
throw new EOFException();
}
offset += result;
length -= result;
}
}
/**
* Reads a 32-bit integer value from this stream.
*
* @return the next int value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before four
* bytes have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeInt(int)
*/
public final int readInt() throws IOException {
if (readToBuff(4) < 0){
throw new EOFException();
}
return ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) |
((buff[2] & 0xff) << 8) | (buff[3] & 0xff);
}
/**
* Returns a string that contains the next line of text available from the
* source stream. A line is represented by zero or more characters followed
* by {@code '\n'}, {@code '\r'}, {@code "\r\n"} or the end of the stream.
* The string does not include the newline sequence.
*
* @return the contents of the line or {@code null} if no characters were
* read before the end of the source stream has been reached.
* @throws IOException
* if a problem occurs while reading from this stream.
* @deprecated Use {@link BufferedReader}
*/
@Deprecated
public final String readLine() throws IOException {
StringBuilder line = new StringBuilder(80); // Typical line length
boolean foundTerminator = false;
while (true) {
int nextByte = in.read();
switch (nextByte) {
case -1:
if (line.length() == 0 && !foundTerminator) {
return null;
}
return line.toString();
case (byte) '\r':
if (foundTerminator) {
((PushbackInputStream) in).unread(nextByte);
return line.toString();
}
foundTerminator = true;
/* Have to be able to peek ahead one byte */
if (!(in.getClass() == PushbackInputStream.class)) {
in = new PushbackInputStream(in);
}
break;
case (byte) '\n':
return line.toString();
default:
if (foundTerminator) {
((PushbackInputStream) in).unread(nextByte);
return line.toString();
}
line.append((char) nextByte);
}
}
}
/**
* Reads a 64-bit long value from this stream.
*
* @return the next long value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before eight
* bytes have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeLong(long)
*/
public final long readLong() throws IOException {
if (readToBuff(8) < 0){
throw new EOFException();
}
int i1 = ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) |
((buff[2] & 0xff) << 8) | (buff[3] & 0xff);
int i2 = ((buff[4] & 0xff) << 24) | ((buff[5] & 0xff) << 16) |
((buff[6] & 0xff) << 8) | (buff[7] & 0xff);
return ((i1 & 0xffffffffL) << 32) | (i2 & 0xffffffffL);
}
/**
* Reads a 16-bit short value from this stream.
*
* @return the next short value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before two bytes
* have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeShort(int)
*/
public final short readShort() throws IOException {
if (readToBuff(2) < 0){
throw new EOFException();
}
return (short) (((buff[0] & 0xff) << 8) | (buff[1] & 0xff));
}
/**
* Reads an unsigned 8-bit byte value from this stream and returns it as an
* int.
*
* @return the next unsigned byte value from the source stream.
* @throws EOFException
* if the end of the filtered stream has been reached before one
* byte has been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeByte(int)
*/
public final int readUnsignedByte() throws IOException {
int temp = in.read();
if (temp < 0) {
throw new EOFException();
}
return temp;
}
/**
* Reads a 16-bit unsigned short value from this stream and returns it as an
* int.
*
* @return the next unsigned short value from the source stream.
* @throws EOFException
* if the end of the filtered stream is reached before two bytes
* have been read.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeShort(int)
*/
public final int readUnsignedShort() throws IOException {
if (readToBuff(2) < 0){
throw new EOFException();
}
return (char) (((buff[0] & 0xff) << 8) | (buff[1] & 0xff));
}
/**
* Reads an string encoded in {@link DataInput modified UTF-8} from this
* stream.
*
* @return the next {@link DataInput MUTF-8} encoded string read from the
* source stream.
* @throws EOFException if the end of the input is reached before the read
* request can be satisfied.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutput#writeUTF(java.lang.String)
*/
public final String readUTF() throws IOException {
return decodeUTF(readUnsignedShort());
}
String decodeUTF(int utfSize) throws IOException {
return decodeUTF(utfSize, this);
}
private static String decodeUTF(int utfSize, DataInput in) throws IOException {
byte[] buf = new byte[utfSize];
in.readFully(buf, 0, utfSize);
return ModifiedUtf8.decode(buf, new char[utfSize], 0, utfSize);
}
/**
* Reads a string encoded in {@link DataInput modified UTF-8} from the
* {@code DataInput} stream {@code in}.
*
* @param in
* the input stream to read from.
* @return the next {@link DataInput MUTF-8} encoded string from the source
* stream.
* @throws IOException
* if a problem occurs while reading from this stream.
* @see DataOutputStream#writeUTF(java.lang.String)
*/
public static final String readUTF(DataInput in) throws IOException {
return decodeUTF(in.readUnsignedShort(), in);
}
/**
* Skips {@code count} number of bytes in this stream. Subsequent {@code
* read()}s will not return these bytes unless {@code reset()} is used.
*
* This method will not throw an {@link EOFException} if the end of the
* input is reached before {@code count} bytes where skipped.
*
* @param count
* the number of bytes to skip.
* @return the number of bytes actually skipped.
* @throws IOException
* if a problem occurs during skipping.
* @see #mark(int)
* @see #reset()
*/
public final int skipBytes(int count) throws IOException {
int skipped = 0;
long skip;
while (skipped < count && (skip = in.skip(count - skipped)) != 0) {
skipped += skip;
}
// BEGIN android-removed
// if (skipped < 0) {
// throw new EOFException();
// }
// END android-removed
return skipped;
}
}
|