- Home
- Objectives

- XyzWs Study Guides
- Study Guides
- Study Notes
- Resources

- Mock Exams
SCJP Study Guide:
API Contents
Printer-friendly version |
Mail this to a friend
java.io.DataInputStream
The package java.io provides two set of class hierarchies - one for handling reading and writing of bytes, and another for handling reading and writing of characters. The abstract classes InputStream and OutputStream are the root of inheritance hierarchies handling reading and writing of bytes respectively.
java.io.DataInputStream
The DataInputStream class is a subclass of FilterInputStream that provides methods for reading a variety of data types. The DataInputStream class implements the DataInput interface, so it defines methods for reading all of the primitive Java data types.
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.
DataInputStream, like other filtered input streams, must be attached to some other InputStream. You create a DataInputStream by passing a reference to an underlying InputStream to the constructor.
-
public DataInputStream(InputStream in) creates a DataInputStream that uses the specified underlying InputStream.
DataInputStream provides a high-level interface for reading data from a file. All of the reading methods are thrown an IOException if an I/O error occurs. All of the reading methods except for read from a byte[] array) are thrown an EOFException if this stream reaches the end before reading all the bytes.
- public final int read(byte[] b) throws IOException reads some number of bytes from the contained input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.
If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b. If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.
- public final int read(byte[] b, int off, int len) throws IOException reads up to len bytes of data from the contained input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.
If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b. In every case, elements b[0] through b[off] and elements b[off+len] through b[b.length-1] are unaffected. If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.
- public final void readFully(byte[] b) throws IOException reads some bytes from an input stream and stores them into the buffer array b. Bytes for this operation are read from the contained input stream. The number of bytes read is equal to the length of b. This method blocks until one of the following conditions occurs: 1). b.length bytes of input data are available, in which case a normal return is made. 2). End of file is detected, in which case an EOFException is thrown. 3). An I/O error occurs, in which case an IOException other than EOFException is thrown.
If b is null, a NullPointerException is thrown. If b.length is zero, then no bytes are read. Otherwise, the first byte read is stored into element b[0], the next one into b[1], and so on. If an exception is thrown from this method, then it may be that some but not all bytes of b have been updated with data from the input stream. If this stream reaches the end before reading all the bytes, an EOFException is thrown. If an I/O error occurs, an IOException is thrown.
- public final void readFully(byte[] b, int off, int len) throws IOException reads len bytes from an input stream. This method blocks until one of the following conditions occurs: 1). len bytes of input data are available, in which case a normal return is made. 2). End of file is detected, in which case an EOFException is thrown. 3). An I/O error occurs, in which case an IOException other than EOFException is thrown.
If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are read. Otherwise, the first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. If this stream reaches the end before reading all the bytes, an EOFException is thrown.
- public final boolean readBoolean() throws IOException reads one input byte and returns true if that byte is nonzero, false if that byte is zero. This method is suitable for reading the byte written by the writeBoolean method of interface DataOutput.
- public final byte readByte() throws IOException reads and returns one input byte (a signed 8-bit byte). The byte is treated as a signed value in the range -128 through 127, inclusive. This method is suitable for reading the byte written by the writeByte method of interface DataOutput.
- public final int readUnsignedByte() throws IOException reads one input byte (the unsigned 8-bit byte value), zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255. This method is suitable for reading the byte written by the writeByte method of interface DataOutput if the argument to writeByte was intended to be a value in the range 0 through 255.
-
public final short readShort() throws IOException reads two input bytes and
returns a short value. Let a be the first byte read and b be the second byte.
The value returned is:
(short)((a << 8) | (b & 0xff)). This method is suitable for reading the bytes written by the writeShort method of interface DataOutput. -
public final int readUnsignedShort() throws IOException reads two input bytes
and returns an int value in the range 0 through 65535. Let a be the first byte
read and b be the second byte. The value returned is:
(((a & 0xff) << 8) | (b & 0xff)). This method is suitable for reading the bytes written by the writeShort method of interface DataOutput if the argument to writeShort was intended to be a value in the range 0 through 65535. -
public final char readChar() throws IOException reads an input char and returns
the Unicode char. A Unicode char is made up of two bytes. Let a be the first
byte read and b be the second byte. The value returned is:
(char)((a << 8) | (b & 0xff)). This method is suitable for reading bytes written by the writeChar method of interface DataOutput. -
public final int readInt() throws IOException reads four input bytes and
returns an int value. Let a be the first byte read, b be the second byte, c be
the third byte, and d be the fourth byte. The value returned is:
(((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)). This method is suitable for reading bytes written by the writeInt method of interface DataOutput. - public final long readLong() throws IOException reads eight input bytes and returns a long value. Let a be the first byte read, b be the second byte, c be the third byte, d be the fourth byte, e be the fifth byte, f be the sixth byte, g be the seventh byte, and h be the eighth byte. The value returned is:
(((long)(a & 0xff) << 56) | ((long)(b & 0xff) << 48) | .
((long)(c & 0xff) << 40) | ((long)(d & 0xff) << 32) |
((long)(e & 0xff) << 24) | ((long)(f & 0xff) << 16) |
((long)(g & 0xff) << 8) | ((long)(h & 0xff)))
This method is suitable for reading bytes written by the writeLong method of interface DataOutput.
- public final float readFloat() throws IOException reads four input bytes and returns a float value. It does this by first constructing an int value in exactly the manner of the readInt method, then converting this int value to a float in exactly the manner of the method Float.intBitsToFloat. This method is suitable for reading bytes written by the writeFloat method of interface DataOutput.
- public final double readDouble() throws IOException reads eight input bytes and returns a double value. It does this by first constructing a long value in exactly the manner of the readlong method, then converting this long value to a double in exactly the manner of the method Double.longBitsToDouble. This method is suitable for reading bytes written by the writeDouble method of interface DataOutput.
DataInputStream also provides a high-level interface for reading modified UTF-8 data format from a file. All of the reading methods are thrown an IOException if an I/O error occurs; If the bytes do not represent a valid modified UTF-8 encoding of a string, an UTFDataFormatException is thrown; If this input stream reaches the end before reading all the bytes, an EOFException is thrown.
- public final String readUTF() throws IOException reads in a string that has been encoded using a modified UTF-8 format. The general contract of readUTF is that it reads a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.
- public static final String readUTF(DataInput in) throws IOException reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String. The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.
Here is an example that creates a DataInputStream and uses it to read an int that represents the length of an array and then to read the array of long values:
Data must be read sequentially from a DataInputStream; you can skip forward, but you cannot move back.
- public final int skipBytes(int n) throws IOException makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes. However, it may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned. An IOException is thrown if an I/O error occurs.