Package edu.rit.io
Class DataInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
edu.rit.io.DataInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
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
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
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
Read a double value from this data input stream.float
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
Read a short value from this data input stream.Read a string value from this data input stream.byte
Read an unsigned byte value from this data input stream.char
Read an unsigned character value from this data input stream.int
Read an unsigned integer value from this data input stream.long
Read an unsigned long value from this data input stream.short
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.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
DataInputStream
Construct a new data input stream.- Parameters:
in
- Underlying input stream.
-
-
Method Details
-
readBoolean
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
Read a byte value from this data input stream. An int is read usingreadInt()
, 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
Read a short value from this data input stream. An int is read usingreadInt()
, 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
Read a character value from this data input stream. An int is read usingreadInt()
, 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
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
Read an unsigned byte value from this data input stream. An int is read usingreadUnsignedInt()
, 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
Read an unsigned short value from this data input stream. An int is read usingreadUnsignedInt()
, 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
Read an unsigned character value from this data input stream. An int is read usingreadUnsignedInt()
, 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
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
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
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
Read a float value from this data input stream. Four bytes are read in big-endian order yielding an int valuev
, thenFloat.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
Read a double value from this data input stream. Eight bytes are read in big-endian order yielding a long valuev
, thenDouble.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
Read a string value from this data input stream. The length of the string is read usingreadUnsignedInt()
, then each character of the string is read usingreadUnsignedInt()
.- 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.
-