Documentation of 'edu.rit.io.DataInputStream' Java class
DataInputStream
edu.rit.io

Class DataInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable


    public class DataInputStream
    extends java.io.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.

    • Constructor Summary

      Constructors 
      Constructor and Description
      DataInputStream(java.io.InputStream in)
      Construct a new data input stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      boolean readBoolean()
      Read a Boolean value from this data input stream.
      byte readByte()
      Read a byte value from this data input stream.
      char readChar()
      Read a character value from this data input stream.
      double readDouble()
      Read a double value from this data input stream.
      float readFloat()
      Read a float value from this data input stream.
      int readInt()
      Read an integer value from this data input stream.
      long readLong()
      Read a long value from this data input stream.
      short readShort()
      Read a short value from this data input stream.
      java.lang.String readString()
      Read a string value from this data input stream.
      byte readUnsignedByte()
      Read an unsigned byte value from this data input stream.
      char readUnsignedChar()
      Read an unsigned character value from this data input stream.
      int readUnsignedInt()
      Read an unsigned integer value from this data input stream.
      long readUnsignedLong()
      Read an unsigned long value from this data input stream.
      short readUnsignedShort()
      Read an unsigned short value from this data input stream.
      • Methods inherited from class java.io.FilterInputStream

        available, close, mark, markSupported, read, read, read, reset, skip
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DataInputStream

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

      • readBoolean

        public boolean readBoolean()
                            throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readByte

        public byte readByte()
                      throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readShort

        public short readShort()
                        throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readChar

        public char readChar()
                      throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readInt

        public int readInt()
                    throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readUnsignedByte

        public byte readUnsignedByte()
                              throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readUnsignedShort

        public short readUnsignedShort()
                                throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readUnsignedChar

        public char readUnsignedChar()
                              throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readUnsignedInt

        public int readUnsignedInt()
                            throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readLong

        public long readLong()
                      throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readUnsignedLong

        public long readUnsignedLong()
                              throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readFloat

        public float readFloat()
                        throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readDouble

        public double readDouble()
                          throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.
      • readString

        public java.lang.String readString()
                                    throws java.io.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:
        java.io.EOFException - Thrown if the end of the stream is encountered.
        java.io.IOException - Thrown if an I/O error occurred.

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.