Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/io/DataInput.java
41152 views
1
/*
2
* Copyright (c) 1995, 2019, 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
/**
29
* The {@code DataInput} interface provides
30
* for reading bytes from a binary stream and
31
* reconstructing from them data in any of
32
* the Java primitive types. There is also
33
* a
34
* facility for reconstructing a {@code String}
35
* from data in
36
* <a href="#modified-utf-8">modified UTF-8</a>
37
* format.
38
* <p>
39
* It is generally true of all the reading
40
* routines in this interface that if end of
41
* file is reached before the desired number
42
* of bytes has been read, an {@code EOFException}
43
* (which is a kind of {@code IOException})
44
* is thrown. If any byte cannot be read for
45
* any reason other than end of file, an {@code IOException}
46
* other than {@code EOFException} is
47
* thrown. In particular, an {@code IOException}
48
* may be thrown if the input stream has been
49
* closed.
50
*
51
* <h2><a id="modified-utf-8">Modified UTF-8</a></h2>
52
* <p>
53
* Implementations of the DataInput and DataOutput interfaces represent
54
* Unicode strings in a format that is a slight modification of UTF-8.
55
* (For information regarding the standard UTF-8 format, see section
56
* <i>3.9 Unicode Encoding Forms</i> of <i>The Unicode Standard, Version
57
* 4.0</i>)
58
*
59
* <ul>
60
* <li>Characters in the range {@code '\u005Cu0001'} to
61
* {@code '\u005Cu007F'} are represented by a single byte.
62
* <li>The null character {@code '\u005Cu0000'} and characters
63
* in the range {@code '\u005Cu0080'} to {@code '\u005Cu07FF'} are
64
* represented by a pair of bytes.
65
* <li>Characters in the range {@code '\u005Cu0800'}
66
* to {@code '\u005CuFFFF'} are represented by three bytes.
67
* </ul>
68
*
69
* <table class="plain" style="margin-left:2em;">
70
* <caption>Encoding of UTF-8 values</caption>
71
* <thead>
72
* <tr>
73
* <th scope="col" rowspan="2">Value</th>
74
* <th scope="col" rowspan="2">Byte</th>
75
* <th scope="col" colspan="8" id="bit_a">Bit Values</th>
76
* </tr>
77
* <tr>
78
* <!-- Value -->
79
* <!-- Byte -->
80
* <th scope="col" style="width:3em"> 7 </th>
81
* <th scope="col" style="width:3em"> 6 </th>
82
* <th scope="col" style="width:3em"> 5 </th>
83
* <th scope="col" style="width:3em"> 4 </th>
84
* <th scope="col" style="width:3em"> 3 </th>
85
* <th scope="col" style="width:3em"> 2 </th>
86
* <th scope="col" style="width:3em"> 1 </th>
87
* <th scope="col" style="width:3em"> 0 </th>
88
* </thead>
89
* <tbody>
90
* <tr>
91
* <th scope="row" style="text-align:left; font-weight:normal">
92
* {@code \u005Cu0001} to {@code \u005Cu007F} </th>
93
* <th scope="row" style="font-weight:normal; text-align:center"> 1 </th>
94
* <td style="text-align:center">0
95
* <td colspan="7" style="text-align:right; padding-right:6em">bits 6-0
96
* </tr>
97
* <tr>
98
* <th scope="row" rowspan="2" style="text-align:left; font-weight:normal">
99
* {@code \u005Cu0000},<br>
100
* {@code \u005Cu0080} to {@code \u005Cu07FF} </th>
101
* <th scope="row" style="font-weight:normal; text-align:center"> 1 </th>
102
* <td style="text-align:center">1
103
* <td style="text-align:center">1
104
* <td style="text-align:center">0
105
* <td colspan="5" style="text-align:right; padding-right:6em">bits 10-6
106
* </tr>
107
* <tr>
108
* <!-- (value) -->
109
* <th scope="row" style="font-weight:normal; text-align:center"> 2 </th>
110
* <td style="text-align:center">1
111
* <td style="text-align:center">0
112
* <td colspan="6" style="text-align:right; padding-right:6em">bits 5-0
113
* </tr>
114
* <tr>
115
* <th scope="row" rowspan="3" style="text-align:left; font-weight:normal">
116
* {@code \u005Cu0800} to {@code \u005CuFFFF} </th>
117
* <th scope="row" style="font-weight:normal; text-align:center"> 1 </th>
118
* <td style="text-align:center">1
119
* <td style="text-align:center">1
120
* <td style="text-align:center">1
121
* <td style="text-align:center">0
122
* <td colspan="4" style="text-align:right; padding-right:6em">bits 15-12
123
* </tr>
124
* <tr>
125
* <!-- (value) -->
126
* <th scope="row" style="font-weight:normal; text-align:center"> 2 </th>
127
* <td style="text-align:center">1
128
* <td style="text-align:center">0
129
* <td colspan="6" style="text-align:right; padding-right:6em">bits 11-6
130
* </tr>
131
* <tr>
132
* <!-- (value) -->
133
* <th scope="row" style="font-weight:normal; text-align:center"> 3 </th>
134
* <td style="text-align:center">1
135
* <td style="text-align:center">0
136
* <td colspan="6" style="text-align:right; padding-right:6em">bits 5-0
137
* </tr>
138
* </tbody>
139
* </table>
140
*
141
* <p>
142
* The differences between this format and the
143
* standard UTF-8 format are the following:
144
* <ul>
145
* <li>The null byte {@code '\u005Cu0000'} is encoded in 2-byte format
146
* rather than 1-byte, so that the encoded strings never have
147
* embedded nulls.
148
* <li>Only the 1-byte, 2-byte, and 3-byte formats are used.
149
* <li><a href="../lang/Character.html#unicode">Supplementary characters</a>
150
* are represented in the form of surrogate pairs.
151
* </ul>
152
* @author Frank Yellin
153
* @see java.io.DataInputStream
154
* @see java.io.DataOutput
155
* @since 1.0
156
*/
157
public interface DataInput {
158
/**
159
* Reads some bytes from an input
160
* stream and stores them into the buffer
161
* array {@code b}. The number of bytes
162
* read is equal
163
* to the length of {@code b}.
164
* <p>
165
* This method blocks until one of the
166
* following conditions occurs:
167
* <ul>
168
* <li>{@code b.length}
169
* bytes of input data are available, in which
170
* case a normal return is made.
171
*
172
* <li>End of
173
* file is detected, in which case an {@code EOFException}
174
* is thrown.
175
*
176
* <li>An I/O error occurs, in
177
* which case an {@code IOException} other
178
* than {@code EOFException} is thrown.
179
* </ul>
180
* <p>
181
* If {@code b} is {@code null},
182
* a {@code NullPointerException} is thrown.
183
* If {@code b.length} is zero, then
184
* no bytes are read. Otherwise, the first
185
* byte read is stored into element {@code b[0]},
186
* the next one into {@code b[1]}, and
187
* so on.
188
* If an exception is thrown from
189
* this method, then it may be that some but
190
* not all bytes of {@code b} have been
191
* updated with data from the input stream.
192
*
193
* @param b the buffer into which the data is read.
194
* @throws NullPointerException if {@code b} is {@code null}.
195
* @throws EOFException if this stream reaches the end before reading
196
* all the bytes.
197
* @throws IOException if an I/O error occurs.
198
*/
199
void readFully(byte b[]) throws IOException;
200
201
/**
202
*
203
* Reads {@code len}
204
* bytes from
205
* an input stream.
206
* <p>
207
* This method
208
* blocks until one of the following conditions
209
* occurs:
210
* <ul>
211
* <li>{@code len} bytes
212
* of input data are available, in which case
213
* a normal return is made.
214
*
215
* <li>End of file
216
* is detected, in which case an {@code EOFException}
217
* is thrown.
218
*
219
* <li>An I/O error occurs, in
220
* which case an {@code IOException} other
221
* than {@code EOFException} is thrown.
222
* </ul>
223
* <p>
224
* If {@code b} is {@code null},
225
* a {@code NullPointerException} is thrown.
226
* If {@code off} is negative, or {@code len}
227
* is negative, or {@code off+len} is
228
* greater than the length of the array {@code b},
229
* then an {@code IndexOutOfBoundsException}
230
* is thrown.
231
* If {@code len} is zero,
232
* then no bytes are read. Otherwise, the first
233
* byte read is stored into element {@code b[off]},
234
* the next one into {@code b[off+1]},
235
* and so on. The number of bytes read is,
236
* at most, equal to {@code len}.
237
*
238
* @param b the buffer into which the data is read.
239
* @param off an int specifying the offset in the data array {@code b}.
240
* @param len an int specifying the number of bytes to read.
241
* @throws NullPointerException if {@code b} is {@code null}.
242
* @throws IndexOutOfBoundsException if {@code off} is negative,
243
* {@code len} is negative, or {@code len} is greater than
244
* {@code b.length - off}.
245
* @throws EOFException if this stream reaches the end before reading
246
* all the bytes.
247
* @throws IOException if an I/O error occurs.
248
*/
249
void readFully(byte b[], int off, int len) throws IOException;
250
251
/**
252
* Makes an attempt to skip over
253
* {@code n} bytes
254
* of data from the input
255
* stream, discarding the skipped bytes. However,
256
* it may skip
257
* over some smaller number of
258
* bytes, possibly zero. This may result from
259
* any of a
260
* number of conditions; reaching
261
* end of file before {@code n} bytes
262
* have been skipped is
263
* only one possibility.
264
* This method never throws an {@code EOFException}.
265
* The actual
266
* number of bytes skipped is returned.
267
*
268
* @param n the number of bytes to be skipped.
269
* @return the number of bytes actually skipped.
270
* @throws IOException if an I/O error occurs.
271
*/
272
int skipBytes(int n) throws IOException;
273
274
/**
275
* Reads one input byte and returns
276
* {@code true} if that byte is nonzero,
277
* {@code false} if that byte is zero.
278
* This method is suitable for reading
279
* the byte written by the {@code writeBoolean}
280
* method of interface {@code DataOutput}.
281
*
282
* @return the {@code boolean} value read.
283
* @throws EOFException if this stream reaches the end before reading
284
* all the bytes.
285
* @throws IOException if an I/O error occurs.
286
*/
287
boolean readBoolean() throws IOException;
288
289
/**
290
* Reads and returns one input byte.
291
* The byte is treated as a signed value in
292
* the range {@code -128} through {@code 127},
293
* inclusive.
294
* This method is suitable for
295
* reading the byte written by the {@code writeByte}
296
* method of interface {@code DataOutput}.
297
*
298
* @return the 8-bit value read.
299
* @throws EOFException if this stream reaches the end before reading
300
* all the bytes.
301
* @throws IOException if an I/O error occurs.
302
*/
303
byte readByte() throws IOException;
304
305
/**
306
* Reads one input byte, zero-extends
307
* it to type {@code int}, and returns
308
* the result, which is therefore in the range
309
* {@code 0}
310
* through {@code 255}.
311
* This method is suitable for reading
312
* the byte written by the {@code writeByte}
313
* method of interface {@code DataOutput}
314
* if the argument to {@code writeByte}
315
* was intended to be a value in the range
316
* {@code 0} through {@code 255}.
317
*
318
* @return the unsigned 8-bit value read.
319
* @throws EOFException if this stream reaches the end before reading
320
* all the bytes.
321
* @throws IOException if an I/O error occurs.
322
*/
323
int readUnsignedByte() throws IOException;
324
325
/**
326
* Reads two input bytes and returns
327
* a {@code short} value. Let {@code a}
328
* be the first byte read and {@code b}
329
* be the second byte. The value
330
* returned
331
* is:
332
* <pre>{@code (short)((a << 8) | (b & 0xff))
333
* }</pre>
334
* This method
335
* is suitable for reading the bytes written
336
* by the {@code writeShort} method of
337
* interface {@code DataOutput}.
338
*
339
* @return the 16-bit value read.
340
* @throws EOFException if this stream reaches the end before reading
341
* all the bytes.
342
* @throws IOException if an I/O error occurs.
343
*/
344
short readShort() throws IOException;
345
346
/**
347
* Reads two input bytes and returns
348
* an {@code int} value in the range {@code 0}
349
* through {@code 65535}. Let {@code a}
350
* be the first byte read and
351
* {@code b}
352
* be the second byte. The value returned is:
353
* <pre>{@code (((a & 0xff) << 8) | (b & 0xff))
354
* }</pre>
355
* This method is suitable for reading the bytes
356
* written by the {@code writeShort} method
357
* of interface {@code DataOutput} if
358
* the argument to {@code writeShort}
359
* was intended to be a value in the range
360
* {@code 0} through {@code 65535}.
361
*
362
* @return the unsigned 16-bit value read.
363
* @throws EOFException if this stream reaches the end before reading
364
* all the bytes.
365
* @throws IOException if an I/O error occurs.
366
*/
367
int readUnsignedShort() throws IOException;
368
369
/**
370
* Reads two input bytes and returns a {@code char} value.
371
* Let {@code a}
372
* be the first byte read and {@code b}
373
* be the second byte. The value
374
* returned is:
375
* <pre>{@code (char)((a << 8) | (b & 0xff))
376
* }</pre>
377
* This method
378
* is suitable for reading bytes written by
379
* the {@code writeChar} method of interface
380
* {@code DataOutput}.
381
*
382
* @return the {@code char} value read.
383
* @throws EOFException if this stream reaches the end before reading
384
* all the bytes.
385
* @throws IOException if an I/O error occurs.
386
*/
387
char readChar() throws IOException;
388
389
/**
390
* Reads four input bytes and returns an
391
* {@code int} value. Let {@code a-d}
392
* be the first through fourth bytes read. The value returned is:
393
* <pre>{@code
394
* (((a & 0xff) << 24) | ((b & 0xff) << 16) |
395
* ((c & 0xff) << 8) | (d & 0xff))
396
* }</pre>
397
* This method is suitable
398
* for reading bytes written by the {@code writeInt}
399
* method of interface {@code DataOutput}.
400
*
401
* @return the {@code int} value read.
402
* @throws EOFException if this stream reaches the end before reading
403
* all the bytes.
404
* @throws IOException if an I/O error occurs.
405
*/
406
int readInt() throws IOException;
407
408
/**
409
* Reads eight input bytes and returns
410
* a {@code long} value. Let {@code a-h}
411
* be the first through eighth bytes read.
412
* The value returned is:
413
* <pre>{@code
414
* (((long)(a & 0xff) << 56) |
415
* ((long)(b & 0xff) << 48) |
416
* ((long)(c & 0xff) << 40) |
417
* ((long)(d & 0xff) << 32) |
418
* ((long)(e & 0xff) << 24) |
419
* ((long)(f & 0xff) << 16) |
420
* ((long)(g & 0xff) << 8) |
421
* ((long)(h & 0xff)))
422
* }</pre>
423
* <p>
424
* This method is suitable
425
* for reading bytes written by the {@code writeLong}
426
* method of interface {@code DataOutput}.
427
*
428
* @return the {@code long} value read.
429
* @throws EOFException if this stream reaches the end before reading
430
* all the bytes.
431
* @throws IOException if an I/O error occurs.
432
*/
433
long readLong() throws IOException;
434
435
/**
436
* Reads four input bytes and returns
437
* a {@code float} value. It does this
438
* by first constructing an {@code int}
439
* value in exactly the manner
440
* of the {@code readInt}
441
* method, then converting this {@code int}
442
* value to a {@code float} in
443
* exactly the manner of the method {@code Float.intBitsToFloat}.
444
* This method is suitable for reading
445
* bytes written by the {@code writeFloat}
446
* method of interface {@code DataOutput}.
447
*
448
* @return the {@code float} value read.
449
* @throws EOFException if this stream reaches the end before reading
450
* all the bytes.
451
* @throws IOException if an I/O error occurs.
452
*/
453
float readFloat() throws IOException;
454
455
/**
456
* Reads eight input bytes and returns
457
* a {@code double} value. It does this
458
* by first constructing a {@code long}
459
* value in exactly the manner
460
* of the {@code readLong}
461
* method, then converting this {@code long}
462
* value to a {@code double} in exactly
463
* the manner of the method {@code Double.longBitsToDouble}.
464
* This method is suitable for reading
465
* bytes written by the {@code writeDouble}
466
* method of interface {@code DataOutput}.
467
*
468
* @return the {@code double} value read.
469
* @throws EOFException if this stream reaches the end before reading
470
* all the bytes.
471
* @throws IOException if an I/O error occurs.
472
*/
473
double readDouble() throws IOException;
474
475
/**
476
* Reads the next line of text from the input stream.
477
* It reads successive bytes, converting
478
* each byte separately into a character,
479
* until it encounters a line terminator or
480
* end of
481
* file; the characters read are then
482
* returned as a {@code String}. Note
483
* that because this
484
* method processes bytes,
485
* it does not support input of the full Unicode
486
* character set.
487
* <p>
488
* If end of file is encountered
489
* before even one byte can be read, then {@code null}
490
* is returned. Otherwise, each byte that is
491
* read is converted to type {@code char}
492
* by zero-extension. If the character {@code '\n'}
493
* is encountered, it is discarded and reading
494
* ceases. If the character {@code '\r'}
495
* is encountered, it is discarded and, if
496
* the following byte converts &#32;to the
497
* character {@code '\n'}, then that is
498
* discarded also; reading then ceases. If
499
* end of file is encountered before either
500
* of the characters {@code '\n'} and
501
* {@code '\r'} is encountered, reading
502
* ceases. Once reading has ceased, a {@code String}
503
* is returned that contains all the characters
504
* read and not discarded, taken in order.
505
* Note that every character in this string
506
* will have a value less than {@code \u005Cu0100},
507
* that is, {@code (char)256}.
508
*
509
* @return the next line of text from the input stream,
510
* or {@code null} if the end of file is
511
* encountered before a byte can be read.
512
* @throws IOException if an I/O error occurs.
513
*/
514
String readLine() throws IOException;
515
516
/**
517
* Reads in a string that has been encoded using a
518
* <a href="#modified-utf-8">modified UTF-8</a>
519
* format.
520
* The general contract of {@code readUTF}
521
* is that it reads a representation of a Unicode
522
* character string encoded in modified
523
* UTF-8 format; this string of characters
524
* is then returned as a {@code String}.
525
* <p>
526
* First, two bytes are read and used to
527
* construct an unsigned 16-bit integer in
528
* exactly the manner of the {@code readUnsignedShort}
529
* method . This integer value is called the
530
* <i>UTF length</i> and specifies the number
531
* of additional bytes to be read. These bytes
532
* are then converted to characters by considering
533
* them in groups. The length of each group
534
* is computed from the value of the first
535
* byte of the group. The byte following a
536
* group, if any, is the first byte of the
537
* next group.
538
* <p>
539
* If the first byte of a group
540
* matches the bit pattern {@code 0xxxxxxx}
541
* (where {@code x} means "may be {@code 0}
542
* or {@code 1}"), then the group consists
543
* of just that byte. The byte is zero-extended
544
* to form a character.
545
* <p>
546
* If the first byte
547
* of a group matches the bit pattern {@code 110xxxxx},
548
* then the group consists of that byte {@code a}
549
* and a second byte {@code b}. If there
550
* is no byte {@code b} (because byte
551
* {@code a} was the last of the bytes
552
* to be read), or if byte {@code b} does
553
* not match the bit pattern {@code 10xxxxxx},
554
* then a {@code UTFDataFormatException}
555
* is thrown. Otherwise, the group is converted
556
* to the character:
557
* <pre>{@code (char)(((a & 0x1F) << 6) | (b & 0x3F))
558
* }</pre>
559
* If the first byte of a group
560
* matches the bit pattern {@code 1110xxxx},
561
* then the group consists of that byte {@code a}
562
* and two more bytes {@code b} and {@code c}.
563
* If there is no byte {@code c} (because
564
* byte {@code a} was one of the last
565
* two of the bytes to be read), or either
566
* byte {@code b} or byte {@code c}
567
* does not match the bit pattern {@code 10xxxxxx},
568
* then a {@code UTFDataFormatException}
569
* is thrown. Otherwise, the group is converted
570
* to the character:
571
* <pre>{@code
572
* (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
573
* }</pre>
574
* If the first byte of a group matches the
575
* pattern {@code 1111xxxx} or the pattern
576
* {@code 10xxxxxx}, then a {@code UTFDataFormatException}
577
* is thrown.
578
* <p>
579
* If end of file is encountered
580
* at any time during this entire process,
581
* then an {@code EOFException} is thrown.
582
* <p>
583
* After every group has been converted to
584
* a character by this process, the characters
585
* are gathered, in the same order in which
586
* their corresponding groups were read from
587
* the input stream, to form a {@code String},
588
* which is returned.
589
* <p>
590
* The {@code writeUTF}
591
* method of interface {@code DataOutput}
592
* may be used to write data that is suitable
593
* for reading by this method.
594
* @return a Unicode string.
595
* @throws EOFException if this stream reaches the end
596
* before reading all the bytes.
597
* @throws IOException if an I/O error occurs.
598
* @throws UTFDataFormatException if the bytes do not represent a
599
* valid modified UTF-8 encoding of a string.
600
*/
601
String readUTF() throws IOException;
602
}
603
604