Package java.io

Class DataInputStream

All Implemented Interfaces:
Closeable, DataInput, AutoCloseable

public class DataInputStream
extends FilterInputStream
implements DataInput
Wraps an existing 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.
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 InputStream in.
  • Method Summary

    Modifier and Type Method 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.
    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.

    Methods inherited from class java.io.FilterInputStream

    available, close, mark, markSupported, read, reset, skip

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DataInputStream

      public DataInputStream​(InputStream in)
      Constructs a new DataInputStream on the InputStream 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.

      Parameters:
      in - the source InputStream the filter reads from.
      See Also:
      DataOutputStream, RandomAccessFile
  • Method Details

    • read

      public final int read​(byte[] buffer) throws IOException
      Description copied from class: InputStream
      Equivalent to read(buffer, 0, buffer.length).
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public final int read​(byte[] buffer, int byteOffset, int byteCount) throws IOException
      Description copied from class: InputStream
      Reads up to byteCount 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.
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException - if the stream is closed or another IOException occurs.
    • readBoolean

      public final boolean readBoolean() throws IOException
      Description copied from interface: DataInput
      Reads a boolean.
      Specified by:
      readBoolean in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads an 8-bit byte value.
      Specified by:
      readByte in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 16-bit character value.
      Specified by:
      readChar in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 64-bit double value.
      Specified by:
      readDouble in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 32-bit float value.
      Specified by:
      readFloat in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Equivalent to readFully(dst, 0, dst.length);.
      Specified by:
      readFully in interface DataInput
      Throws:
      IOException
    • readFully

      public final void readFully​(byte[] dst, int offset, int byteCount) throws IOException
      Description copied from interface: DataInput
      Reads 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.
      Specified by:
      readFully in interface DataInput
      Parameters:
      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.
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 32-bit integer value.
      Specified by:
      readInt in interface DataInput
      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 a BufferedReader instead.
      Description copied from interface: DataInput
      Returns 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:
      readLine in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 64-bit long value.
      Specified by:
      readLong in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 16-bit short value.
      Specified by:
      readShort in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads an unsigned 8-bit byte value and returns it as an int.
      Specified by:
      readUnsignedByte in interface DataInput
      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 IOException
      Description copied from interface: DataInput
      Reads a big-endian 16-bit unsigned short value and returns it as an int.
      Specified by:
      readUnsignedShort in interface DataInput
      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: DataInput
      Reads a string encoded with modified UTF-8.
      Specified by:
      readUTF in interface DataInput
      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 IOException
      Skips 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.
      Specified by:
      skipBytes in interface DataInput
      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()