edu.rit.io
Class DataInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- edu.rit.io.DataInputStream
-
- All Implemented Interfaces:
- java.io.Closeable, java.lang.AutoCloseable
public class DataInputStream extends java.io.FilterInputStreamClass 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 booleanreadBoolean()Read a Boolean value from this data input stream.bytereadByte()Read a byte value from this data input stream.charreadChar()Read a character value from this data input stream.doublereadDouble()Read a double value from this data input stream.floatreadFloat()Read a float value from this data input stream.intreadInt()Read an integer value from this data input stream.longreadLong()Read a long value from this data input stream.shortreadShort()Read a short value from this data input stream.java.lang.StringreadString()Read a string value from this data input stream.bytereadUnsignedByte()Read an unsigned byte value from this data input stream.charreadUnsignedChar()Read an unsigned character value from this data input stream.intreadUnsignedInt()Read an unsigned integer value from this data input stream.longreadUnsignedLong()Read an unsigned long value from this data input stream.shortreadUnsignedShort()Read an unsigned short value from this data input stream.
-
-
-
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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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.IOExceptionRead 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