assignment4
Class BitInputStream

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

public class BitInputStream
extends java.io.InputStream

Reads bits-at-a-time where the number of bits is between 1 and 32. Updated for version 2.0 to extend java.io.InputStream. This class can be used together with BitOutputStream to facilitate reading and writing data several bits-at-a-time. BitInputStream objects that are constructed from a File support reset(). However, if constructed from an InputStream an object cannot be reset.

Any exceptions generated are rethrown as RuntimeException objects so client code does not have to catch or rethrow them. (Unless the extension of InputStream requires throwing as another type of exception, e.g., as with method read.


Constructor Summary
BitInputStream(java.io.File file)
          Construct a bit-at-a-time input stream from file.
BitInputStream(java.io.InputStream in)
          Open a bit-at-a-time stream that reads from supplied InputStream.
BitInputStream(java.lang.String filename)
          Construct a bit-at-a-time input stream from a file whose name is supplied.
 
Method Summary
 void close()
          Closes the input stream.
 boolean markSupported()
          Return true if the stream has been initialized from a File and is thus reset-able.
 int read()
          Required by classes extending InputStream, returns the next byte from this stream as an int value.
 int readBits(int howManyBits)
          Returns the number of bits requested as rightmost bits in returned value, returns -1 if not enough bits available to satisfy the request.
 void reset()
          Reset stream to beginning.
 
Methods inherited from class java.io.InputStream
available, mark, read, read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitInputStream

public BitInputStream(java.lang.String filename)
Construct a bit-at-a-time input stream from a file whose name is supplied.

Parameters:
filename - is the name of the file that will be read.
Throws:
java.lang.RuntimeException - if filename cannot be opened.

BitInputStream

public BitInputStream(java.io.File file)
Construct a bit-at-a-time input stream from file.

Parameters:
file - is the File that is the source of the input
Throws:
RuntimeExceptoin - if file cannot be opened.

BitInputStream

public BitInputStream(java.io.InputStream in)
Open a bit-at-a-time stream that reads from supplied InputStream. If this constructor is used the BitInputStream is not reset-able.

Parameters:
in - is the stream from which bits are read.
Method Detail

markSupported

public boolean markSupported()
Return true if the stream has been initialized from a File and is thus reset-able. If constructed from an InputStream it is not reset-able.

Overrides:
markSupported in class java.io.InputStream
Returns:
true if stream can be reset (it has been constructed appropriately from a File).

reset

public void reset()
           throws java.io.IOException
Reset stream to beginning. The implementation creates a new stream.

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - if not reset-able (e.g., constructed from InputStream).

close

public void close()
Closes the input stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.lang.RuntimeException - if the close fails

readBits

public int readBits(int howManyBits)
             throws java.io.IOException
Returns the number of bits requested as rightmost bits in returned value, returns -1 if not enough bits available to satisfy the request.

Parameters:
howManyBits - is the number of bits to read and return
Returns:
the value read, only rightmost howManyBits are valid, returns -1 if not enough bits left
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Required by classes extending InputStream, returns the next byte from this stream as an int value.

Specified by:
read in class java.io.InputStream
Returns:
the next byte from this stream
Throws:
java.io.IOException