com.twelvemonkeys.io
Class LittleEndianDataInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by com.twelvemonkeys.io.LittleEndianDataInputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataInput

public class LittleEndianDataInputStream
extends java.io.FilterInputStream
implements java.io.DataInput

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:
LittleEndianRandomAccessFile, DataInputStream, DataInput, DataOutput

Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
LittleEndianDataInputStream(java.io.InputStream pStream)
          Creates a new little endian input stream and chains it to the input stream specified by the pStream argument.
 
Method Summary
 boolean readBoolean()
          Reads a boolean from the underlying input stream by reading a single byte.
 byte readByte()
          Reads a signed byte from the underlying input stream with value between -128 and 127
 char readChar()
          Reads a two byte Unicode char from the underlying input stream in little endian order, low byte first.
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] pBytes)
          See the general contract of the readFully method of DataInput.
 void readFully(byte[] pBytes, int pOffset, int pLength)
          See the general contract of the readFully method of DataInput.
 int readInt()
          Reads a four byte signed int from the underlying input stream in little endian order, low byte first.
 java.lang.String readLine()
          Deprecated. This method does not properly convert bytes to characters.
 long readLong()
          Reads an eight byte signed int from the underlying input stream in little endian order, low byte first.
 short readShort()
          Reads a two byte signed short from the underlying input stream in little endian order, low byte first.
 int readUnsignedByte()
          Reads an unsigned byte from the underlying input stream with value between 0 and 255
 int readUnsignedShort()
          Reads a two byte unsigned short from the underlying input stream in little endian order, low byte first.
 java.lang.String readUTF()
          Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding.
 int skipBytes(int pLength)
          See the general contract of the skipBytes method of DataInput.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 the pStream argument.

Parameters:
pStream - the underlying input stream.
See Also:
FilterInputStream.in
Method Detail

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Reads a boolean 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 interface java.io.DataInput
Returns:
the boolean value read.
Throws:
java.io.EOFException - if the end of the underlying input stream has been reached
java.io.IOException - if the underlying stream throws an IOException.

readByte

public byte readByte()
              throws java.io.IOException
Reads a signed byte from the underlying input stream with value between -128 and 127

Specified by:
readByte in interface java.io.DataInput
Returns:
the byte value read.
Throws:
java.io.EOFException - if the end of the underlying input stream has been reached
java.io.IOException - if the underlying stream throws an IOException.

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Reads an unsigned byte from the underlying input stream with value between 0 and 255

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the byte value read.
Throws:
java.io.EOFException - if the end of the underlying input stream has been reached
java.io.IOException - if the underlying stream throws an IOException.

readShort

public short readShort()
                throws java.io.IOException
Reads a two byte signed short from the underlying input stream in little endian order, low byte first.

Specified by:
readShort in interface java.io.DataInput
Returns:
the short read.
Throws:
java.io.EOFException - if the end of the underlying input stream has been reached
java.io.IOException - if the underlying stream throws an IOException.

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Reads a two byte unsigned short from the underlying input stream in little endian order, low byte first.

Specified by:
readUnsignedShort in interface java.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 reached
java.io.IOException - if the underlying stream throws an IOException.

readChar

public char readChar()
              throws java.io.IOException
Reads a two byte Unicode char from the underlying input stream in little endian order, low byte first.

Specified by:
readChar in interface java.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 reached
java.io.IOException - if the underlying stream throws an IOException.

readInt

public int readInt()
            throws java.io.IOException
Reads a four byte signed int from the underlying input stream in little endian order, low byte first.

Specified by:
readInt in interface java.io.DataInput
Returns:
the int read.
Throws:
java.io.EOFException - if the end of the underlying input stream has been reached
java.io.IOException - if the underlying stream throws an IOException.

readLong

public long readLong()
              throws java.io.IOException
Reads an eight byte signed int from the underlying input stream in little endian order, low byte first.

Specified by:
readLong in interface java.io.DataInput
Returns:
the int read.
Throws:
java.io.EOFException - if the end of the underlying input stream has been reached
java.io.IOException - if the underlying stream throws an IOException.

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
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 interface java.io.DataInput
Returns:
the decoded string
Throws:
java.io.UTFDataFormatException - if the string cannot be decoded
java.io.IOException - if the underlying stream throws an IOException.

readDouble

public final double readDouble()
                        throws java.io.IOException
Specified by:
readDouble in interface java.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:
readFloat in interface java.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.IOException
See the general contract of the skipBytes method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
skipBytes in interface java.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.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFully in interface java.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.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFully in interface java.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

public java.lang.String readLine()
                          throws java.io.IOException
Deprecated. This method does not properly convert bytes to characters.

See the general contract of the readLine method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readLine in interface java.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()


Copyright © 2014. All Rights Reserved.