Package edu.rit.io

Class DataInputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public class DataInputStream extends FilterInputStream
Class DataInputStream provides an input stream that reads primitive data types and strings in binary form. It behaves similarly to class java.io.DataInputStream, except the methods for reading types byte, short, char, int, long, and String are implemented differently. These methods read an integer value using a variable number of bytes, as described below. This can save space in the file if small integer values are written more frequently than large integer values. The byte stream being read must have been written using class DataOutputStream.

Note that class DataInputStream does not implement interface java.io.DataInput, because the methods do not obey the contract specified in that interface.

Version:
19-Dec-2009
Author:
Alan Kaminsky
  • Constructor Details

    • DataInputStream

      public DataInputStream(InputStream in)
      Construct a new data input stream.
      Parameters:
      in - Underlying input stream.
  • Method Details

    • readBoolean

      public boolean readBoolean() throws IOException
      Read a Boolean value from this data input stream. One byte is read; if it is 0, false is returned; otherwise true is returned.
      Returns:
      Boolean value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readByte

      public byte readByte() throws IOException
      Read a byte value from this data input stream. An int is read using readInt(), the int is converted to a byte, and the byte is returned.
      Returns:
      Byte value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readShort

      public short readShort() throws IOException
      Read a short value from this data input stream. An int is read using readInt(), the int is converted to a short, and the short is returned.
      Returns:
      Short value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readChar

      public char readChar() throws IOException
      Read a character value from this data input stream. An int is read using readInt(), the int is converted to a char, and the char is returned.
      Returns:
      Character value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readInt

      public int readInt() throws IOException
      Read an integer value from this data input stream. From one to five bytes are read, in big-endian order, as follows:
      • If the first byte's most significant bit is 0, then the least significant 7 bits are sign-extended to an int and returned.
      • Else if the first byte's two most significant bits are 10, then one more byte is read, and the least significant 14 bits are sign-extended to an int and returned.
      • Else if the first byte's three most significant bits are 110, then two more bytes are read, and the least significant 21 bits are sign-extended to an int and returned.
      • Else if the first byte's four most significant bits are 1110, then three more bytes are read, and the least significant 28 bits are sign-extended to an int and returned.
      • Else four more bytes are read, and the least significant 32 bits are returned.
      Returns:
      Integer value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readUnsignedByte

      public byte readUnsignedByte() throws IOException
      Read an unsigned byte value from this data input stream. An int is read using readUnsignedInt(), the int is converted to a byte, and the byte is returned.
      Returns:
      Byte value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readUnsignedShort

      public short readUnsignedShort() throws IOException
      Read an unsigned short value from this data input stream. An int is read using readUnsignedInt(), the int is converted to a short, and the short is returned.
      Returns:
      Short value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readUnsignedChar

      public char readUnsignedChar() throws IOException
      Read an unsigned character value from this data input stream. An int is read using readUnsignedInt(), the int is converted to a char, and the char is returned.
      Returns:
      Character value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readUnsignedInt

      public int readUnsignedInt() throws IOException
      Read an unsigned integer value from this data input stream. From one to five bytes are read, in big-endian order, as follows:
      • If the first byte's most significant bit is 0, then the least significant 7 bits are zero-extended to an int and returned.
      • Else if the first byte's two most significant bits are 10, then one more byte is read, and the least significant 14 bits are zero-extended to an int and returned.
      • Else if the first byte's three most significant bits are 110, then two more bytes are read, and the least significant 21 bits are zero-extended to an int and returned.
      • Else if the first byte's four most significant bits are 1110, then three more bytes are read, and the least significant 28 bits are zero-extended to an int and returned.
      • Else four more bytes are read, and the least significant 32 bits are returned.
      Returns:
      Integer value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readLong

      public long readLong() throws IOException
      Read a long value from this data input stream. From one to nine bytes are read, in big-endian order, as follows:
      • If the first byte's most significant bit is 0, then the least significant 7 bits are sign-extended to a long and returned.
      • Else if the first byte's two most significant bits are 10, then one more byte is read, and the least significant 14 bits are sign-extended to a long and returned.
      • Else if the first byte's three most significant bits are 110, then two more bytes are read, and the least significant 21 bits are sign-extended to a long and returned.
      • Else if the first byte's four most significant bits are 1110, then three more bytes are read, and the least significant 28 bits are sign-extended to a long and returned.
      • Else if the first byte's five most significant bits are 11110, then four more bytes are read, and the least significant 35 bits are sign-extended to a long and returned.
      • Else if the first byte's six most significant bits are 111110, then five more bytes are read, and the least significant 42 bits are sign-extended to a long and returned.
      • Else if the first byte's seven most significant bits are 1111110, then six more bytes are read, and the least significant 49 bits are sign-extended to a long and returned.
      • Else if the first byte is 11111110, then seven more bytes are read, and the least significant 56 bits are sign-extended to a long and returned.
      • Else eight more bytes are read, and the least significant 64 bits are returned.
      Returns:
      Long value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readUnsignedLong

      public long readUnsignedLong() throws IOException
      Read an unsigned long value from this data input stream. From one to nine bytes are read, in big-endian order, as follows:
      • If the first byte's most significant bit is 0, then the least significant 7 bits are zero-extended to a long and returned.
      • Else if the first byte's two most significant bits are 10, then one more byte is read, and the least significant 14 bits are zero-extended to a long and returned.
      • Else if the first byte's three most significant bits are 110, then two more bytes are read, and the least significant 21 bits are zero-extended to a long and returned.
      • Else if the first byte's four most significant bits are 1110, then three more bytes are read, and the least significant 28 bits are zero-extended to a long and returned.
      • Else if the first byte's five most significant bits are 11110, then four more bytes are read, and the least significant 35 bits are zero-extended to a long and returned.
      • Else if the first byte's six most significant bits are 111110, then five more bytes are read, and the least significant 42 bits are zero-extended to a long and returned.
      • Else if the first byte's seven most significant bits are 1111110, then six more bytes are read, and the least significant 49 bits are zero-extended to a long and returned.
      • Else if the first byte is 11111110, then seven more bytes are read, and the least significant 56 bits are zero-extended to a long and returned.
      • Else eight more bytes are read, and the least significant 64 bits are returned.
      Returns:
      Long value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readFloat

      public float readFloat() throws IOException
      Read a float value from this data input stream. Four bytes are read in big-endian order yielding an int value v, then Float.intBitsToFloat(v) is returned.
      Returns:
      Float value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readDouble

      public double readDouble() throws IOException
      Read a double value from this data input stream. Eight bytes are read in big-endian order yielding a long value v, then Double.longBitsToDouble(v) is returned.
      Returns:
      Double value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • readString

      public String readString() throws IOException
      Read a string value from this data input stream. The length of the string is read using readUnsignedInt(), then each character of the string is read using readUnsignedInt().
      Returns:
      String value.
      Throws:
      EOFException - Thrown if the end of the stream is encountered.
      IOException - Thrown if an I/O error occurred.
      IOException - if any.