Class BitInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byBitInputStream

public class BitInputStream
extends java.io.InputStream

This class is a buffered input stream that allows for users to easily read bits and bytes from the underlying stream. All methods use computer arithmetic to maipulate the bits, and have a default buffer of 64 bits (long).

 Usage: Identical to the BufferedInputStream.
        ie.  BitInputStream out = new BitInputStream(new FileInputStream(file));
 


Field Summary
protected  long buf
          The 'long' buffer that stores the bits.
protected  int bufLength
          A 'long' is represented by 64 bits, thus the length of the buffer is 64 bits.
protected  long[] byteLReduce
          The reduce arrays used to reduce large values.
protected  int count
          The current location in the buffer.
protected  int curBufLength
          A 'long' is represented by 64 bits, thus the length of the buffer is 64 bits.
protected  long[] longLReduce
          The reduce arrays used to reduce large values.
protected  long[] outReduce
          The reduce arrays used to reduce large values.
 
Constructor Summary
BitInputStream(java.io.InputStream is)
           
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 long availableBits()
          Returns the number of bits that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] b)
          Reads some number of bytes from the input stream and stores them into the buffer array b.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes.
 boolean readBit()
          Reads a single bit from the input stream, which is returned.
 int readBits(boolean[] b, int off, int len)
          Reads a series of bits from the input stream, which will then be written to the boolean array with the specified offset and for the specified length.
 int readByte()
          Reads the next UNSIGNED byte of data from the input stream.
 int readBytes(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes.
 int readInt()
          Reads an int value from the input stream, which is 4 bytes.
 long readLong()
          Reads a long value from the input stream, which is 8 bytes.
 java.lang.String readString(int len)
          Reads a String from the input stream given the expected length of the String to read.
 long skip(long n)
          Skips over and discards n bytes of data from this input stream INCLUDING any half bits that are currently being read in the buffer.
 long skipBits(long n)
           
 java.lang.String toString()
          Returns a String representation of the state of the current buffer in binary form, as well as any variables associated with the buffer.
 
Methods inherited from class java.io.InputStream
mark, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected long buf
The 'long' buffer that stores the bits.


bufLength

protected int bufLength
A 'long' is represented by 64 bits, thus the length of the buffer is 64 bits.


curBufLength

protected int curBufLength
A 'long' is represented by 64 bits, thus the length of the buffer is 64 bits.


count

protected int count
The current location in the buffer.


byteLReduce

protected long[] byteLReduce
The reduce arrays used to reduce large values.


longLReduce

protected long[] longLReduce
The reduce arrays used to reduce large values.


outReduce

protected long[] outReduce
The reduce arrays used to reduce large values.

Constructor Detail

BitInputStream

public BitInputStream(java.io.InputStream is)
               throws java.io.IOException
Method Detail

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods.


available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. HALF-BYTES are NOT counted. ie. 9 bits -> 1 byte available.

Throws:
java.io.IOException

availableBits

public long availableBits()
                   throws java.io.IOException
Returns the number of bits that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream.

Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data from this input stream INCLUDING any half bits that are currently being read in the buffer.

Throws:
java.io.IOException

skipBits

public long skipBits(long n)
              throws java.io.IOException
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Reads the next byte of data from the input stream.

Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Reads some number of bytes from the input stream and stores them into the buffer array b.

Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from the input stream into an array of bytes.

Throws:
java.io.IOException

readByte

public int readByte()
             throws java.io.IOException
Reads the next UNSIGNED byte of data from the input stream.

Throws:
java.io.IOException

readBytes

public int readBytes(byte[] b,
                     int off,
                     int len)
              throws java.io.IOException
Reads up to len bytes of data from the input stream into an array of bytes.

Throws:
java.io.IOException

readBit

public boolean readBit()
                throws java.io.IOException
Reads a single bit from the input stream, which is returned.

Throws:
java.io.IOException

readBits

public int readBits(boolean[] b,
                    int off,
                    int len)
             throws java.io.IOException
Reads a series of bits from the input stream, which will then be written to the boolean array with the specified offset and for the specified length. It returns the number of bits read.

Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Reads an int value from the input stream, which is 4 bytes.

Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Reads a long value from the input stream, which is 8 bytes.

Throws:
java.io.IOException

readString

public java.lang.String readString(int len)
                            throws java.io.IOException
Reads a String from the input stream given the expected length of the String to read.

Parameters:
len - the length (in bytes) of the String to be read, including the current (possibly half) byte.
Throws:
java.io.IOException

toString

public java.lang.String toString()
Returns a String representation of the state of the current buffer in binary form, as well as any variables associated with the buffer.