1 /* DataInputStream.java -- FilteredInputStream that implements DataInput
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
29 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
30 * "The Java Language Specification", ISBN 0-201-63451-1
31 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
32 * Status: Believed complete and correct.
36 * This subclass of <code>FilteredInputStream</code> implements the
37 * <code>DataInput</code> interface that provides method for reading primitive
38 * Java data types from a stream.
44 * @author Warren Levy <warrenl@cygnus.com>
45 * @author Aaron M. Renn (arenn@urbanophile.com)
46 * @date October 20, 1998.
48 public class DataInputStream extends FilterInputStream implements DataInput
50 // readLine() hack to ensure that an '\r' not followed by an '\n' is
51 // handled correctly. If set, readLine() will ignore the first char it sees
52 // if that char is a '\n'
53 boolean ignoreInitialNewline = false;
56 * This constructor initializes a new <code>DataInputStream</code>
57 * to read from the specified subordinate stream.
59 * @param in The subordinate <code>InputStream</code> to read from
61 public DataInputStream(InputStream in)
67 * This method reads bytes from the underlying stream into the specified
68 * byte array buffer. It will attempt to fill the buffer completely, but
69 * may return a short count if there is insufficient data remaining to be
70 * read to fill the buffer.
72 * @param b The buffer into which bytes will be read.
74 * @return The actual number of bytes read, or -1 if end of stream reached
75 * before reading any bytes.
77 * @exception IOException If an error occurs.
79 public final int read(byte[] b) throws IOException
81 return in.read(b, 0, b.length);
85 * This method reads bytes from the underlying stream into the specified
86 * byte array buffer. It will attempt to read <code>len</code> bytes and
87 * will start storing them at position <code>off</code> into the buffer.
88 * This method can return a short count if there is insufficient data
89 * remaining to be read to complete the desired read length.
91 * @param b The buffer into which bytes will be read.
92 * @param off The offset into the buffer to start storing bytes.
93 * @param len The requested number of bytes to read.
95 * @return The actual number of bytes read, or -1 if end of stream reached
96 * before reading any bytes.
98 * @exception IOException If an error occurs.
100 public final int read(byte[] b, int off, int len) throws IOException
102 return in.read(b, off, len);
106 * This method reads a Java boolean value from an input stream. It does
107 * so by reading a single byte of data. If that byte is zero, then the
108 * value returned is <code>false</code>. If the byte is non-zero, then
109 * the value returned is <code>true</code>.
111 * This method can read a <code>boolean</code> written by an object
112 * implementing the <code>writeBoolean()</code> method in the
113 * <code>DataOutput</code> interface.
115 * @return The <code>boolean</code> value read
117 * @exception EOFException If end of file is reached before reading
119 * @exception IOException If any other error occurs
121 public final boolean readBoolean() throws IOException
125 throw new EOFException();
130 * This method reads a Java byte value from an input stream. The value
131 * is in the range of -128 to 127.
133 * This method can read a <code>byte</code> written by an object
134 * implementing the <code>writeByte()</code> method in the
135 * <code>DataOutput</code> interface.
137 * @return The <code>byte</code> value read
139 * @exception EOFException If end of file is reached before reading the byte
140 * @exception IOException If any other error occurs
144 public final byte readByte() throws IOException
148 throw new EOFException();
154 * This method reads a Java <code>char</code> value from an input stream.
155 * It operates by reading two bytes from the stream and converting them to
156 * a single 16-bit Java <code>char</code>. The two bytes are stored most
157 * significant byte first (i.e., "big endian") regardless of the native
158 * host byte ordering.
160 * As an example, if <code>byte1</code> and <code>byte2</code>
161 * represent the first and second byte read from the stream
162 * respectively, they will be transformed to a <code>char</code> in
163 * the following manner:
165 * <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
167 * This method can read a <code>char</code> written by an object
168 * implementing the <code>writeChar()</code> method in the
169 * <code>DataOutput</code> interface.
171 * @return The <code>char</code> value read
173 * @exception EOFException If end of file is reached before reading the char
174 * @exception IOException If any other error occurs
178 public final char readChar() throws IOException
183 throw new EOFException();
184 return (char) ((a << 8) | (b & 0xff));
188 * This method reads a Java double value from an input stream. It operates
189 * by first reading a <code>long</code> value from the stream by calling the
190 * <code>readLong()</code> method in this interface, then converts
191 * that <code>long</code> to a <code>double</code> using the
192 * <code>longBitsToDouble</code> method in the class
193 * <code>java.lang.Double</code>
195 * This method can read a <code>double</code> written by an object
196 * implementing the <code>writeDouble()</code> method in the
197 * <code>DataOutput</code> interface.
199 * @return The <code>double</code> value read
201 * @exception EOFException If end of file is reached before reading
203 * @exception IOException If any other error occurs
205 * @see java.lang.Double
208 public final double readDouble() throws IOException
210 return Double.longBitsToDouble(readLong());
214 * This method reads a Java float value from an input stream. It
215 * operates by first reading an <code>int</code> value from the
216 * stream by calling the <code>readInt()</code> method in this
217 * interface, then converts that <code>int</code> to a
218 * <code>float</code> using the <code>intBitsToFloat</code> method
219 * in the class <code>java.lang.Float</code>
221 * This method can read a <code>float</code> written by an object
222 * implementing the * <code>writeFloat()</code> method in the
223 * <code>DataOutput</code> interface.
225 * @return The <code>float</code> value read
227 * @exception EOFException If end of file is reached before reading the float
228 * @exception IOException If any other error occurs
230 * @see java.lang.Float
232 public final float readFloat() throws IOException
234 return Float.intBitsToFloat(readInt());
238 * This method reads raw bytes into the passed array until the array is
239 * full. Note that this method blocks until the data is available and
240 * throws an exception if there is not enough data left in the stream to
243 * @param b The buffer into which to read the data
245 * @exception EOFException If end of file is reached before filling
247 * @exception IOException If any other error occurs */
248 public final void readFully(byte[] b) throws IOException
250 readFully(b, 0, b.length);
254 * This method reads raw bytes into the passed array
255 * <code>buf</code> starting <code>offset</code> bytes into the
256 * buffer. The number of bytes read will be exactly
257 * <code>len</code> Note that this method blocks until the data is
258 * available and * throws an exception if there is not enough data
259 * left in the stream to read <code>len</code> bytes.
261 * @param buf The buffer into which to read the data
262 * @param offset The offset into the buffer to start storing data
263 * @param len The number of bytes to read into the buffer
265 * @exception EOFException If end of file is reached before filling
267 * @exception IOException If any other error occurs
269 public final void readFully(byte[] b, int off, int len) throws IOException
273 // in.read will block until some data is available.
274 int numread = in.read(b, off, len);
276 throw new EOFException();
283 * This method reads a Java <code>int</code> value from an input
284 * stream It operates by reading four bytes from the stream and
285 * converting them to a single Java <code>int</code> The bytes are
286 * stored most significant byte first (i.e., "big endian")
287 * regardless of the native host byte ordering.
289 * As an example, if <code>byte1</code> through <code>byte4</code>
290 * represent the first four bytes read from the stream, they will be
291 * transformed to an <code>int</code> in the following manner:
293 * <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) +
294 * ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code>
296 * The value returned is in the range of 0 to 65535.
298 * This method can read an <code>int</code> written by an object
299 * implementing the <code>writeInt()</code> method in the
300 * <code>DataOutput</code> interface.
302 * @return The <code>int</code> value read
304 * @exception EOFException If end of file is reached before reading the int
305 * @exception IOException If any other error occurs
309 public final int readInt() throws IOException
316 throw new EOFException();
318 return (((a & 0xff) << 24) | ((b & 0xff) << 16) |
319 ((c & 0xff) << 8) | (d & 0xff));
323 * This method reads the next line of text data from an input
324 * stream. It operates by reading bytes and converting those bytes
325 * to <code>char</code> values by treating the byte read as the low
326 * eight bits of the <code>char</code> and using 0 as the high eight
327 * bits. Because of this, it does not support the full 16-bit
328 * Unicode character set.
330 * The reading of bytes ends when either the end of file or a line
331 * terminator is encountered. The bytes read are then returned as a
332 * <code>String</code> A line terminator is a byte sequence
333 * consisting of either <code>\r</code>, <code>\n</code> or
334 * <code>\r\n</code>. These termination charaters are discarded and
335 * are not returned as part of the string.
337 * This method can read data that was written by an object implementing the
338 * <code>writeLine()</code> method in <code>DataOutput</code>.
340 * @return The line read as a <code>String</code>
342 * @exception IOException If an error occurs
348 public final String readLine() throws IOException
350 StringBuffer strb = new StringBuffer();
352 readloop: while (true)
356 boolean getnext = true;
361 if (c < 0) // got an EOF
362 return strb.length() > 0 ? strb.toString() : null;
364 if ((ch &= 0xFF) == '\n')
365 // hack to correctly handle '\r\n' sequences
366 if (ignoreInitialNewline)
368 ignoreInitialNewline = false;
377 // FIXME: The following code tries to adjust the stream back one
378 // character if the next char read is '\n'. As a last resort,
379 // it tries to mark the position before reading but the bottom
380 // line is that it is possible that this method will not properly
381 // deal with a '\r' '\n' combination thus not fulfilling the
382 // DataInput contract for readLine. It's not a particularly
383 // safe approach threadwise since it is unsynchronized and
384 // since it might mark an input stream behind the users back.
385 // Along the same vein it could try the same thing for
386 // ByteArrayInputStream and PushbackInputStream, but that is
387 // probably overkill since this is deprecated & BufferedInputStream
388 // is the most likely type of input stream.
390 // The alternative is to somehow push back the next byte if it
391 // isn't a '\n' or to have the reading methods of this class
392 // keep track of whether the last byte read was '\r' by readLine
393 // and then skip the very next byte if it is '\n'. Either way,
394 // this would increase the complexity of the non-deprecated methods
395 // and since it is undesirable to make non-deprecated methods
396 // less efficient, the following seems like the most reasonable
400 if (in instanceof BufferedInputStream)
403 next_ch = (char) (next_c & 0xFF);
404 if ((next_ch != '\n') && (next_c >= 0))
406 BufferedInputStream bin = (BufferedInputStream) in;
411 else if (markSupported())
414 next_ch = (char) (next_c & 0xFF);
415 if ((next_ch != '\n') && (next_c >= 0))
418 if ((in.read() & 0xFF) != '\n')
422 // In order to catch cases where 'in' isn't a BufferedInputStream
423 // and doesn't support mark() (such as reading from a Socket), set
424 // a flag that instructs readLine() to ignore the first character
425 // it sees _if_ that character is a '\n'.
426 else ignoreInitialNewline = true;
432 return strb.length() > 0 ? strb.toString() : "";
436 * This method reads a Java long value from an input stream
437 * It operates by reading eight bytes from the stream and converting them to
438 * a single Java <code>long</code> The bytes are stored most
439 * significant byte first (i.e., "big endian") regardless of the native
440 * host byte ordering.
442 * As an example, if <code>byte1</code> through <code>byte8</code>
443 * represent the first eight bytes read from the stream, they will
444 * be transformed to an <code>long</code> in the following manner:
446 * <code>(long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) +
447 * (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) +
448 * (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) +
449 * (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code>
451 * The value returned is in the range of 0 to 65535.
453 * This method can read an <code>long</code> written by an object
454 * implementing the <code>writeLong()</code> method in the
455 * <code>DataOutput</code> interface.
457 * @return The <code>long</code> value read
459 * @exception EOFException If end of file is reached before reading the long
460 * @exception IOException If any other error occurs
464 public final long readLong() throws IOException
475 throw new EOFException();
477 return (((long)(a & 0xff) << 56) |
478 ((long)(b & 0xff) << 48) |
479 ((long)(c & 0xff) << 40) |
480 ((long)(d & 0xff) << 32) |
481 ((long)(e & 0xff) << 24) |
482 ((long)(f & 0xff) << 16) |
483 ((long)(g & 0xff) << 8) |
488 * This method reads a signed 16-bit value into a Java in from the
489 * stream. It operates by reading two bytes from the stream and
490 * converting them to a single 16-bit Java <code>short</code>. The
491 * two bytes are stored most significant byte first (i.e., "big
492 * endian") regardless of the native host byte ordering.
494 * As an example, if <code>byte1</code> and <code>byte2</code>
495 * represent the first and second byte read from the stream
496 * respectively, they will be transformed to a <code>short</code>. in
497 * the following manner:
499 * <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
501 * The value returned is in the range of -32768 to 32767.
503 * This method can read a <code>short</code> written by an object
504 * implementing the <code>writeShort()</code> method in the
505 * <code>DataOutput</code> interface.
507 * @return The <code>short</code> value read
509 * @exception EOFException If end of file is reached before reading the value
510 * @exception IOException If any other error occurs
514 public final short readShort() throws IOException
519 throw new EOFException();
520 return (short) ((a << 8) | (b & 0xff));
524 * This method reads 8 unsigned bits into a Java <code>int</code>
525 * value from the stream. The value returned is in the range of 0 to
528 * This method can read an unsigned byte written by an object
529 * implementing the <code>writeUnsignedByte()</code> method in the
530 * <code>DataOutput</code> interface.
532 * @return The unsigned bytes value read as a Java <code>int</code>.
534 * @exception EOFException If end of file is reached before reading the value
535 * @exception IOException If any other error occurs
539 public final int readUnsignedByte() throws IOException
543 throw new EOFException();
549 * This method reads 16 unsigned bits into a Java int value from the stream.
550 * It operates by reading two bytes from the stream and converting them to
551 * a single Java <code>int</code> The two bytes are stored most
552 * significant byte first (i.e., "big endian") regardless of the native
553 * host byte ordering.
555 * As an example, if <code>byte1</code> and <code>byte2</code>
556 * represent the first and second byte read from the stream
557 * respectively, they will be transformed to an <code>int</code> in
558 * the following manner:
560 * <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code>
562 * The value returned is in the range of 0 to 65535.
564 * This method can read an unsigned short written by an object
565 * implementing the <code>writeUnsignedShort()</code> method in the
566 * <code>DataOutput</code> interface.
568 * @return The unsigned short value read as a Java <code>int</code>
570 * @exception EOFException If end of file is reached before reading the value
571 * @exception IOException If any other error occurs
573 public final int readUnsignedShort() throws IOException
578 throw new EOFException();
579 return (((a & 0xff) << 8) | (b & 0xff));
583 * This method reads a <code>String</code> from an input stream that
584 * is encoded in a modified UTF-8 format. This format has a leading
585 * two byte sequence that contains the remaining number of bytes to
586 * read. This two byte sequence is read using the
587 * <code>readUnsignedShort()</code> method of this interface.
589 * After the number of remaining bytes have been determined, these
590 * bytes are read an transformed into <code>char</code> values.
591 * These <code>char</code> values are encoded in the stream using
592 * either a one, two, or three byte format. The particular format
593 * in use can be determined by examining the first byte read.
595 * If the first byte has a high order bit of 0, then that character
596 * consists on only one byte. This character value consists of
597 * seven bits that are at positions 0 through 6 of the byte. As an
598 * example, if <code>byte1</code> is the byte read from the stream,
599 * it would be converted to a <code>char</code> like so:
601 * <code>(char)byte1</code>
603 * If the first byte has 110 as its high order bits, then the
604 * character consists of two bytes. The bits that make up the character
605 * value are in positions 0 through 4 of the first byte and bit positions
606 * 0 through 5 of the second byte. (The second byte should have
607 * 10 as its high order bits). These values are in most significant
608 * byte first (i.e., "big endian") order.
610 * As an example, if <code>byte1</code> and <code>byte2</code> are
611 * the first two bytes read respectively, and the high order bits of
612 * them match the patterns which indicate a two byte character
613 * encoding, then they would be converted to a Java
614 * <code>char</code> like so:
616 * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
618 * If the first byte has a 1110 as its high order bits, then the
619 * character consists of three bytes. The bits that make up the character
620 * value are in positions 0 through 3 of the first byte and bit positions
621 * 0 through 5 of the other two bytes. (The second and third bytes should
622 * have 10 as their high order bits). These values are in most
623 * significant byte first (i.e., "big endian") order.
625 * As an example, if <code>byte1</code> <code>byte2</code> and
626 * <code>byte3</code> are the three bytes read, and the high order
627 * bits of them match the patterns which indicate a three byte
628 * character encoding, then they would be converted to a Java
629 * <code>char</code> like so:
631 * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F))</code>
633 * Note that all characters are encoded in the method that requires
634 * the fewest number of bytes with the exception of the character
635 * with the value of <code>\u0000</code> which is encoded as two
636 * bytes. This is a modification of the UTF standard used to
637 * prevent C language style <code>NUL</code> values from appearing
638 * in the byte stream.
640 * This method can read data that was written by an object implementing the
641 * <code>writeUTF()</code> method in <code>DataOutput</code>
643 * @returns The <code>String</code> read
645 * @exception EOFException If end of file is reached before reading
647 * @exception UTFDataFormatException If the data is not in UTF-8 format
648 * @exception IOException If any other error occurs
652 public final String readUTF() throws IOException
654 return readUTF(this);
658 * This method reads a String encoded in UTF-8 format from the
659 * specified <code>DataInput</code> source.
661 * @param in The <code>DataInput</code> source to read from
663 * @return The String read from the source
665 * @exception IOException If an error occurs
667 public final static String readUTF(DataInput in) throws IOException
669 final int UTFlen = in.readUnsignedShort();
670 byte[] buf = new byte[UTFlen];
671 StringBuffer strbuf = new StringBuffer();
673 // This blocks until the entire string is available rather than
674 // doing partial processing on the bytes that are available and then
675 // blocking. An advantage of the latter is that Exceptions
676 // could be thrown earlier. The former is a bit cleaner.
677 in.readFully(buf, 0, UTFlen);
678 for (int i = 0; i < UTFlen; )
680 if ((buf[i] & 0x80) == 0) // bit pattern 0xxxxxxx
681 strbuf.append((char) (buf[i++] & 0xFF));
682 else if ((buf[i] & 0xE0) == 0xC0) // bit pattern 110xxxxx
684 if (i + 1 >= UTFlen || (buf[i+1] & 0xC0) != 0x80)
685 throw new UTFDataFormatException();
687 strbuf.append((char) (((buf[i++] & 0x1F) << 6) |
690 else if ((buf[i] & 0xF0) == 0xE0) // bit pattern 1110xxxx
692 if (i + 2 >= UTFlen ||
693 (buf[i+1] & 0xC0) != 0x80 || (buf[i+2] & 0xC0) != 0x80)
694 throw new UTFDataFormatException();
696 strbuf.append((char) (((buf[i++] & 0x0F) << 12) |
697 ((buf[i++] & 0x3F) << 6) |
700 else // must be ((buf[i] & 0xF0) == 0xF0 || (buf[i] & 0xC0) == 0x80)
701 throw new UTFDataFormatException(); // bit patterns 1111xxxx or
705 return strbuf.toString();
709 * This method attempts to skip and discard the specified number of bytes
710 * in the input stream. It may actually skip fewer bytes than requested.
711 * This method will not skip any bytes if passed a negative number of bytes
714 * @param n The requested number of bytes to skip.
715 * @return The requested number of bytes to skip.
716 * @exception IOException If an error occurs.
717 * @specnote The JDK docs claim that this returns the number of bytes
718 * actually skipped. The JCL claims that this method can throw an
719 * EOFException. Neither of these appear to be true in the JDK 1.3's
720 * implementation. This tries to implement the actual JDK behaviour.
722 public final int skipBytes(int n) throws IOException
728 return (int) in.skip(n);
730 catch (EOFException x)