org.at4j.support.io
Class LittleEndianBitInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.at4j.support.io.LittleEndianBitInputStream
All Implemented Interfaces:
Closeable, BitInput

public class LittleEndianBitInputStream
extends InputStream
implements BitInput

This is an input stream that a client can use to read single or several bits from an underlying InputStream. The bits are read in little-endian bit order.

Since:
1.1
Author:
Karl Gustafsson

Constructor Summary
LittleEndianBitInputStream(InputStream in)
           
 
Method Summary
 int available()
          Get the number of bytes available in the input.
 void close()
           
 long getNumberOfBytesRead()
          Get the number of whole bytes read this far.
 boolean isAtEof()
          Has the input come to its end? If so, nothing more can be read from it.
 int read()
          Read a single byte from the input.
 int read(byte[] barr)
          Read bytes into the supplied array.
 int read(byte[] barr, int offset, int len)
          Read bytes into the supplied array.
 boolean readBit()
          Read the value of the next bit in the stream.
 int readBits(int no)
          Read up to eight bits from the input.
 int readBitsLittleEndian(int no)
          Read up to 32 bits from the input.
 byte[] readBytes(byte[] barr, int off, int len)
          Read bytes from the input.
 long skip(long n)
          Skip bytes in the input.
 void skipToByteBoundary()
          Move the position to the next byte boundary.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LittleEndianBitInputStream

public LittleEndianBitInputStream(InputStream in)
                           throws IOException
Throws:
IOException
Method Detail

isAtEof

public boolean isAtEof()
Description copied from interface: BitInput
Has the input come to its end? If so, nothing more can be read from it.

Specified by:
isAtEof in interface BitInput
Returns:
true if no more can be read from this input.

getNumberOfBytesRead

public long getNumberOfBytesRead()
Get the number of whole bytes read this far.

Returns:
The number of bytes read this far.

skipToByteBoundary

public void skipToByteBoundary()
                        throws IOException
Description copied from interface: BitInput
Move the position to the next byte boundary. If the current position is already at a byte boundary, this method does nothing.

Specified by:
skipToByteBoundary in interface BitInput
Throws:
IOException - On I/O errors or if this input is already at the end of the available data.

readBit

public boolean readBit()
                throws IOException
Description copied from interface: BitInput
Read the value of the next bit in the stream.

Specified by:
readBit in interface BitInput
Returns:
true if the value is 1, false if it is 0.
Throws:
IOException - On I/O errors or if this input is already at the end of the available data.

readBits

public int readBits(int no)
             throws IOException,
                    IndexOutOfBoundsException
Description copied from interface: BitInput
Read up to eight bits from the input.

Specified by:
readBits in interface BitInput
Parameters:
no - The number of bits to read.
Returns:
The bits as the least significant bits of the returned integer. For instance, if 1011 is read, the returned integer will have the value 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 == 11.
Throws:
IOException - On I/O errors or if this input is already at the end of the available data.
IndexOutOfBoundsException - If no is less than 0 or greater than 8.
See Also:
BitInput.readBitsLittleEndian(int)

readBitsLittleEndian

public int readBitsLittleEndian(int no)
                         throws IOException,
                                IndexOutOfBoundsException
Description copied from interface: BitInput
Read up to 32 bits from the input. The first eight bits that is read will be the most significant byte of the returned integer.

Specified by:
readBitsLittleEndian in interface BitInput
Parameters:
no - The number of bits to read.
Returns:
The bits read as the least significant bits of the returned integer. (Just like for BitInput.readBits(int).
Throws:
IOException - On I/O errors or if this input is already at the end of the available data.
IndexOutOfBoundsException - If no is less than 0 or greater than 32.
See Also:
BitInput.readBits(int)

readBytes

public byte[] readBytes(byte[] barr,
                        int off,
                        int len)
                 throws IOException,
                        IndexOutOfBoundsException
Description copied from interface: BitInput
Read bytes from the input. Unlike BitInput.read(byte[], int, int), this method does not require that the current position is at a byte boundary.

Another difference to BitInput.read(byte[], int, int) is that this method throws an IOException if it cannot read all requested bytes.

Specified by:
readBytes in interface BitInput
Parameters:
barr - The byte array to read bytes into.
off - The offset in the array to start writing read bytes at.
len - The number of bytes to read.
Returns:
barr.
Throws:
IOException - On I/O errors or if there was not enough bytes to read from the input.
IndexOutOfBoundsException - If the length or the offset is negative or if the sum of the length and the offset is greater than the length of the supplied byte array.
See Also:
BitInput.read(byte[], int, int)

read

public int read()
         throws IOException
Description copied from interface: BitInput
Read a single byte from the input. See InputStream.read() .

This method requires that the current position in the input is at a byte boundary.

Specified by:
read in interface BitInput
Specified by:
read in class InputStream
Returns:
The read byte or -1 if the current position is at the end of the input.
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.
See Also:
InputStream.read()

read

public int read(byte[] barr)
         throws IOException
Description copied from interface: BitInput
Read bytes into the supplied array. See InputStream.read(byte[]).

This method requires that the current position in the input is at a byte boundary.

Specified by:
read in interface BitInput
Overrides:
read in class InputStream
Parameters:
barr - The byte array to read bytes into.
Returns:
The number of bytes read.
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.
See Also:
InputStream.read(byte[])

read

public int read(byte[] barr,
                int offset,
                int len)
         throws IndexOutOfBoundsException,
                IOException
Description copied from interface: BitInput
Read bytes into the supplied array. See InputStream.read(byte[], int, int).

This method requires that the current position in the input is at a byte boundary.

Specified by:
read in interface BitInput
Overrides:
read in class InputStream
Parameters:
barr - The byte array to read bytes into.
offset - The offset position in the array to start write read bytes to.
len - The number of bytes to read.
Returns:
The number of bytes actually read.
Throws:
IndexOutOfBoundsException - If the offset or the length is negative or if the sum of the offset and the length is greater than the length of the supplied byte array.
IOException - On I/O errors or if the current position is not at a byte boundary.

skip

public long skip(long n)
          throws IOException
Description copied from interface: BitInput
Skip bytes in the input. See InputStream.skip(long).

This method requires that the current position in the input is at a byte boundary.

Specified by:
skip in interface BitInput
Overrides:
skip in class InputStream
Parameters:
n - The number of bytes to skip.
Returns:
The number of bytes skipped.
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.

available

public int available()
              throws IOException
Description copied from interface: BitInput
Get the number of bytes available in the input. See InputStream.available().

This method requires that the current position in the input is at a byte boundary.

Specified by:
available in interface BitInput
Overrides:
available in class InputStream
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException