Package com.twelvemonkeys.io
Class LittleEndianDataInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- com.twelvemonkeys.io.LittleEndianDataInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.DataInput,java.lang.AutoCloseable
- Direct Known Subclasses:
CompoundDocument.SeekableLittleEndianDataInputStream
public class LittleEndianDataInputStream extends java.io.FilterInputStream implements java.io.DataInputA little endian input stream reads two's complement, little endian integers, floating point numbers, and characters and returns them as Java primitive types.The standard
java.io.DataInputStreamclass which this class imitates reads big endian quantities.Warning: The
DataInputandDataOutputinterfaces specifies big endian byte order in their documentation. This means that this class is, strictly speaking, not a proper implementation. However, I don't see a reason for the these interfaces to specify the byte order of their underlying representations.- Version:
- 2
- See Also:
LittleEndianRandomAccessFile,DataInputStream,DataInput,DataOutput
-
-
Constructor Summary
Constructors Constructor Description LittleEndianDataInputStream(java.io.InputStream pStream)Creates a new little endian input stream and chains it to the input stream specified by thepStreamargument.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanreadBoolean()Reads abooleanfrom the underlying input stream by reading a single byte.bytereadByte()Reads a signedbytefrom the underlying input stream with value between -128 and 127charreadChar()Reads a two byte Unicodecharfrom the underlying input stream in little endian order, low byte first.doublereadDouble()floatreadFloat()voidreadFully(byte[] pBytes)See the general contract of thereadFullymethod ofDataInput.voidreadFully(byte[] pBytes, int pOffset, int pLength)See the general contract of thereadFullymethod ofDataInput.intreadInt()Reads a four byte signedintfrom the underlying input stream in little endian order, low byte first.java.lang.StringreadLine()Deprecated.This method does not properly convert bytes to characters.longreadLong()Reads an eight byte signedintfrom the underlying input stream in little endian order, low byte first.shortreadShort()Reads a two byte signedshortfrom the underlying input stream in little endian order, low byte first.intreadUnsignedByte()Reads an unsignedbytefrom the underlying input stream with value between 0 and 255intreadUnsignedShort()Reads a two byte unsignedshortfrom the underlying input stream in little endian order, low byte first.java.lang.StringreadUTF()Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding.intskipBytes(int pLength)See the general contract of theskipBytesmethod ofDataInput.-
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
-
-
-
-
Constructor Detail
-
LittleEndianDataInputStream
public LittleEndianDataInputStream(java.io.InputStream pStream)
Creates a new little endian input stream and chains it to the input stream specified by thepStreamargument.- Parameters:
pStream- the underlying input stream.- See Also:
FilterInputStream.in
-
-
Method Detail
-
readBoolean
public boolean readBoolean() throws java.io.IOExceptionReads abooleanfrom the underlying input stream by reading a single byte. If the byte is zero, false is returned. If the byte is positive, true is returned.- Specified by:
readBooleanin interfacejava.io.DataInput- Returns:
- the
booleanvalue read. - Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readByte
public byte readByte() throws java.io.IOExceptionReads a signedbytefrom the underlying input stream with value between -128 and 127- Specified by:
readBytein interfacejava.io.DataInput- Returns:
- the
bytevalue read. - Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOExceptionReads an unsignedbytefrom the underlying input stream with value between 0 and 255- Specified by:
readUnsignedBytein interfacejava.io.DataInput- Returns:
- the
bytevalue read. - Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readShort
public short readShort() throws java.io.IOExceptionReads a two byte signedshortfrom the underlying input stream in little endian order, low byte first.- Specified by:
readShortin interfacejava.io.DataInput- Returns:
- the
shortread. - Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOExceptionReads a two byte unsignedshortfrom the underlying input stream in little endian order, low byte first.- Specified by:
readUnsignedShortin interfacejava.io.DataInput- Returns:
- the int value of the unsigned short read.
- Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readChar
public char readChar() throws java.io.IOExceptionReads a two byte Unicodecharfrom the underlying input stream in little endian order, low byte first.- Specified by:
readCharin interfacejava.io.DataInput- Returns:
- the int value of the unsigned short read.
- Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readInt
public int readInt() throws java.io.IOExceptionReads a four byte signedintfrom the underlying input stream in little endian order, low byte first.- Specified by:
readIntin interfacejava.io.DataInput- Returns:
- the
intread. - Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readLong
public long readLong() throws java.io.IOExceptionReads an eight byte signedintfrom the underlying input stream in little endian order, low byte first.- Specified by:
readLongin interfacejava.io.DataInput- Returns:
- the
intread. - Throws:
java.io.EOFException- if the end of the underlying input stream has been reachedjava.io.IOException- if the underlying stream throws an IOException.
-
readUTF
public java.lang.String readUTF() throws java.io.IOExceptionReads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding. This method first reads a two byte short in big endian order as required by the UTF-8 specification. This gives the number of bytes in the UTF-8 encoded version of the string. Next this many bytes are read and decoded as UTF-8 encoded characters.- Specified by:
readUTFin interfacejava.io.DataInput- Returns:
- the decoded string
- Throws:
java.io.UTFDataFormatException- if the string cannot be decodedjava.io.IOException- if the underlying stream throws an IOException.
-
readDouble
public final double readDouble() throws java.io.IOException- Specified by:
readDoublein interfacejava.io.DataInput- Returns:
- the next eight bytes of this input stream, interpreted as a
little endian
double. - Throws:
java.io.EOFException- if end of stream occurs before eight bytes have been read.java.io.IOException- if an I/O error occurs.
-
readFloat
public final float readFloat() throws java.io.IOException- Specified by:
readFloatin interfacejava.io.DataInput- Returns:
- the next four bytes of this input stream, interpreted as a
little endian
int. - Throws:
java.io.EOFException- if end of stream occurs before four bytes have been read.java.io.IOException- if an I/O error occurs.
-
skipBytes
public final int skipBytes(int pLength) throws java.io.IOExceptionSee the general contract of theskipBytesmethod ofDataInput.Bytes for this operation are read from the contained input stream.
- Specified by:
skipBytesin interfacejava.io.DataInput- Parameters:
pLength- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
java.io.IOException- if an I/O error occurs.
-
readFully
public final void readFully(byte[] pBytes) throws java.io.IOExceptionSee the general contract of thereadFullymethod ofDataInput.Bytes for this operation are read from the contained input stream.
- Specified by:
readFullyin interfacejava.io.DataInput- Parameters:
pBytes- the buffer into which the data is read.- Throws:
java.io.EOFException- if this input stream reaches the end before reading all the bytes.java.io.IOException- if an I/O error occurs.- See Also:
FilterInputStream.in
-
readFully
public final void readFully(byte[] pBytes, int pOffset, int pLength) throws java.io.IOExceptionSee the general contract of thereadFullymethod ofDataInput.Bytes for this operation are read from the contained input stream.
- Specified by:
readFullyin interfacejava.io.DataInput- Parameters:
pBytes- the buffer into which the data is read.pOffset- the start offset of the data.pLength- the number of bytes to read.- Throws:
java.io.EOFException- if this input stream reaches the end before reading all the bytes.java.io.IOException- if an I/O error occurs.- See Also:
FilterInputStream.in
-
readLine
@Deprecated public java.lang.String readLine() throws java.io.IOExceptionDeprecated.This method does not properly convert bytes to characters.See the general contract of thereadLinemethod ofDataInput.Bytes for this operation are read from the contained input stream.
- Specified by:
readLinein interfacejava.io.DataInput- Returns:
- the next line of text from this input stream.
- Throws:
java.io.IOException- if an I/O error occurs.- See Also:
BufferedReader.readLine(),DataInputStream.readLine()
-
-