Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/io/DataInputStream.java
41152 views
1
/*
2
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.io;
27
28
import java.util.Objects;
29
30
/**
31
* A data input stream lets an application read primitive Java data
32
* types from an underlying input stream in a machine-independent
33
* way. An application uses a data output stream to write data that
34
* can later be read by a data input stream.
35
* <p>
36
* A DataInputStream is not safe for use by multiple concurrent
37
* threads. If a DataInputStream is to be used by more than one
38
* thread then access to the data input stream should be controlled
39
* by appropriate synchronization.
40
*
41
* @author Arthur van Hoff
42
* @see java.io.DataOutputStream
43
* @since 1.0
44
*/
45
public class DataInputStream extends FilterInputStream implements DataInput {
46
47
/**
48
* Creates a DataInputStream that uses the specified
49
* underlying InputStream.
50
*
51
* @param in the specified input stream
52
*/
53
public DataInputStream(InputStream in) {
54
super(in);
55
}
56
57
/**
58
* working arrays initialized on demand by readUTF
59
*/
60
private byte bytearr[] = new byte[80];
61
private char chararr[] = new char[80];
62
63
/**
64
* Reads some number of bytes from the contained input stream and
65
* stores them into the buffer array {@code b}. The number of
66
* bytes actually read is returned as an integer. This method blocks
67
* until input data is available, end of file is detected, or an
68
* exception is thrown.
69
*
70
* <p>If {@code b} is null, a {@code NullPointerException} is
71
* thrown. If the length of {@code b} is zero, then no bytes are
72
* read and {@code 0} is returned; otherwise, there is an attempt
73
* to read at least one byte. If no byte is available because the
74
* stream is at end of file, the value {@code -1} is returned;
75
* otherwise, at least one byte is read and stored into {@code b}.
76
*
77
* <p>The first byte read is stored into element {@code b[0]}, the
78
* next one into {@code b[1]}, and so on. The number of bytes read
79
* is, at most, equal to the length of {@code b}. Let {@code k}
80
* be the number of bytes actually read; these bytes will be stored in
81
* elements {@code b[0]} through {@code b[k-1]}, leaving
82
* elements {@code b[k]} through {@code b[b.length-1]}
83
* unaffected.
84
*
85
* <p>The {@code read(b)} method has the same effect as:
86
* <blockquote><pre>
87
* read(b, 0, b.length)
88
* </pre></blockquote>
89
*
90
* @param b the buffer into which the data is read.
91
* @return the total number of bytes read into the buffer, or
92
* {@code -1} if there is no more data because the end
93
* of the stream has been reached.
94
* @throws IOException if the first byte cannot be read for any reason
95
* other than end of file, the stream has been closed and the underlying
96
* input stream does not support reading after close, or another I/O
97
* error occurs.
98
* @see java.io.FilterInputStream#in
99
* @see java.io.InputStream#read(byte[], int, int)
100
*/
101
public final int read(byte b[]) throws IOException {
102
return in.read(b, 0, b.length);
103
}
104
105
/**
106
* Reads up to {@code len} bytes of data from the contained
107
* input stream into an array of bytes. An attempt is made to read
108
* as many as {@code len} bytes, but a smaller number may be read,
109
* possibly zero. The number of bytes actually read is returned as an
110
* integer.
111
*
112
* <p> This method blocks until input data is available, end of file is
113
* detected, or an exception is thrown.
114
*
115
* <p> If {@code len} is zero, then no bytes are read and
116
* {@code 0} is returned; otherwise, there is an attempt to read at
117
* least one byte. If no byte is available because the stream is at end of
118
* file, the value {@code -1} is returned; otherwise, at least one
119
* byte is read and stored into {@code b}.
120
*
121
* <p> The first byte read is stored into element {@code b[off]}, the
122
* next one into {@code b[off+1]}, and so on. The number of bytes read
123
* is, at most, equal to {@code len}. Let <i>k</i> be the number of
124
* bytes actually read; these bytes will be stored in elements
125
* {@code b[off]} through {@code b[off+}<i>k</i>{@code -1]},
126
* leaving elements {@code b[off+}<i>k</i>{@code ]} through
127
* {@code b[off+len-1]} unaffected.
128
*
129
* <p> In every case, elements {@code b[0]} through
130
* {@code b[off]} and elements {@code b[off+len]} through
131
* {@code b[b.length-1]} are unaffected.
132
*
133
* @param b the buffer into which the data is read.
134
* @param off the start offset in the destination array {@code b}
135
* @param len the maximum number of bytes read.
136
* @return the total number of bytes read into the buffer, or
137
* {@code -1} if there is no more data because the end
138
* of the stream has been reached.
139
* @throws NullPointerException If {@code b} is {@code null}.
140
* @throws IndexOutOfBoundsException If {@code off} is negative,
141
* {@code len} is negative, or {@code len} is greater than
142
* {@code b.length - off}
143
* @throws IOException if the first byte cannot be read for any reason
144
* other than end of file, the stream has been closed and the underlying
145
* input stream does not support reading after close, or another I/O
146
* error occurs.
147
* @see java.io.FilterInputStream#in
148
* @see java.io.InputStream#read(byte[], int, int)
149
*/
150
public final int read(byte b[], int off, int len) throws IOException {
151
return in.read(b, off, len);
152
}
153
154
/**
155
* See the general contract of the {@code readFully}
156
* method of {@code DataInput}.
157
* <p>
158
* Bytes
159
* for this operation are read from the contained
160
* input stream.
161
*
162
* @param b the buffer into which the data is read.
163
* @throws NullPointerException if {@code b} is {@code null}.
164
* @throws EOFException if this input stream reaches the end before
165
* reading all the bytes.
166
* @throws IOException the stream has been closed and the contained
167
* input stream does not support reading after close, or
168
* another I/O error occurs.
169
* @see java.io.FilterInputStream#in
170
*/
171
public final void readFully(byte b[]) throws IOException {
172
readFully(b, 0, b.length);
173
}
174
175
/**
176
* See the general contract of the {@code readFully}
177
* method of {@code DataInput}.
178
* <p>
179
* Bytes
180
* for this operation are read from the contained
181
* input stream.
182
*
183
* @param b the buffer into which the data is read.
184
* @param off the start offset in the data array {@code b}.
185
* @param len the number of bytes to read.
186
* @throws NullPointerException if {@code b} is {@code null}.
187
* @throws IndexOutOfBoundsException if {@code off} is negative,
188
* {@code len} is negative, or {@code len} is greater than
189
* {@code b.length - off}.
190
* @throws EOFException if this input stream reaches the end before
191
* reading all the bytes.
192
* @throws IOException the stream has been closed and the contained
193
* input stream does not support reading after close, or
194
* another I/O error occurs.
195
* @see java.io.FilterInputStream#in
196
*/
197
public final void readFully(byte b[], int off, int len) throws IOException {
198
Objects.checkFromIndexSize(off, len, b.length);
199
int n = 0;
200
while (n < len) {
201
int count = in.read(b, off + n, len - n);
202
if (count < 0)
203
throw new EOFException();
204
n += count;
205
}
206
}
207
208
/**
209
* See the general contract of the {@code skipBytes}
210
* method of {@code DataInput}.
211
* <p>
212
* Bytes for this operation are read from the contained
213
* input stream.
214
*
215
* @param n the number of bytes to be skipped.
216
* @return the actual number of bytes skipped.
217
* @throws IOException if the contained input stream does not support
218
* seek, or the stream has been closed and
219
* the contained input stream does not support
220
* reading after close, or another I/O error occurs.
221
*/
222
public final int skipBytes(int n) throws IOException {
223
int total = 0;
224
int cur = 0;
225
226
while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
227
total += cur;
228
}
229
230
return total;
231
}
232
233
/**
234
* See the general contract of the {@code readBoolean}
235
* method of {@code DataInput}.
236
* <p>
237
* Bytes for this operation are read from the contained
238
* input stream.
239
*
240
* @return the {@code boolean} value read.
241
* @throws EOFException if this input stream has reached the end.
242
* @throws IOException the stream has been closed and the contained
243
* input stream does not support reading after close, or
244
* another I/O error occurs.
245
* @see java.io.FilterInputStream#in
246
*/
247
public final boolean readBoolean() throws IOException {
248
int ch = in.read();
249
if (ch < 0)
250
throw new EOFException();
251
return (ch != 0);
252
}
253
254
/**
255
* See the general contract of the {@code readByte}
256
* method of {@code DataInput}.
257
* <p>
258
* Bytes
259
* for this operation are read from the contained
260
* input stream.
261
*
262
* @return the next byte of this input stream as a signed 8-bit
263
* {@code byte}.
264
* @throws EOFException if this input stream has reached the end.
265
* @throws IOException the stream has been closed and the contained
266
* input stream does not support reading after close, or
267
* another I/O error occurs.
268
* @see java.io.FilterInputStream#in
269
*/
270
public final byte readByte() throws IOException {
271
int ch = in.read();
272
if (ch < 0)
273
throw new EOFException();
274
return (byte)(ch);
275
}
276
277
/**
278
* See the general contract of the {@code readUnsignedByte}
279
* method of {@code DataInput}.
280
* <p>
281
* Bytes
282
* for this operation are read from the contained
283
* input stream.
284
*
285
* @return the next byte of this input stream, interpreted as an
286
* unsigned 8-bit number.
287
* @throws EOFException if this input stream has reached the end.
288
* @throws IOException the stream has been closed and the contained
289
* input stream does not support reading after close, or
290
* another I/O error occurs.
291
* @see java.io.FilterInputStream#in
292
*/
293
public final int readUnsignedByte() throws IOException {
294
int ch = in.read();
295
if (ch < 0)
296
throw new EOFException();
297
return ch;
298
}
299
300
/**
301
* See the general contract of the {@code readShort}
302
* method of {@code DataInput}.
303
* <p>
304
* Bytes
305
* for this operation are read from the contained
306
* input stream.
307
*
308
* @return the next two bytes of this input stream, interpreted as a
309
* signed 16-bit number.
310
* @throws EOFException if this input stream reaches the end before
311
* reading two bytes.
312
* @throws IOException the stream has been closed and the contained
313
* input stream does not support reading after close, or
314
* another I/O error occurs.
315
* @see java.io.FilterInputStream#in
316
*/
317
public final short readShort() throws IOException {
318
int ch1 = in.read();
319
int ch2 = in.read();
320
if ((ch1 | ch2) < 0)
321
throw new EOFException();
322
return (short)((ch1 << 8) + (ch2 << 0));
323
}
324
325
/**
326
* See the general contract of the {@code readUnsignedShort}
327
* method of {@code DataInput}.
328
* <p>
329
* Bytes
330
* for this operation are read from the contained
331
* input stream.
332
*
333
* @return the next two bytes of this input stream, interpreted as an
334
* unsigned 16-bit integer.
335
* @throws EOFException if this input stream reaches the end before
336
* reading two bytes.
337
* @throws IOException the stream has been closed and the contained
338
* input stream does not support reading after close, or
339
* another I/O error occurs.
340
* @see java.io.FilterInputStream#in
341
*/
342
public final int readUnsignedShort() throws IOException {
343
int ch1 = in.read();
344
int ch2 = in.read();
345
if ((ch1 | ch2) < 0)
346
throw new EOFException();
347
return (ch1 << 8) + (ch2 << 0);
348
}
349
350
/**
351
* See the general contract of the {@code readChar}
352
* method of {@code DataInput}.
353
* <p>
354
* Bytes
355
* for this operation are read from the contained
356
* input stream.
357
*
358
* @return the next two bytes of this input stream, interpreted as a
359
* {@code char}.
360
* @throws EOFException if this input stream reaches the end before
361
* reading two bytes.
362
* @throws IOException the stream has been closed and the contained
363
* input stream does not support reading after close, or
364
* another I/O error occurs.
365
* @see java.io.FilterInputStream#in
366
*/
367
public final char readChar() throws IOException {
368
int ch1 = in.read();
369
int ch2 = in.read();
370
if ((ch1 | ch2) < 0)
371
throw new EOFException();
372
return (char)((ch1 << 8) + (ch2 << 0));
373
}
374
375
/**
376
* See the general contract of the {@code readInt}
377
* method of {@code DataInput}.
378
* <p>
379
* Bytes
380
* for this operation are read from the contained
381
* input stream.
382
*
383
* @return the next four bytes of this input stream, interpreted as an
384
* {@code int}.
385
* @throws EOFException if this input stream reaches the end before
386
* reading four bytes.
387
* @throws IOException the stream has been closed and the contained
388
* input stream does not support reading after close, or
389
* another I/O error occurs.
390
* @see java.io.FilterInputStream#in
391
*/
392
public final int readInt() throws IOException {
393
int ch1 = in.read();
394
int ch2 = in.read();
395
int ch3 = in.read();
396
int ch4 = in.read();
397
if ((ch1 | ch2 | ch3 | ch4) < 0)
398
throw new EOFException();
399
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
400
}
401
402
private byte readBuffer[] = new byte[8];
403
404
/**
405
* See the general contract of the {@code readLong}
406
* method of {@code DataInput}.
407
* <p>
408
* Bytes
409
* for this operation are read from the contained
410
* input stream.
411
*
412
* @return the next eight bytes of this input stream, interpreted as a
413
* {@code long}.
414
* @throws EOFException if this input stream reaches the end before
415
* reading eight bytes.
416
* @throws IOException the stream has been closed and the contained
417
* input stream does not support reading after close, or
418
* another I/O error occurs.
419
* @see java.io.FilterInputStream#in
420
*/
421
public final long readLong() throws IOException {
422
readFully(readBuffer, 0, 8);
423
return (((long)readBuffer[0] << 56) +
424
((long)(readBuffer[1] & 255) << 48) +
425
((long)(readBuffer[2] & 255) << 40) +
426
((long)(readBuffer[3] & 255) << 32) +
427
((long)(readBuffer[4] & 255) << 24) +
428
((readBuffer[5] & 255) << 16) +
429
((readBuffer[6] & 255) << 8) +
430
((readBuffer[7] & 255) << 0));
431
}
432
433
/**
434
* See the general contract of the {@code readFloat}
435
* method of {@code DataInput}.
436
* <p>
437
* Bytes
438
* for this operation are read from the contained
439
* input stream.
440
*
441
* @return the next four bytes of this input stream, interpreted as a
442
* {@code float}.
443
* @throws EOFException if this input stream reaches the end before
444
* reading four bytes.
445
* @throws IOException the stream has been closed and the contained
446
* input stream does not support reading after close, or
447
* another I/O error occurs.
448
* @see java.io.DataInputStream#readInt()
449
* @see java.lang.Float#intBitsToFloat(int)
450
*/
451
public final float readFloat() throws IOException {
452
return Float.intBitsToFloat(readInt());
453
}
454
455
/**
456
* See the general contract of the {@code readDouble}
457
* method of {@code DataInput}.
458
* <p>
459
* Bytes
460
* for this operation are read from the contained
461
* input stream.
462
*
463
* @return the next eight bytes of this input stream, interpreted as a
464
* {@code double}.
465
* @throws EOFException if this input stream reaches the end before
466
* reading eight bytes.
467
* @throws IOException the stream has been closed and the contained
468
* input stream does not support reading after close, or
469
* another I/O error occurs.
470
* @see java.io.DataInputStream#readLong()
471
* @see java.lang.Double#longBitsToDouble(long)
472
*/
473
public final double readDouble() throws IOException {
474
return Double.longBitsToDouble(readLong());
475
}
476
477
private char lineBuffer[];
478
479
/**
480
* See the general contract of the {@code readLine}
481
* method of {@code DataInput}.
482
* <p>
483
* Bytes
484
* for this operation are read from the contained
485
* input stream.
486
*
487
* @deprecated This method does not properly convert bytes to characters.
488
* As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
489
* {@code BufferedReader.readLine()} method. Programs that use the
490
* {@code DataInputStream} class to read lines can be converted to use
491
* the {@code BufferedReader} class by replacing code of the form:
492
* <blockquote><pre>
493
* DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
494
* </pre></blockquote>
495
* with:
496
* <blockquote><pre>
497
* BufferedReader d
498
* =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
499
* </pre></blockquote>
500
*
501
* @return the next line of text from this input stream.
502
* @throws IOException if an I/O error occurs.
503
* @see java.io.BufferedReader#readLine()
504
* @see java.io.FilterInputStream#in
505
*/
506
@Deprecated
507
public final String readLine() throws IOException {
508
char buf[] = lineBuffer;
509
510
if (buf == null) {
511
buf = lineBuffer = new char[128];
512
}
513
514
int room = buf.length;
515
int offset = 0;
516
int c;
517
518
loop: while (true) {
519
switch (c = in.read()) {
520
case -1:
521
case '\n':
522
break loop;
523
524
case '\r':
525
int c2 = in.read();
526
if ((c2 != '\n') && (c2 != -1)) {
527
if (!(in instanceof PushbackInputStream)) {
528
this.in = new PushbackInputStream(in);
529
}
530
((PushbackInputStream)in).unread(c2);
531
}
532
break loop;
533
534
default:
535
if (--room < 0) {
536
buf = new char[offset + 128];
537
room = buf.length - offset - 1;
538
System.arraycopy(lineBuffer, 0, buf, 0, offset);
539
lineBuffer = buf;
540
}
541
buf[offset++] = (char) c;
542
break;
543
}
544
}
545
if ((c == -1) && (offset == 0)) {
546
return null;
547
}
548
return String.copyValueOf(buf, 0, offset);
549
}
550
551
/**
552
* See the general contract of the {@code readUTF}
553
* method of {@code DataInput}.
554
* <p>
555
* Bytes
556
* for this operation are read from the contained
557
* input stream.
558
*
559
* @return a Unicode string.
560
* @throws EOFException if this input stream reaches the end before
561
* reading all the bytes.
562
* @throws IOException the stream has been closed and the contained
563
* input stream does not support reading after close, or
564
* another I/O error occurs.
565
* @throws UTFDataFormatException if the bytes do not represent a valid
566
* modified UTF-8 encoding of a string.
567
* @see java.io.DataInputStream#readUTF(java.io.DataInput)
568
*/
569
public final String readUTF() throws IOException {
570
return readUTF(this);
571
}
572
573
/**
574
* Reads from the
575
* stream {@code in} a representation
576
* of a Unicode character string encoded in
577
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
578
* this string of characters is then returned as a {@code String}.
579
* The details of the modified UTF-8 representation
580
* are exactly the same as for the {@code readUTF}
581
* method of {@code DataInput}.
582
*
583
* @param in a data input stream.
584
* @return a Unicode string.
585
* @throws EOFException if the input stream reaches the end
586
* before all the bytes.
587
* @throws IOException the stream has been closed and the contained
588
* input stream does not support reading after close, or
589
* another I/O error occurs.
590
* @throws UTFDataFormatException if the bytes do not represent a
591
* valid modified UTF-8 encoding of a Unicode string.
592
* @see java.io.DataInputStream#readUnsignedShort()
593
*/
594
public static final String readUTF(DataInput in) throws IOException {
595
int utflen = in.readUnsignedShort();
596
byte[] bytearr = null;
597
char[] chararr = null;
598
if (in instanceof DataInputStream dis) {
599
if (dis.bytearr.length < utflen){
600
dis.bytearr = new byte[utflen*2];
601
dis.chararr = new char[utflen*2];
602
}
603
chararr = dis.chararr;
604
bytearr = dis.bytearr;
605
} else {
606
bytearr = new byte[utflen];
607
chararr = new char[utflen];
608
}
609
610
int c, char2, char3;
611
int count = 0;
612
int chararr_count=0;
613
614
in.readFully(bytearr, 0, utflen);
615
616
while (count < utflen) {
617
c = (int) bytearr[count] & 0xff;
618
if (c > 127) break;
619
count++;
620
chararr[chararr_count++]=(char)c;
621
}
622
623
while (count < utflen) {
624
c = (int) bytearr[count] & 0xff;
625
switch (c >> 4) {
626
case 0, 1, 2, 3, 4, 5, 6, 7 -> {
627
/* 0xxxxxxx*/
628
count++;
629
chararr[chararr_count++]=(char)c;
630
}
631
case 12, 13 -> {
632
/* 110x xxxx 10xx xxxx*/
633
count += 2;
634
if (count > utflen)
635
throw new UTFDataFormatException(
636
"malformed input: partial character at end");
637
char2 = (int) bytearr[count-1];
638
if ((char2 & 0xC0) != 0x80)
639
throw new UTFDataFormatException(
640
"malformed input around byte " + count);
641
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
642
(char2 & 0x3F));
643
}
644
case 14 -> {
645
/* 1110 xxxx 10xx xxxx 10xx xxxx */
646
count += 3;
647
if (count > utflen)
648
throw new UTFDataFormatException(
649
"malformed input: partial character at end");
650
char2 = (int) bytearr[count-2];
651
char3 = (int) bytearr[count-1];
652
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
653
throw new UTFDataFormatException(
654
"malformed input around byte " + (count-1));
655
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
656
((char2 & 0x3F) << 6) |
657
((char3 & 0x3F) << 0));
658
}
659
default ->
660
/* 10xx xxxx, 1111 xxxx */
661
throw new UTFDataFormatException(
662
"malformed input around byte " + count);
663
}
664
}
665
// The number of chars produced may be less than utflen
666
return new String(chararr, 0, chararr_count);
667
}
668
}
669
670