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;
29 import org.apidesign.bck2brwsr.emul.lang.System;
32 * A data input stream lets an application read primitive Java data
33 * types from an underlying input stream in a machine-independent
34 * way. An application uses a data output stream to write data that
35 * can later be read by a data input stream.
37 * DataInputStream is not necessarily safe for multithreaded access.
38 * Thread safety is optional and is the responsibility of users of
39 * methods in this class.
41 * @author Arthur van Hoff
42 * @see java.io.DataOutputStream
46 class DataInputStream extends FilterInputStream implements DataInput {
49 * Creates a DataInputStream that uses the specified
50 * underlying InputStream.
52 * @param in the specified input stream
54 public DataInputStream(InputStream in) {
59 * working arrays initialized on demand by readUTF
61 private byte bytearr[] = new byte[80];
62 private char chararr[] = new char[80];
65 * Reads some number of bytes from the contained input stream and
66 * stores them into the buffer array <code>b</code>. The number of
67 * bytes actually read is returned as an integer. This method blocks
68 * until input data is available, end of file is detected, or an
69 * exception is thrown.
71 * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
72 * thrown. If the length of <code>b</code> is zero, then no bytes are
73 * read and <code>0</code> is returned; otherwise, there is an attempt
74 * to read at least one byte. If no byte is available because the
75 * stream is at end of file, the value <code>-1</code> is returned;
76 * otherwise, at least one byte is read and stored into <code>b</code>.
78 * <p>The first byte read is stored into element <code>b[0]</code>, the
79 * next one into <code>b[1]</code>, and so on. The number of bytes read
80 * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
81 * be the number of bytes actually read; these bytes will be stored in
82 * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
83 * elements <code>b[k]</code> through <code>b[b.length-1]</code>
86 * <p>The <code>read(b)</code> method has the same effect as:
88 * read(b, 0, b.length)
91 * @param b the buffer into which the data is read.
92 * @return the total number of bytes read into the buffer, or
93 * <code>-1</code> if there is no more data because the end
94 * of the stream has been reached.
95 * @exception IOException if the first byte cannot be read for any reason
96 * other than end of file, the stream has been closed and the underlying
97 * input stream does not support reading after close, or another I/O
99 * @see java.io.FilterInputStream#in
100 * @see java.io.InputStream#read(byte[], int, int)
102 public final int read(byte b[]) throws IOException {
103 return in.read(b, 0, b.length);
107 * Reads up to <code>len</code> bytes of data from the contained
108 * input stream into an array of bytes. An attempt is made to read
109 * as many as <code>len</code> bytes, but a smaller number may be read,
110 * possibly zero. The number of bytes actually read is returned as an
113 * <p> This method blocks until input data is available, end of file is
114 * detected, or an exception is thrown.
116 * <p> If <code>len</code> is zero, then no bytes are read and
117 * <code>0</code> is returned; otherwise, there is an attempt to read at
118 * least one byte. If no byte is available because the stream is at end of
119 * file, the value <code>-1</code> is returned; otherwise, at least one
120 * byte is read and stored into <code>b</code>.
122 * <p> The first byte read is stored into element <code>b[off]</code>, the
123 * next one into <code>b[off+1]</code>, and so on. The number of bytes read
124 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
125 * bytes actually read; these bytes will be stored in elements
126 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
127 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
128 * <code>b[off+len-1]</code> unaffected.
130 * <p> In every case, elements <code>b[0]</code> through
131 * <code>b[off]</code> and elements <code>b[off+len]</code> through
132 * <code>b[b.length-1]</code> are unaffected.
134 * @param b the buffer into which the data is read.
135 * @param off the start offset in the destination array <code>b</code>
136 * @param len the maximum number of bytes read.
137 * @return the total number of bytes read into the buffer, or
138 * <code>-1</code> if there is no more data because the end
139 * of the stream has been reached.
140 * @exception NullPointerException If <code>b</code> is <code>null</code>.
141 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
142 * <code>len</code> is negative, or <code>len</code> is greater than
143 * <code>b.length - off</code>
144 * @exception IOException if the first byte cannot be read for any reason
145 * other than end of file, the stream has been closed and the underlying
146 * input stream does not support reading after close, or another I/O
148 * @see java.io.FilterInputStream#in
149 * @see java.io.InputStream#read(byte[], int, int)
151 public final int read(byte b[], int off, int len) throws IOException {
152 return in.read(b, off, len);
156 * See the general contract of the <code>readFully</code>
157 * method of <code>DataInput</code>.
160 * for this operation are read from the contained
163 * @param b the buffer into which the data is read.
164 * @exception EOFException if this input stream reaches the end before
165 * reading all the bytes.
166 * @exception 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
171 public final void readFully(byte b[]) throws IOException {
172 readFully(b, 0, b.length);
176 * See the general contract of the <code>readFully</code>
177 * method of <code>DataInput</code>.
180 * for this operation are read from the contained
183 * @param b the buffer into which the data is read.
184 * @param off the start offset of the data.
185 * @param len the number of bytes to read.
186 * @exception EOFException if this input stream reaches the end before
187 * reading all the bytes.
188 * @exception IOException the stream has been closed and the contained
189 * input stream does not support reading after close, or
190 * another I/O error occurs.
191 * @see java.io.FilterInputStream#in
193 public final void readFully(byte b[], int off, int len) throws IOException {
195 throw new IndexOutOfBoundsException();
198 int count = in.read(b, off + n, len - n);
200 throw new EOFException();
206 * See the general contract of the <code>skipBytes</code>
207 * method of <code>DataInput</code>.
209 * Bytes for this operation are read from the contained
212 * @param n the number of bytes to be skipped.
213 * @return the actual number of bytes skipped.
214 * @exception IOException if the contained input stream does not support
215 * seek, or the stream has been closed and
216 * the contained input stream does not support
217 * reading after close, or another I/O error occurs.
219 public final int skipBytes(int n) throws IOException {
223 while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
231 * See the general contract of the <code>readBoolean</code>
232 * method of <code>DataInput</code>.
234 * Bytes for this operation are read from the contained
237 * @return the <code>boolean</code> value read.
238 * @exception EOFException if this input stream has reached the end.
239 * @exception IOException the stream has been closed and the contained
240 * input stream does not support reading after close, or
241 * another I/O error occurs.
242 * @see java.io.FilterInputStream#in
244 public final boolean readBoolean() throws IOException {
247 throw new EOFException();
252 * See the general contract of the <code>readByte</code>
253 * method of <code>DataInput</code>.
256 * for this operation are read from the contained
259 * @return the next byte of this input stream as a signed 8-bit
261 * @exception EOFException if this input stream has reached the end.
262 * @exception IOException the stream has been closed and the contained
263 * input stream does not support reading after close, or
264 * another I/O error occurs.
265 * @see java.io.FilterInputStream#in
267 public final byte readByte() throws IOException {
270 throw new EOFException();
275 * See the general contract of the <code>readUnsignedByte</code>
276 * method of <code>DataInput</code>.
279 * for this operation are read from the contained
282 * @return the next byte of this input stream, interpreted as an
283 * unsigned 8-bit number.
284 * @exception EOFException if this input stream has reached the end.
285 * @exception IOException the stream has been closed and the contained
286 * input stream does not support reading after close, or
287 * another I/O error occurs.
288 * @see java.io.FilterInputStream#in
290 public final int readUnsignedByte() throws IOException {
293 throw new EOFException();
298 * See the general contract of the <code>readShort</code>
299 * method of <code>DataInput</code>.
302 * for this operation are read from the contained
305 * @return the next two bytes of this input stream, interpreted as a
306 * signed 16-bit number.
307 * @exception EOFException if this input stream reaches the end before
309 * @exception IOException the stream has been closed and the contained
310 * input stream does not support reading after close, or
311 * another I/O error occurs.
312 * @see java.io.FilterInputStream#in
314 public final short readShort() throws IOException {
318 throw new EOFException();
319 return (short)((ch1 << 8) + (ch2 << 0));
323 * See the general contract of the <code>readUnsignedShort</code>
324 * method of <code>DataInput</code>.
327 * for this operation are read from the contained
330 * @return the next two bytes of this input stream, interpreted as an
331 * unsigned 16-bit integer.
332 * @exception EOFException if this input stream reaches the end before
334 * @exception IOException the stream has been closed and the contained
335 * input stream does not support reading after close, or
336 * another I/O error occurs.
337 * @see java.io.FilterInputStream#in
339 public final int readUnsignedShort() throws IOException {
343 throw new EOFException();
344 return (ch1 << 8) + (ch2 << 0);
348 * See the general contract of the <code>readChar</code>
349 * method of <code>DataInput</code>.
352 * for this operation are read from the contained
355 * @return the next two bytes of this input stream, interpreted as a
357 * @exception EOFException if this input stream reaches the end before
359 * @exception IOException the stream has been closed and the contained
360 * input stream does not support reading after close, or
361 * another I/O error occurs.
362 * @see java.io.FilterInputStream#in
364 public final char readChar() throws IOException {
368 throw new EOFException();
369 return (char)((ch1 << 8) + (ch2 << 0));
373 * See the general contract of the <code>readInt</code>
374 * method of <code>DataInput</code>.
377 * for this operation are read from the contained
380 * @return the next four bytes of this input stream, interpreted as an
382 * @exception EOFException if this input stream reaches the end before
383 * reading four bytes.
384 * @exception IOException the stream has been closed and the contained
385 * input stream does not support reading after close, or
386 * another I/O error occurs.
387 * @see java.io.FilterInputStream#in
389 public final int readInt() throws IOException {
394 if ((ch1 | ch2 | ch3 | ch4) < 0)
395 throw new EOFException();
396 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
399 private byte readBuffer[] = new byte[8];
402 * See the general contract of the <code>readLong</code>
403 * method of <code>DataInput</code>.
406 * for this operation are read from the contained
409 * @return the next eight bytes of this input stream, interpreted as a
411 * @exception EOFException if this input stream reaches the end before
412 * reading eight bytes.
413 * @exception IOException the stream has been closed and the contained
414 * input stream does not support reading after close, or
415 * another I/O error occurs.
416 * @see java.io.FilterInputStream#in
418 public final long readLong() throws IOException {
419 readFully(readBuffer, 0, 8);
420 return (((long)readBuffer[0] << 56) +
421 ((long)(readBuffer[1] & 255) << 48) +
422 ((long)(readBuffer[2] & 255) << 40) +
423 ((long)(readBuffer[3] & 255) << 32) +
424 ((long)(readBuffer[4] & 255) << 24) +
425 ((readBuffer[5] & 255) << 16) +
426 ((readBuffer[6] & 255) << 8) +
427 ((readBuffer[7] & 255) << 0));
431 * See the general contract of the <code>readFloat</code>
432 * method of <code>DataInput</code>.
435 * for this operation are read from the contained
438 * @return the next four bytes of this input stream, interpreted as a
439 * <code>float</code>.
440 * @exception EOFException if this input stream reaches the end before
441 * reading four bytes.
442 * @exception IOException the stream has been closed and the contained
443 * input stream does not support reading after close, or
444 * another I/O error occurs.
445 * @see java.io.DataInputStream#readInt()
446 * @see java.lang.Float#intBitsToFloat(int)
448 public final float readFloat() throws IOException {
449 return Float.intBitsToFloat(readInt());
453 * See the general contract of the <code>readDouble</code>
454 * method of <code>DataInput</code>.
457 * for this operation are read from the contained
460 * @return the next eight bytes of this input stream, interpreted as a
461 * <code>double</code>.
462 * @exception EOFException if this input stream reaches the end before
463 * reading eight bytes.
464 * @exception IOException the stream has been closed and the contained
465 * input stream does not support reading after close, or
466 * another I/O error occurs.
467 * @see java.io.DataInputStream#readLong()
468 * @see java.lang.Double#longBitsToDouble(long)
470 public final double readDouble() throws IOException {
473 return toDouble(hi, low);
476 @JavaScriptBody(args={ "hi", "low" },
479 + " if (hi === 0x7ff00000) return Number.POSITIVE_INFINITY;\n"
480 + " if (hi === 0xfff00000) return Number.NEGATIVE_INFINITY;\n"
482 + "if (hi >= 0x7ff00000 && hi <= 0x7fffffff) return Number.NaN;\n"
483 + "if (hi >= 0xfff00000 && hi <= 0xffffffff) return Number.NaN;\n"
484 + "var s = (hi & 0x80000000) === 0 ? 1 : -1;\n"
485 + "var e = (hi >> 20) & 0x7ff;\n"
486 + "var to32 = low >> 0;\n"
488 + " if (to32 & 0x80000000) {\n"
489 + " hi = hi << 1 + 1; low = low << 1;\n"
491 + " hi = hi << 1; low = low << 1;\n"
494 + " hi = (hi & 0xfffff) | 0x100000;\n"
496 + "to32 = low >> 0;\n"
497 + "var m = Math.pow(2.0, 32) * hi + to32;\n"
498 + "var r = s * m * Math.pow(2.0, e - 1075);\n"
499 + "//throw 'exp: ' + e + ' sign: ' + s + ' hi:' + hi + ' low: ' + low + ' m: ' + m + ' r: ' + r;\n"
502 private static double toDouble(int hi, int low) {
504 both = (both << 32) & low;
505 return Double.doubleToLongBits(both);
508 private char lineBuffer[];
511 * See the general contract of the <code>readLine</code>
512 * method of <code>DataInput</code>.
515 * for this operation are read from the contained
518 * @deprecated This method does not properly convert bytes to characters.
519 * As of JDK 1.1, the preferred way to read lines of text is via the
520 * <code>BufferedReader.readLine()</code> method. Programs that use the
521 * <code>DataInputStream</code> class to read lines can be converted to use
522 * the <code>BufferedReader</code> class by replacing code of the form:
524 * DataInputStream d = new DataInputStream(in);
525 * </pre></blockquote>
529 * = new BufferedReader(new InputStreamReader(in));
530 * </pre></blockquote>
532 * @return the next line of text from this input stream.
533 * @exception IOException if an I/O error occurs.
534 * @see java.io.BufferedReader#readLine()
535 * @see java.io.FilterInputStream#in
538 public final String readLine() throws IOException {
539 char buf[] = lineBuffer;
542 buf = lineBuffer = new char[128];
545 int room = buf.length;
550 switch (c = in.read()) {
557 if ((c2 != '\n') && (c2 != -1)) {
558 if (!(in instanceof PushbackInputStream)) {
559 this.in = new PushbackInputStream(in);
561 ((PushbackInputStream)in).unread(c2);
567 buf = new char[offset + 128];
568 room = buf.length - offset - 1;
569 System.arraycopy(lineBuffer, 0, buf, 0, offset);
572 buf[offset++] = (char) c;
576 if ((c == -1) && (offset == 0)) {
579 return String.copyValueOf(buf, 0, offset);
583 * See the general contract of the <code>readUTF</code>
584 * method of <code>DataInput</code>.
587 * for this operation are read from the contained
590 * @return a Unicode string.
591 * @exception EOFException if this input stream reaches the end before
592 * reading all the bytes.
593 * @exception IOException the stream has been closed and the contained
594 * input stream does not support reading after close, or
595 * another I/O error occurs.
596 * @exception UTFDataFormatException if the bytes do not represent a valid
597 * modified UTF-8 encoding of a string.
598 * @see java.io.DataInputStream#readUTF(java.io.DataInput)
600 public final String readUTF() throws IOException {
601 return readUTF(this);
606 * stream <code>in</code> a representation
607 * of a Unicode character string encoded in
608 * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
609 * this string of characters is then returned as a <code>String</code>.
610 * The details of the modified UTF-8 representation
611 * are exactly the same as for the <code>readUTF</code>
612 * method of <code>DataInput</code>.
614 * @param in a data input stream.
615 * @return a Unicode string.
616 * @exception EOFException if the input stream reaches the end
617 * before all the bytes.
618 * @exception IOException the stream has been closed and the contained
619 * input stream does not support reading after close, or
620 * another I/O error occurs.
621 * @exception UTFDataFormatException if the bytes do not represent a
622 * valid modified UTF-8 encoding of a Unicode string.
623 * @see java.io.DataInputStream#readUnsignedShort()
625 public final static String readUTF(DataInput in) throws IOException {
626 int utflen = in.readUnsignedShort();
627 byte[] bytearr = null;
628 char[] chararr = null;
629 if (in instanceof DataInputStream) {
630 DataInputStream dis = (DataInputStream)in;
631 if (dis.bytearr.length < utflen){
632 dis.bytearr = new byte[utflen*2];
633 dis.chararr = new char[utflen*2];
635 chararr = dis.chararr;
636 bytearr = dis.bytearr;
638 bytearr = new byte[utflen];
639 chararr = new char[utflen];
646 in.readFully(bytearr, 0, utflen);
648 while (count < utflen) {
649 c = (int) bytearr[count] & 0xff;
652 chararr[chararr_count++]=(char)c;
655 while (count < utflen) {
656 c = (int) bytearr[count] & 0xff;
658 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
661 chararr[chararr_count++]=(char)c;
664 /* 110x xxxx 10xx xxxx*/
667 throw new UTFDataFormatException(
668 "malformed input: partial character at end");
669 char2 = (int) bytearr[count-1];
670 if ((char2 & 0xC0) != 0x80)
671 throw new UTFDataFormatException(
672 "malformed input around byte " + count);
673 chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
677 /* 1110 xxxx 10xx xxxx 10xx xxxx */
680 throw new UTFDataFormatException(
681 "malformed input: partial character at end");
682 char2 = (int) bytearr[count-2];
683 char3 = (int) bytearr[count-1];
684 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
685 throw new UTFDataFormatException(
686 "malformed input around byte " + (count-1));
687 chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
688 ((char2 & 0x3F) << 6) |
689 ((char3 & 0x3F) << 0));
692 /* 10xx xxxx, 1111 xxxx */
693 throw new UTFDataFormatException(
694 "malformed input around byte " + count);
697 // The number of chars produced may be less than utflen
698 return new String(chararr, 0, chararr_count);