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 0s 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, BitOutputStream partner, boolean bitMode)
          Creates a BitInputStream reading bits/bytes input from the given stream source and partnered with the given bit output stream.
BitInputStream(java.io.InputStream input, boolean bitMode)
          Creates a BitInputStream reading bits/bytes input from the given stream source.
 
Method Summary
 int available()
          Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
 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 read(byte[] bytes)
          Reads and returns some bytes of information from this stream into the given array.
 int read(byte[] bytes, int offset, int length)
          Reads and returns some bytes of information from this stream into the given array.
 int readBit()
          Reads and returns the next single bit of input from this stream.
 java.lang.String readLine()
          Reads and returns an entire line of text from this bit input stream as a String.
 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
mark, markSupported, 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)
Creates a BitInputStream reading bits of input from the given stream source.

Parameters:
input - the input stream to read.
Throws:
BitIOException - if the input stream cannot be accessed.

BitInputStream

public BitInputStream(java.io.InputStream input,
                      boolean bitMode)
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:
BitIOException - if the input stream cannot be accessed.

BitInputStream

public BitInputStream(java.io.InputStream input,
                      BitOutputStream partner,
                      boolean bitMode)
Creates a BitInputStream reading bits/bytes input from the given stream source and partnered with the given bit output stream.

Parameters:
input - the input stream to read.
partner - a "partner" bit output stream to watch for EOF.
bitMode - true to write bits at a time; false to write ASCII characters (bytes) at a time for debugging.
Throws:
BitIOException - if the input stream cannot be accessed.
java.lang.NullPointerException - if the given input or output stream is null. (The partner can be null; simply won't partner with any bit output stream in such a case.)
Method Detail

available

public int available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

Overrides:
available in class java.io.InputStream
Returns:
an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, or 0 when it reaches the end of the input stream.
Throws:
BitIOException - if the input stream cannot be accessed.

close

public void close()
Closes this stream for reading.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
BitIOException - 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()
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:
BitIOException - if the input stream cannot be read.

read

public int read(byte[] bytes)
Reads and returns some bytes of information from this stream into the given array.

Overrides:
read in class java.io.InputStream
Parameters:
bytes - array of byte to fill
Returns:
the number of bytes read, or -1 if no bytes remain to read.
Throws:
BitIOException - if the input stream cannot be read.
java.lang.NullPointerException - if bytes is null.

read

public int read(byte[] bytes,
                int offset,
                int length)
Reads and returns some bytes of information from this stream into the given array.

Overrides:
read in class java.io.InputStream
Parameters:
bytes - array of byte to fill
Returns:
the number of bytes read, or -1 if no bytes remain to read.
Throws:
BitIOException - if the input stream cannot be read.
java.lang.NullPointerException - if bytes is null.
java.lang.ArrayIndexOutOfBoundsException - if offset + length is past the end of the array.

readBit

public int readBit()
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:
BitIOException - if the input stream cannot be read.

readLine

public java.lang.String readLine()
Reads and returns an entire line of text from this bit input stream as a String. You would not normally want to call this method while you're reading bits from a bit input stream; it is provided as a convenience for students doing extra credit functionality in the assignment.

Returns:
the line read; an empty string "" if no input remains.
Throws:
BitIOException - 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). Ignores the caller and always uses 'byte' mode if reading from System.in or if the JVM bitstream.bitmode environment variable is set.

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