public class DataInputStream extends FilterInputStream implements DataInput
InputStream
and reads big-endian typed data from it.
Typically, this stream has been written by a DataOutputStream. Types that can
be read include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long,
64-bit double, byte strings, and strings encoded in
modified UTF-8
.DataOutputStream
in
Constructor and Description |
---|
DataInputStream(InputStream in)
Constructs a new DataInputStream on the InputStream
in . |
Modifier and Type | Method and Description |
---|---|
int |
read(byte[] buffer)
Equivalent to
read(buffer, 0, buffer.length) . |
int |
read(byte[] buffer,
int offset,
int length)
Reads at most
length bytes from this stream and stores them in
the byte array buffer starting at offset . |
boolean |
readBoolean()
Reads a boolean.
|
byte |
readByte()
Reads an 8-bit byte value.
|
char |
readChar()
Reads a big-endian 16-bit character value.
|
double |
readDouble()
Reads a big-endian 64-bit double value.
|
float |
readFloat()
Reads a big-endian 32-bit float value.
|
void |
readFully(byte[] dst)
Equivalent to
readFully(dst, 0, dst.length); . |
void |
readFully(byte[] dst,
int offset,
int byteCount)
Reads
byteCount bytes from this stream and stores them in the byte
array dst starting at offset . |
int |
readInt()
Reads a big-endian 32-bit integer value.
|
String |
readLine()
Deprecated.
|
long |
readLong()
Reads a big-endian 64-bit long value.
|
short |
readShort()
Reads a big-endian 16-bit short value.
|
int |
readUnsignedByte()
Reads an unsigned 8-bit byte value and returns it as an int.
|
int |
readUnsignedShort()
Reads a big-endian 16-bit unsigned short value and returns it as an int.
|
String |
readUTF()
Reads a string encoded with
modified UTF-8 . |
static String |
readUTF(DataInput in) |
int |
skipBytes(int count)
Skips
count number of bytes in this stream. |
available, close, mark, markSupported, read, reset, skip
public DataInputStream(InputStream in)
in
. All
reads are then filtered through this stream. Note that data read by this
stream is not in a human readable format and was most likely created by a
DataOutputStream.
Warning: passing a null source creates an invalid
DataInputStream
. All operations on such a stream will fail.
in
- the source InputStream the filter reads from.DataOutputStream
,
RandomAccessFile
public final int read(byte[] buffer) throws IOException
InputStream
read(buffer, 0, buffer.length)
.read
in class InputStream
IOException
public final int read(byte[] buffer, int offset, int length) throws IOException
length
bytes from this stream and stores them in
the byte array buffer
starting at offset
. Returns the
number of bytes that have been read or -1 if no bytes have been read and
the end of the stream has been reached.read
in class FilterInputStream
buffer
- the byte array in which to store the bytes read.offset
- the initial position in buffer
to store the bytes
read from this stream.length
- the maximum number of bytes to store in buffer
.IOException
- if a problem occurs while reading from this stream.DataOutput.write(byte[])
,
DataOutput.write(byte[], int, int)
public final boolean readBoolean() throws IOException
DataInput
readBoolean
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeBoolean(boolean)
public final byte readByte() throws IOException
DataInput
readByte
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeByte(int)
public final char readChar() throws IOException
DataInput
readChar
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeChar(int)
public final double readDouble() throws IOException
DataInput
readDouble
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeDouble(double)
public final float readFloat() throws IOException
DataInput
readFloat
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeFloat(float)
public final void readFully(byte[] dst) throws IOException
DataInput
readFully(dst, 0, dst.length);
.readFully
in interface DataInput
IOException
public final void readFully(byte[] dst, int offset, int byteCount) throws IOException
DataInput
byteCount
bytes from this stream and stores them in the byte
array dst
starting at offset
. If byteCount
is zero, then this
method returns without reading any bytes. Otherwise, this method blocks until
byteCount
bytes have been read. If insufficient bytes are available,
EOFException
is thrown. If an I/O error occurs, IOException
is
thrown. When an exception is thrown, some bytes may have been consumed from the stream
and written into the array.readFully
in interface DataInput
dst
- the byte array into which the data is read.offset
- the offset in dst
at which to store the bytes.byteCount
- the number of bytes to read.EOFException
- if the end of the source stream is reached before enough
bytes have been read.IOException
- if a problem occurs while reading from this stream.public final int readInt() throws IOException
DataInput
readInt
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeInt(int)
@Deprecated public final String readLine() throws IOException
DataInput
'\n'
, '\r'
, "\r\n"
or the end of the stream. The string
does not include the newline sequence.readLine
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.public final long readLong() throws IOException
DataInput
readLong
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeLong(long)
public final short readShort() throws IOException
DataInput
readShort
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeShort(int)
public final int readUnsignedByte() throws IOException
DataInput
readUnsignedByte
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeByte(int)
public final int readUnsignedShort() throws IOException
DataInput
readUnsignedShort
in interface DataInput
EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeShort(int)
public final String readUTF() throws IOException
DataInput
modified UTF-8
.readUTF
in interface DataInput
modified UTF-8
.EOFException
- if the end of the input is reached before the read
request can be satisfied.IOException
- if an I/O error occurs while reading.DataOutput.writeUTF(java.lang.String)
public static final String readUTF(DataInput in) throws IOException
IOException
public final int skipBytes(int count) throws IOException
count
number of bytes in this stream. Subsequent read()
s will not return these bytes unless reset()
is used.
This method will not throw an EOFException
if the end of the
input is reached before count
bytes where skipped.skipBytes
in interface DataInput
count
- the number of bytes to skip.IOException
- if a problem occurs during skipping.FilterInputStream.mark(int)
,
FilterInputStream.reset()