Class BitInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by BitInputStream
All Implemented Interfaces:
java.io.Closeable

public class BitInputStream
extends java.io.InputStream

The BitOutputStream and BitInputStream classes provide the ability to write and read individual bits to a file in a compact form. One minor limitation of this approach is that the resulting file will always have a number of bits that is a multiple of 8. In effect, whatever bits are output to the file are padded at the end with 0's to make the total number of bits a multiple of 8.


Constructor Summary
BitInputStream(java.io.InputStream input)
          Creates a BitInputStream reading bits of input from the given stream source.
BitInputStream(java.io.InputStream input, boolean bitMode)
          Creates a BitInputStream reading bits/bytes input from the given stream source.
 
Method Summary
 void close()
          Closes this stream for reading.
protected  void finalize()
          Called by Java when the program is shutting down; included to help ensure that the stream is closed.
 boolean hasNextBit()
          Returns whether this stream has more bits available to be read.
 boolean inBitMode()
          Returns whether this stream is in real 'bit mode', reading a bit from the file for each call to readBit.
 int read()
          Reads and returns a single byte of information from this stream.
 int readBit()
          Reads and returns the next single bit of input from this stream.
 void setBitMode(boolean bitMode)
          Sets whether this stream is in real 'bit mode', reading a bit from the file for each call to readBit (as described under inBitMode).
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitInputStream

public BitInputStream(java.io.InputStream input)
               throws java.io.IOException
Creates a BitInputStream reading bits of input from the given stream source.

Parameters:
input - the input stream to read.
Throws:
java.io.IOException - if the input stream cannot be accessed.

BitInputStream

public BitInputStream(java.io.InputStream input,
                      boolean bitMode)
               throws java.io.IOException
Creates a BitInputStream reading bits/bytes input from the given stream source.

Parameters:
input - the input stream to read.
bitMode - true to write bits at a time; false to write ASCII characters (bytes) at a time for debugging.
Throws:
java.io.IOException - if the input stream cannot be accessed.
Method Detail

close

public void close()
           throws java.io.IOException
Closes this stream for reading.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - if the input stream cannot be closed.

hasNextBit

public boolean hasNextBit()
Returns whether this stream has more bits available to be read.

Returns:
true if more bits are available, otherwise false.

inBitMode

public boolean inBitMode()
Returns whether this stream is in real 'bit mode', reading a bit from the file for each call to readBit. The alternative is 'byte mode', where a full byte (character) is read from the file each time readBit is called, to make it easier to debug your program.

Returns:
true if in bit mode, false if in byte mode.

read

public int read()
         throws java.io.IOException
Reads and returns a single byte of information from this stream.

Specified by:
read in class java.io.InputStream
Returns:
the byte of information read, or -1 if no bytes remain to read.
Throws:
java.io.IOException - if the input stream cannot be read.

readBit

public int readBit()
            throws java.io.IOException
Reads and returns the next single bit of input from this stream.

Returns:
the bit of information read, or -1 if no bits remain to read.
Throws:
java.io.IOException - if the input stream cannot be read.

setBitMode

public void setBitMode(boolean bitMode)
Sets whether this stream is in real 'bit mode', reading a bit from the file for each call to readBit (as described under inBitMode).

Parameters:
bitMode - true to use bit mode, false to use byte mode.

finalize

protected void finalize()
Called by Java when the program is shutting down; included to help ensure that the stream is closed.

Overrides:
finalize in class java.lang.Object