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.DataOutputStreamin| 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 byteOffset,
int byteCount)
Reads up to
byteCount bytes from this stream and stores them in
the byte array buffer starting at byteOffset. |
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.
This method cannot be trusted to convert bytes to characters correctly.
Wrap this stream with a
BufferedReader instead. |
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, skipnullInputStream, readAllBytes, readNBytes, readNBytes, transferTopublic 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,
RandomAccessFilepublic final int read(byte[] buffer)
throws IOException
InputStreamread(buffer, 0, buffer.length).read in class InputStreamIOExceptionpublic final int read(byte[] buffer,
int byteOffset,
int byteCount)
throws IOException
InputStreambyteCount bytes from this stream and stores them in
the byte array buffer starting at byteOffset.
Returns the number of bytes actually read or -1 if the end of the stream
has been reached.read in class FilterInputStreamIOException - if the stream is closed or another IOException occurs.public final boolean readBoolean()
throws IOException
DataInputreadBoolean in interface DataInputEOFException - 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
DataInputreadByte in interface DataInputEOFException - 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
DataInputreadChar in interface DataInputEOFException - 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
DataInputreadDouble in interface DataInputEOFException - 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
DataInputreadFloat in interface DataInputEOFException - 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
DataInputreadFully(dst, 0, dst.length);.readFully in interface DataInputIOExceptionpublic final void readFully(byte[] dst,
int offset,
int byteCount)
throws IOException
DataInputbyteCount 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 DataInputdst - 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
DataInputreadInt in interface DataInputEOFException - 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
BufferedReader instead.DataInput'\n', '\r', "\r\n" or the end of the stream. The string
does not include the newline sequence.readLine in interface DataInputEOFException - 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
DataInputreadLong in interface DataInputEOFException - 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
DataInputreadShort in interface DataInputEOFException - 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
DataInputreadUnsignedByte in interface DataInputEOFException - 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
DataInputreadUnsignedShort in interface DataInputEOFException - 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
DataInputmodified UTF-8.readUTF in interface DataInputmodified 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
IOExceptionpublic 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 DataInputcount - the number of bytes to skip.IOException - if a problem occurs during skipping.FilterInputStream.mark(int),
FilterInputStream.reset()