2 * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
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.
36 * DataInputStream is not necessarily safe for multithreaded access.
37 * Thread safety is optional and is the responsibility of users of
38 * methods in this class.
40 * @author Arthur van Hoff
41 * @see java.io.DataOutputStream
45 class DataInputStream extends FilterInputStream implements DataInput {
48 * Creates a DataInputStream that uses the specified
49 * underlying InputStream.
51 * @param in the specified input stream
53 public DataInputStream(InputStream in) {
58 * working arrays initialized on demand by readUTF
60 private byte bytearr[] = new byte[80];
61 private char chararr[] = new char[80];
64 * Reads some number of bytes from the contained input stream and
65 * stores them into the buffer array <code>b</code>. 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.
70 * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
71 * thrown. If the length of <code>b</code> is zero, then no bytes are
72 * read and <code>0</code> 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</code> is returned;
75 * otherwise, at least one byte is read and stored into <code>b</code>.
77 * <p>The first byte read is stored into element <code>b[0]</code>, the
78 * next one into <code>b[1]</code>, and so on. The number of bytes read
79 * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
80 * be the number of bytes actually read; these bytes will be stored in
81 * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
82 * elements <code>b[k]</code> through <code>b[b.length-1]</code>
85 * <p>The <code>read(b)</code> method has the same effect as:
87 * read(b, 0, b.length)
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</code> if there is no more data because the end
93 * of the stream has been reached.
94 * @exception 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
98 * @see java.io.FilterInputStream#in
99 * @see java.io.InputStream#read(byte[], int, int)
101 public final int read(byte b[]) throws IOException {
102 return in.read(b, 0, b.length);
106 * Reads up to <code>len</code> 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</code> bytes, but a smaller number may be read,
109 * possibly zero. The number of bytes actually read is returned as an
112 * <p> This method blocks until input data is available, end of file is
113 * detected, or an exception is thrown.
115 * <p> If <code>len</code> is zero, then no bytes are read and
116 * <code>0</code> 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</code> is returned; otherwise, at least one
119 * byte is read and stored into <code>b</code>.
121 * <p> The first byte read is stored into element <code>b[off]</code>, the
122 * next one into <code>b[off+1]</code>, and so on. The number of bytes read
123 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
124 * bytes actually read; these bytes will be stored in elements
125 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
126 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
127 * <code>b[off+len-1]</code> unaffected.
129 * <p> In every case, elements <code>b[0]</code> through
130 * <code>b[off]</code> and elements <code>b[off+len]</code> through
131 * <code>b[b.length-1]</code> are unaffected.
133 * @param b the buffer into which the data is read.
134 * @param off the start offset in the destination array <code>b</code>
135 * @param len the maximum number of bytes read.
136 * @return the total number of bytes read into the buffer, or
137 * <code>-1</code> if there is no more data because the end
138 * of the stream has been reached.
139 * @exception NullPointerException If <code>b</code> is <code>null</code>.
140 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
141 * <code>len</code> is negative, or <code>len</code> is greater than
142 * <code>b.length - off</code>
143 * @exception 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
147 * @see java.io.FilterInputStream#in
148 * @see java.io.InputStream#read(byte[], int, int)
150 public final int read(byte b[], int off, int len) throws IOException {
151 return in.read(b, off, len);
155 * See the general contract of the <code>readFully</code>
156 * method of <code>DataInput</code>.
159 * for this operation are read from the contained
162 * @param b the buffer into which the data is read.
163 * @exception EOFException if this input stream reaches the end before
164 * reading all the bytes.
165 * @exception IOException the stream has been closed and the contained
166 * input stream does not support reading after close, or
167 * another I/O error occurs.
168 * @see java.io.FilterInputStream#in
170 public final void readFully(byte b[]) throws IOException {
171 readFully(b, 0, b.length);
175 * See the general contract of the <code>readFully</code>
176 * method of <code>DataInput</code>.
179 * for this operation are read from the contained
182 * @param b the buffer into which the data is read.
183 * @param off the start offset of the data.
184 * @param len the number of bytes to read.
185 * @exception EOFException if this input stream reaches the end before
186 * reading all the bytes.
187 * @exception IOException the stream has been closed and the contained
188 * input stream does not support reading after close, or
189 * another I/O error occurs.
190 * @see java.io.FilterInputStream#in
192 public final void readFully(byte b[], int off, int len) throws IOException {
194 throw new IndexOutOfBoundsException();
197 int count = in.read(b, off + n, len - n);
199 throw new EOFException();
205 * See the general contract of the <code>skipBytes</code>
206 * method of <code>DataInput</code>.
208 * Bytes for this operation are read from the contained
211 * @param n the number of bytes to be skipped.
212 * @return the actual number of bytes skipped.
213 * @exception IOException if the contained input stream does not support
214 * seek, or the stream has been closed and
215 * the contained input stream does not support
216 * reading after close, or another I/O error occurs.
218 public final int skipBytes(int n) throws IOException {
222 while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
230 * See the general contract of the <code>readBoolean</code>
231 * method of <code>DataInput</code>.
233 * Bytes for this operation are read from the contained
236 * @return the <code>boolean</code> value read.
237 * @exception EOFException if this input stream has reached the end.
238 * @exception IOException the stream has been closed and the contained
239 * input stream does not support reading after close, or
240 * another I/O error occurs.
241 * @see java.io.FilterInputStream#in
243 public final boolean readBoolean() throws IOException {
246 throw new EOFException();
251 * See the general contract of the <code>readByte</code>
252 * method of <code>DataInput</code>.
255 * for this operation are read from the contained
258 * @return the next byte of this input stream as a signed 8-bit
260 * @exception EOFException if this input stream has reached the end.
261 * @exception IOException the stream has been closed and the contained
262 * input stream does not support reading after close, or
263 * another I/O error occurs.
264 * @see java.io.FilterInputStream#in
266 public final byte readByte() throws IOException {
269 throw new EOFException();
274 * See the general contract of the <code>readUnsignedByte</code>
275 * method of <code>DataInput</code>.
278 * for this operation are read from the contained
281 * @return the next byte of this input stream, interpreted as an
282 * unsigned 8-bit number.
283 * @exception EOFException if this input stream has reached the end.
284 * @exception IOException the stream has been closed and the contained
285 * input stream does not support reading after close, or
286 * another I/O error occurs.
287 * @see java.io.FilterInputStream#in
289 public final int readUnsignedByte() throws IOException {
292 throw new EOFException();
297 * See the general contract of the <code>readShort</code>
298 * method of <code>DataInput</code>.
301 * for this operation are read from the contained
304 * @return the next two bytes of this input stream, interpreted as a
305 * signed 16-bit number.
306 * @exception EOFException if this input stream reaches the end before
308 * @exception IOException the stream has been closed and the contained
309 * input stream does not support reading after close, or
310 * another I/O error occurs.
311 * @see java.io.FilterInputStream#in
313 public final short readShort() throws IOException {
317 throw new EOFException();
318 return (short)((ch1 << 8) + (ch2 << 0));
322 * See the general contract of the <code>readUnsignedShort</code>
323 * method of <code>DataInput</code>.
326 * for this operation are read from the contained
329 * @return the next two bytes of this input stream, interpreted as an
330 * unsigned 16-bit integer.
331 * @exception EOFException if this input stream reaches the end before
333 * @exception IOException the stream has been closed and the contained
334 * input stream does not support reading after close, or
335 * another I/O error occurs.
336 * @see java.io.FilterInputStream#in
338 public final int readUnsignedShort() throws IOException {
342 throw new EOFException();
343 return (ch1 << 8) + (ch2 << 0);
347 * See the general contract of the <code>readChar</code>
348 * method of <code>DataInput</code>.
351 * for this operation are read from the contained
354 * @return the next two bytes of this input stream, interpreted as a
356 * @exception EOFException if this input stream reaches the end before
358 * @exception IOException the stream has been closed and the contained
359 * input stream does not support reading after close, or
360 * another I/O error occurs.
361 * @see java.io.FilterInputStream#in
363 public final char readChar() throws IOException {
367 throw new EOFException();
368 return (char)((ch1 << 8) + (ch2 << 0));
372 * See the general contract of the <code>readInt</code>
373 * method of <code>DataInput</code>.
376 * for this operation are read from the contained
379 * @return the next four bytes of this input stream, interpreted as an
381 * @exception EOFException if this input stream reaches the end before
382 * reading four bytes.
383 * @exception IOException the stream has been closed and the contained
384 * input stream does not support reading after close, or
385 * another I/O error occurs.
386 * @see java.io.FilterInputStream#in
388 public final int readInt() throws IOException {
393 if ((ch1 | ch2 | ch3 | ch4) < 0)
394 throw new EOFException();
395 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
398 private byte readBuffer[] = new byte[8];
401 * See the general contract of the <code>readLong</code>
402 * method of <code>DataInput</code>.
405 * for this operation are read from the contained
408 * @return the next eight bytes of this input stream, interpreted as a
410 * @exception EOFException if this input stream reaches the end before
411 * reading eight bytes.
412 * @exception IOException the stream has been closed and the contained
413 * input stream does not support reading after close, or
414 * another I/O error occurs.
415 * @see java.io.FilterInputStream#in
417 public final long readLong() throws IOException {
418 readFully(readBuffer, 0, 8);
419 return (((long)readBuffer[0] << 56) +
420 ((long)(readBuffer[1] & 255) << 48) +
421 ((long)(readBuffer[2] & 255) << 40) +
422 ((long)(readBuffer[3] & 255) << 32) +
423 ((long)(readBuffer[4] & 255) << 24) +
424 ((readBuffer[5] & 255) << 16) +
425 ((readBuffer[6] & 255) << 8) +
426 ((readBuffer[7] & 255) << 0));
430 * See the general contract of the <code>readFloat</code>
431 * method of <code>DataInput</code>.
434 * for this operation are read from the contained
437 * @return the next four bytes of this input stream, interpreted as a
438 * <code>float</code>.
439 * @exception EOFException if this input stream reaches the end before
440 * reading four bytes.
441 * @exception IOException the stream has been closed and the contained
442 * input stream does not support reading after close, or
443 * another I/O error occurs.
444 * @see java.io.DataInputStream#readInt()
445 * @see java.lang.Float#intBitsToFloat(int)
447 public final float readFloat() throws IOException {
448 return Float.intBitsToFloat(readInt());
452 * See the general contract of the <code>readDouble</code>
453 * method of <code>DataInput</code>.
456 * for this operation are read from the contained
459 * @return the next eight bytes of this input stream, interpreted as a
460 * <code>double</code>.
461 * @exception EOFException if this input stream reaches the end before
462 * reading eight bytes.
463 * @exception IOException the stream has been closed and the contained
464 * input stream does not support reading after close, or
465 * another I/O error occurs.
466 * @see java.io.DataInputStream#readLong()
467 * @see java.lang.Double#longBitsToDouble(long)
469 public final double readDouble() throws IOException {
472 return toDouble(hi, low);
475 @JavaScriptBody(args={ "hi", "low" },
478 + " if (hi === 0x7ff00000) return Number.POSITIVE_INFINITY;\n"
479 + " if (hi === 0xfff00000) return Number.NEGATIVE_INFINITY;\n"
481 + "if (hi >= 0x7ff00000 && hi <= 0x7fffffff) return Number.NaN;\n"
482 + "if (hi >= 0xfff00000 && hi <= 0xffffffff) return Number.NaN;\n"
483 + "var s = (hi & 0x80000000) === 0 ? 1 : -1;\n"
484 + "var e = (hi >> 20) & 0x7ff;\n"
485 + "var to32 = low >> 0;\n"
487 + " if (to32 & 0x80000000) {\n"
488 + " hi = hi << 1 + 1; low = low << 1;\n"
490 + " hi = hi << 1; low = low << 1;\n"
493 + " hi = (hi & 0xfffff) | 0x100000;\n"
495 + "to32 = low >> 0;\n"
496 + "var m = Math.pow(2.0, 32) * hi + to32;\n"
497 + "var r = s * m * Math.pow(2.0, e - 1075);\n"
498 + "//throw 'exp: ' + e + ' sign: ' + s + ' hi:' + hi + ' low: ' + low + ' m: ' + m + ' r: ' + r;\n"
501 private static double toDouble(int hi, int low) {
503 both = (both << 32) & low;
504 return Double.doubleToLongBits(both);
507 private char lineBuffer[];
510 * See the general contract of the <code>readLine</code>
511 * method of <code>DataInput</code>.
514 * for this operation are read from the contained
517 * @deprecated This method does not properly convert bytes to characters.
518 * As of JDK 1.1, the preferred way to read lines of text is via the
519 * <code>BufferedReader.readLine()</code> method. Programs that use the
520 * <code>DataInputStream</code> class to read lines can be converted to use
521 * the <code>BufferedReader</code> class by replacing code of the form:
523 * DataInputStream d = new DataInputStream(in);
524 * </pre></blockquote>
528 * = new BufferedReader(new InputStreamReader(in));
529 * </pre></blockquote>
531 * @return the next line of text from this input stream.
532 * @exception IOException if an I/O error occurs.
533 * @see java.io.BufferedReader#readLine()
534 * @see java.io.FilterInputStream#in
537 public final String readLine() throws IOException {
538 char buf[] = lineBuffer;
541 buf = lineBuffer = new char[128];
544 int room = buf.length;
549 switch (c = in.read()) {
556 if ((c2 != '\n') && (c2 != -1)) {
557 if (!(in instanceof PushbackInputStream)) {
558 this.in = new PushbackInputStream(in);
560 ((PushbackInputStream)in).unread(c2);
566 buf = new char[offset + 128];
567 room = buf.length - offset - 1;
568 arraycopy(lineBuffer, 0, buf, 0, offset);
571 buf[offset++] = (char) c;
575 if ((c == -1) && (offset == 0)) {
578 return String.copyValueOf(buf, 0, offset);
582 * See the general contract of the <code>readUTF</code>
583 * method of <code>DataInput</code>.
586 * for this operation are read from the contained
589 * @return a Unicode string.
590 * @exception EOFException if this input stream reaches the end before
591 * reading all the bytes.
592 * @exception IOException the stream has been closed and the contained
593 * input stream does not support reading after close, or
594 * another I/O error occurs.
595 * @exception UTFDataFormatException if the bytes do not represent a valid
596 * modified UTF-8 encoding of a string.
597 * @see java.io.DataInputStream#readUTF(java.io.DataInput)
599 public final String readUTF() throws IOException {
600 return readUTF(this);
605 * stream <code>in</code> a representation
606 * of a Unicode character string encoded in
607 * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
608 * this string of characters is then returned as a <code>String</code>.
609 * The details of the modified UTF-8 representation
610 * are exactly the same as for the <code>readUTF</code>
611 * method of <code>DataInput</code>.
613 * @param in a data input stream.
614 * @return a Unicode string.
615 * @exception EOFException if the input stream reaches the end
616 * before all the bytes.
617 * @exception IOException the stream has been closed and the contained
618 * input stream does not support reading after close, or
619 * another I/O error occurs.
620 * @exception UTFDataFormatException if the bytes do not represent a
621 * valid modified UTF-8 encoding of a Unicode string.
622 * @see java.io.DataInputStream#readUnsignedShort()
624 public final static String readUTF(DataInput in) throws IOException {
625 int utflen = in.readUnsignedShort();
626 byte[] bytearr = null;
627 char[] chararr = null;
628 if (in instanceof DataInputStream) {
629 DataInputStream dis = (DataInputStream)in;
630 if (dis.bytearr.length < utflen){
631 dis.bytearr = new byte[utflen*2];
632 dis.chararr = new char[utflen*2];
634 chararr = dis.chararr;
635 bytearr = dis.bytearr;
637 bytearr = new byte[utflen];
638 chararr = new char[utflen];
645 in.readFully(bytearr, 0, utflen);
647 while (count < utflen) {
648 c = (int) bytearr[count] & 0xff;
651 chararr[chararr_count++]=(char)c;
654 while (count < utflen) {
655 c = (int) bytearr[count] & 0xff;
657 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
660 chararr[chararr_count++]=(char)c;
663 /* 110x xxxx 10xx xxxx*/
666 throw new UTFDataFormatException(
667 "malformed input: partial character at end");
668 char2 = (int) bytearr[count-1];
669 if ((char2 & 0xC0) != 0x80)
670 throw new UTFDataFormatException(
671 "malformed input around byte " + count);
672 chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
676 /* 1110 xxxx 10xx xxxx 10xx xxxx */
679 throw new UTFDataFormatException(
680 "malformed input: partial character at end");
681 char2 = (int) bytearr[count-2];
682 char3 = (int) bytearr[count-1];
683 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
684 throw new UTFDataFormatException(
685 "malformed input around byte " + (count-1));
686 chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
687 ((char2 & 0x3F) << 6) |
688 ((char3 & 0x3F) << 0));
691 /* 10xx xxxx, 1111 xxxx */
692 throw new UTFDataFormatException(
693 "malformed input around byte " + count);
696 // The number of chars produced may be less than utflen
697 return new String(chararr, 0, chararr_count);
699 static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
700 while (count-- > 0) {
701 dst[dstBegin++] = value[srcBegin++];