|
Chapter 11 The java.io Package |
|
DataInputStream
Name
DataInputStream
- Class Name:
-
java.io.DataInputStream
- Superclass:
-
java.io.FilterInputStream
- Immediate Subclasses:
-
None
- Interfaces Implemented:
-
java.io.DataInput
- Availability:
-
JDK 1.0 or later
The DataInputStream class provides
methods for reading primitive data types and lines of text from an underlying
input stream in a machine-independent manner. Many of the methods of DataInputStream
read a single primitive data type, in binary format, from an underlying
input stream. All multibyte quantities are assumed to be in a format that
stores the most significant byte as the first byte and the least significant
byte as the last byte.
public class java.io.DataInputStream extends java.io.FilterInputStream
implements java.io.DataInput {
// Constructors
public DataInputStream(InputStream in);
// Class Methods
public final static String readUTF(DataInput in);
// Instance Methods
public final int read(byte[] b);
public final int read(byte[] b, int off, int len);
public final boolean readBoolean();
public final byte readByte();
public final char readChar();
public final double readDouble();
public final float readFloat();
public final void readFully(byte[] b);
public final void readFully(byte[] b, int off, int len);
public final int readInt();
public final String readLine(); // Deprecated in 1.1
public final long readLong();
public final short readShort();
public final int readUnsignedByte();
public final int readUnsignedShort();
public final String readUTF() throws IOException;
public final int skipBytes(int n) throws IOException;
}
- Parameters
-
- in
-
The input stream to
use.
- Description
-
This constructor creates a DataInputStream
object that reads from, or wraps, the given input stream.
- Parameters
-
- in
-
The data input stream
to use.
- Returns
-
The String
read from the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- UTFDataFormatException
-
If the bytes do not represent a valid UTF-8 encoding.
- Description
-
This method reads a UTF-8 encoded string from the
given DataInput object. To get the number of bytes
in the encoded string, the first two bytes are read as an unsigned
short value. Then the following bytes are read and
interpreted as UTF-8 encoded bytes; these bytes are converted into
characters for the resulting String. This method
blocks until all of the bytes in the encoded string have been read,
the end of the stream is encountered, or an exception is thrown.
For details on the UTF-8 encoding, see Appendix B, The UTF-8 Encoding.
- Parameters
-
- b
-
An array of bytes to
be filled from the stream.
- Returns
-
The number of bytes read, or -1 if the end of file is encountered immediately.
- Throws
-
- IOException
-
If
any kind of I/O error occurs.
- Overrides
-
FilterInputStream.read(byte[])
- Description
-
This method reads bytes of input into the given array by calling the read()
method of the underlying stream. The method reads up to b.length
bytes of data from the stream. The method blocks until there is some input
available.
- Parameters
-
- b
-
An array of bytes to be filled from the stream.
- off
-
An offset into the byte array.
- len
-
The number of bytes to read.
- Returns
-
The number of bytes read, or -1 if the end of file is encountered immediately.
- Throws
-
- IOException
-
If any kind of I/O error occurs.
- Overrides
-
FilterInputStream.read(byte[], int, int)
- Description
-
This method reads up to len
bytes of input into the given array starting at index off.
The method reads the bytes by calling the read()
method of the underlying stream and blocks until there is some
input available.
- Returns
-
The boolean value read from
the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readBoolean()
- Description
-
This method reads a byte as a boolean
value from the underlying input stream. A byte that contains a zero is
read as false; that which
contains any other value is read as true.
The method blocks until the byte is read, the end of the stream is encountered,
or an exception is thrown.
- Returns
-
The byte value read from the
stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readByte()
- Description
-
This method reads a signed 8-bit value--a byte--from the underlying input stream. The method blocks until the byte is read,
the end of the stream is encountered, or an exception is thrown.
- Returns
-
The char value read from the
stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readChar()
- Description
-
This method reads a 16-bit Unicode character from the stream. The method
reads two bytes from the underlying input stream and then creates a char
value, using the first byte read as the most significant byte. The method
blocks until the two bytes are read, the end of the stream is encountered,
or an exception is thrown.
- Returns
-
The double value read from
the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readDouble()
- Description
-
This method reads a 64-bit double
quantity from the stream. The method reads a long
value from the underlying input stream as if using the readLong()
method. The long value is then
converted to a double using
the longBitsToDouble() method
in Double. The method blocks
until the necessary eight bytes are read, the end of the stream is encountered,
or an exception is thrown.
- Returns
-
The float value read from the
stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readFloat()
- Description
-
This method reads a 32-bit float
quantity from the stream. The method reads an int
value from the underlying input stream as if using the readInt()
method. The int value is then
converted to a float using
the intBitsToFloat() method
in Float. The method blocks
until the necessary four bytes are read, the end of the stream is encountered,
or an exception is thrown.
- Parameters
-
- b
-
The array to fill.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readFully(byte[])
- Description
-
This method reads bytes into the given array b
until the array is full. The method reads repeatedly from the underlying
stream to fill the array. The method blocks until all of the bytes are
read, the end of the stream is encountered, or an exception is thrown.
- Parameters
-
- b
-
The array to fill.
- off
-
An offset into the array.
- len
-
The number of bytes to read.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readFully(byte[], int, int)
- Description
-
This method reads len bytes into the given array, starting
at offset off. The method reads repeatedly from the underlying
stream to fill the array. The method blocks until all the bytes are
read, the end of the stream is encountered, or an exception is thrown.
- Returns
-
The int value read from the
stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readInt()
- Description
-
This method reads a signed 32-bit int
quantity from the stream. The method reads four bytes from the underlying
input stream and then creates an int
quantity, using the first byte read as the most significant byte. The method
blocks until the four bytes are read, the end of the stream is encountered,
or an exception is thrown.
- Availability
-
Deprecated as of JDK 1.1
- Returns
-
A String that contains the
line read from the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other I/O error occurs.
- Implements
-
DataInput.readLine()
- Description
-
This method reads the next line of text from the stream. The method reads
bytes of data from the underlying input stream until it encounters a line
terminator. A line terminator is a carriage return ("\r"),
a newline character ("\n"),
a carriage return immediately followed by a newline character, or the end
of the stream. The method blocks until a line terminator is read, the end
of the stream is encountered, or an exception is thrown.
This method is deprecated as of JDK 1.1 because it does not convert bytes
to characters correctly. It's replaced by BufferedReader.readLine().
- Returns
-
The long value read from the
stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readLong()
- Description
-
This method reads a signed 64-bit long
quantity from the stream. The method reads eight bytes from the underlying
input stream and then creates a long
quantity, using the first byte read as the most significant byte. The method
blocks until the eight bytes are read, the end of the stream is encountered,
or an exception is thrown.
- Returns
-
The short value read from the
stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readShort()
- Description
-
This method reads a signed 16-bit short
quantity from the stream. The method reads two bytes from the underlying
input stream and then creates a short
quantity, using the first byte read as the most significant byte. The method
blocks until the two bytes are read, the end of the stream is encountered,
or an exception is thrown.
- Returns
-
The unsigned byte value read
from the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readUnsignedByte()
- Description
-
This method reads an unsigned 8-bit quantity from the stream. The method
reads a byte from the underlying input stream and returns that byte, and blocks until the byte is read, the end of the stream is encountered,
or an exception is thrown.
- Returns
-
The unsigned short value read
from the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- Implements
-
DataInput.readUnsignedShort()
- Description
-
This method reads an unsigned 16-bit quantity from the stream. The method
reads two bytes from the underlying input stream and creates an unsigned
short quantity, using the first
byte read as the most significant byte. The method blocks until the two
bytes are read, the end of the stream is encountered, or an exception is
thrown.
- Returns
-
The String
read from the stream.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind of I/O error occurs.
- UTFDataFormatException
-
If the bytes do not represent a valid UTF-8 encoding.
- Implements
-
DataInput.readUTF()
- Description
-
This method reads a UTF-8 encoded string from the stream.
See the description
of the readUTF(DataInput) class method for more
information.
- Parameters
-
- n
-
The number of bytes to
skip.
- Returns
-
The actual number of skipped bytes.
- Throws
-
- EOFException
-
If the end of the file is encountered.
- IOException
-
If any other kind I/O error occurs.
- Implements
-
DataInput.skipBytes()
- Description
-
This method skips over n bytes
in the underlying input stream. The method blocks until all of the bytes
are skipped, the end of the stream is encountered, or an exception is thrown.
DataOutputStream,
Double,
EOFException,
FilterInputStream,
Float,
InputStream,
IOException,
String,
UTFDataFormatException
|