Package com.twelvemonkeys.io
Class LittleEndianDataInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.twelvemonkeys.io.LittleEndianDataInputStream
- All Implemented Interfaces:
Closeable
,DataInput
,AutoCloseable
A 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.DataInputStream
class
which this class imitates reads big endian quantities.
Warning:
The DataInput
and DataOutput
interfaces
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
- Author:
- Elliotte Rusty Harold, Harald Kuhr
- See Also:
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsConstructorDescriptionLittleEndianDataInputStream
(InputStream pStream) Creates a new little endian input stream and chains it to the input stream specified by thepStream
argument. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Reads aboolean
from the underlying input stream by reading a single byte.byte
readByte()
Reads a signedbyte
from the underlying input stream with value between -128 and 127char
readChar()
Reads a two byte Unicodechar
from the underlying input stream in little endian order, low byte first.final double
final float
final void
readFully
(byte[] pBytes) See the general contract of thereadFully
method ofDataInput
.final void
readFully
(byte[] pBytes, int pOffset, int pLength) See the general contract of thereadFully
method ofDataInput
.int
readInt()
Reads a four byte signedint
from the underlying input stream in little endian order, low byte first.readLine()
Deprecated.This method does not properly convert bytes to characters.long
readLong()
Reads an eight byte signedint
from the underlying input stream in little endian order, low byte first.short
Reads a two byte signedshort
from the underlying input stream in little endian order, low byte first.int
Reads an unsignedbyte
from the underlying input stream with value between 0 and 255int
Reads a two byte unsignedshort
from the underlying input stream in little endian order, low byte first.readUTF()
Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding.final int
skipBytes
(int pLength) See the general contract of theskipBytes
method ofDataInput
.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
-
LittleEndianDataInputStream
Creates a new little endian input stream and chains it to the input stream specified by thepStream
argument.- Parameters:
pStream
- the underlying input stream.- See Also:
-
-
Method Details
-
readBoolean
Reads aboolean
from 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:
readBoolean
in interfaceDataInput
- Returns:
- the
boolean
value read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readByte
Reads a signedbyte
from the underlying input stream with value between -128 and 127- Specified by:
readByte
in interfaceDataInput
- Returns:
- the
byte
value read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readUnsignedByte
Reads an unsignedbyte
from the underlying input stream with value between 0 and 255- Specified by:
readUnsignedByte
in interfaceDataInput
- Returns:
- the
byte
value read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readShort
Reads a two byte signedshort
from the underlying input stream in little endian order, low byte first.- Specified by:
readShort
in interfaceDataInput
- Returns:
- the
short
read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readUnsignedShort
Reads a two byte unsignedshort
from the underlying input stream in little endian order, low byte first.- Specified by:
readUnsignedShort
in interfaceDataInput
- Returns:
- the int value of the unsigned short read.
- Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readChar
Reads a two byte Unicodechar
from the underlying input stream in little endian order, low byte first.- Specified by:
readChar
in interfaceDataInput
- Returns:
- the int value of the unsigned short read.
- Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readInt
Reads a four byte signedint
from the underlying input stream in little endian order, low byte first.- Specified by:
readInt
in interfaceDataInput
- Returns:
- the
int
read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readLong
Reads an eight byte signedint
from the underlying input stream in little endian order, low byte first.- Specified by:
readLong
in interfaceDataInput
- Returns:
- the
int
read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readUTF
Reads 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:
readUTF
in interfaceDataInput
- Returns:
- the decoded string
- Throws:
UTFDataFormatException
- if the string cannot be decodedIOException
- if the underlying stream throws an IOException.
-
readDouble
- Specified by:
readDouble
in interfaceDataInput
- Returns:
- the next eight bytes of this input stream, interpreted as a
little endian
double
. - Throws:
EOFException
- if end of stream occurs before eight bytes have been read.IOException
- if an I/O error occurs.
-
readFloat
- Specified by:
readFloat
in interfaceDataInput
- Returns:
- the next four bytes of this input stream, interpreted as a
little endian
int
. - Throws:
EOFException
- if end of stream occurs before four bytes have been read.IOException
- if an I/O error occurs.
-
skipBytes
See the general contract of theskipBytes
method ofDataInput
.Bytes for this operation are read from the contained input stream.
- Specified by:
skipBytes
in interfaceDataInput
- Parameters:
pLength
- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException
- if an I/O error occurs.
-
readFully
See the general contract of thereadFully
method ofDataInput
.Bytes for this operation are read from the contained input stream.
- Specified by:
readFully
in interfaceDataInput
- Parameters:
pBytes
- the buffer into which the data is read.- Throws:
EOFException
- if this input stream reaches the end before reading all the bytes.IOException
- if an I/O error occurs.- See Also:
-
readFully
See the general contract of thereadFully
method ofDataInput
.Bytes for this operation are read from the contained input stream.
- Specified by:
readFully
in interfaceDataInput
- 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:
EOFException
- if this input stream reaches the end before reading all the bytes.IOException
- if an I/O error occurs.- See Also:
-
readLine
Deprecated.This method does not properly convert bytes to characters.See the general contract of thereadLine
method ofDataInput
.Bytes for this operation are read from the contained input stream.
- Specified by:
readLine
in interfaceDataInput
- Returns:
- the next line of text from this input stream.
- Throws:
IOException
- if an I/O error occurs.- See Also:
-