Package java.io
Class DataInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.DataInputStream
-
- All Implemented Interfaces:
Closeable,DataInput,AutoCloseable
public class DataInputStream extends FilterInputStream implements DataInput
Wraps an existingInputStreamand 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 inmodified UTF-8.- See Also:
DataOutputStream
-
-
Field Summary
-
Fields inherited from class java.io.FilterInputStream
in
-
-
Constructor Summary
Constructors Constructor Description DataInputStream(InputStream in)Constructs a new DataInputStream on the InputStreamin.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intread(byte[] buffer)Equivalent toread(buffer, 0, buffer.length).intread(byte[] buffer, int byteOffset, int byteCount)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.booleanreadBoolean()Reads a boolean.bytereadByte()Reads an 8-bit byte value.charreadChar()Reads a big-endian 16-bit character value.doublereadDouble()Reads a big-endian 64-bit double value.floatreadFloat()Reads a big-endian 32-bit float value.voidreadFully(byte[] dst)Equivalent toreadFully(dst, 0, dst.length);.voidreadFully(byte[] dst, int offset, int byteCount)ReadsbyteCountbytes from this stream and stores them in the byte arraydststarting atoffset.intreadInt()Reads a big-endian 32-bit integer value.StringreadLine()Deprecated.This method cannot be trusted to convert bytes to characters correctly.longreadLong()Reads a big-endian 64-bit long value.shortreadShort()Reads a big-endian 16-bit short value.intreadUnsignedByte()Reads an unsigned 8-bit byte value and returns it as an int.intreadUnsignedShort()Reads a big-endian 16-bit unsigned short value and returns it as an int.StringreadUTF()Reads a string encoded withmodified UTF-8.static StringreadUTF(DataInput in)intskipBytes(int count)Skipscountnumber of bytes in this stream.-
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
-
-
-
-
Constructor Detail
-
DataInputStream
public DataInputStream(InputStream in)
Constructs a new DataInputStream on the InputStreamin. 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.- Parameters:
in- the source InputStream the filter reads from.- See Also:
DataOutputStream,RandomAccessFile
-
-
Method Detail
-
read
public final int read(byte[] buffer) throws IOExceptionDescription copied from class:InputStreamEquivalent toread(buffer, 0, buffer.length).- Overrides:
readin classInputStream- Throws:
IOException
-
read
public final int read(byte[] buffer, int byteOffset, int byteCount) throws IOExceptionDescription copied from class:InputStreamReads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.- Overrides:
readin classFilterInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
readBoolean
public final boolean readBoolean() throws IOExceptionDescription copied from interface:DataInputReads a boolean.- Specified by:
readBooleanin interfaceDataInput- Returns:
- the next boolean value.
- Throws:
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.- See Also:
DataOutput.writeBoolean(boolean)
-
readByte
public final byte readByte() throws IOExceptionDescription copied from interface:DataInputReads an 8-bit byte value.- Specified by:
readBytein interfaceDataInput- Returns:
- the next byte value.
- Throws:
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.- See Also:
DataOutput.writeByte(int)
-
readChar
public final char readChar() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 16-bit character value.- Specified by:
readCharin interfaceDataInput- Returns:
- the next char value.
- Throws:
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.- See Also:
DataOutput.writeChar(int)
-
readDouble
public final double readDouble() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 64-bit double value.- Specified by:
readDoublein interfaceDataInput- Returns:
- the next double value.
- Throws:
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.- See Also:
DataOutput.writeDouble(double)
-
readFloat
public final float readFloat() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 32-bit float value.- Specified by:
readFloatin interfaceDataInput- Returns:
- the next float value.
- Throws:
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.- See Also:
DataOutput.writeFloat(float)
-
readFully
public final void readFully(byte[] dst) throws IOExceptionDescription copied from interface:DataInputEquivalent toreadFully(dst, 0, dst.length);.- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
public final void readFully(byte[] dst, int offset, int byteCount) throws IOExceptionDescription copied from interface:DataInputReadsbyteCountbytes from this stream and stores them in the byte arraydststarting atoffset. IfbyteCountis zero, then this method returns without reading any bytes. Otherwise, this method blocks untilbyteCountbytes have been read. If insufficient bytes are available,EOFExceptionis thrown. If an I/O error occurs,IOExceptionis thrown. When an exception is thrown, some bytes may have been consumed from the stream and written into the array.- Specified by:
readFullyin interfaceDataInput- Parameters:
dst- the byte array into which the data is read.offset- the offset indstat which to store the bytes.byteCount- the number of bytes to read.- Throws:
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.
-
readInt
public final int readInt() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 32-bit integer value.- Specified by:
readIntin interfaceDataInput- Returns:
- the next int value.
- Throws:
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.- See Also:
DataOutput.writeInt(int)
-
readLine
@Deprecated public final String readLine() throws IOException
Deprecated.This method cannot be trusted to convert bytes to characters correctly. Wrap this stream with aBufferedReaderinstead.Description copied from interface:DataInputReturns a string containing the next line of text available from this stream. A line is made of zero or more characters followed by'\n','\r',"\r\n"or the end of the stream. The string does not include the newline sequence.- Specified by:
readLinein interfaceDataInput- Returns:
- the contents of the line or null if no characters have been read before the end of the stream.
- Throws:
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.
-
readLong
public final long readLong() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 64-bit long value.- Specified by:
readLongin interfaceDataInput- Returns:
- the next long value.
- Throws:
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.- See Also:
DataOutput.writeLong(long)
-
readShort
public final short readShort() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 16-bit short value.- Specified by:
readShortin interfaceDataInput- Returns:
- the next short value.
- Throws:
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.- See Also:
DataOutput.writeShort(int)
-
readUnsignedByte
public final int readUnsignedByte() throws IOExceptionDescription copied from interface:DataInputReads an unsigned 8-bit byte value and returns it as an int.- Specified by:
readUnsignedBytein interfaceDataInput- Returns:
- the next unsigned byte value.
- Throws:
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.- See Also:
DataOutput.writeByte(int)
-
readUnsignedShort
public final int readUnsignedShort() throws IOExceptionDescription copied from interface:DataInputReads a big-endian 16-bit unsigned short value and returns it as an int.- Specified by:
readUnsignedShortin interfaceDataInput- Returns:
- the next unsigned short value.
- Throws:
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.- See Also:
DataOutput.writeShort(int)
-
readUTF
public final String readUTF() throws IOException
Description copied from interface:DataInputReads a string encoded withmodified UTF-8.- Specified by:
readUTFin interfaceDataInput- Returns:
- the next string encoded with
modified UTF-8. - Throws:
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.- See Also:
DataOutput.writeUTF(java.lang.String)
-
readUTF
public static final String readUTF(DataInput in) throws IOException
- Throws:
IOException
-
skipBytes
public final int skipBytes(int count) throws IOExceptionSkipscountnumber of bytes in this stream. Subsequentread()s will not return these bytes unlessreset()is used. This method will not throw anEOFExceptionif the end of the input is reached beforecountbytes where skipped.- Specified by:
skipBytesin interfaceDataInput- Parameters:
count- the number of bytes to skip.- Returns:
- the number of bytes actually skipped.
- Throws:
IOException- if a problem occurs during skipping.- See Also:
FilterInputStream.mark(int),FilterInputStream.reset()
-
-