src
Class BitInputStream

java.lang.Object
  extended by src.BitInputStream

public class BitInputStream
extends java.lang.Object

The BitInputStream allows reading individual bits from a general Java InputStream. This class is able to read a single bit from a stream It is also able to read Serializable objects.

The code is based on the following reference:

http://www.developer.nokia.com/Community/Wiki/Bit_Input/Output_Stream_utility_classes_for_efficient_data_transfer

The original author is Andreas Jakl. The code has been modified to include support for partial bytes at the end of the stream and to allow reading of serializable objects.


Constructor Summary
BitInputStream(java.io.InputStream in)
          Creates a BitInputStream that reads bits from the specified InputStream.
BitInputStream(java.lang.String name)
          Creates a BitInputStream that reads bits from the file with the given name.
 
Method Summary
 void close()
          Closes the underlying input stream.
 boolean hasNext()
          Returns true if there are more bits in this stream.
 int readBit()
          Reads the next bit from this stream.
 java.lang.Object readObject()
          Reads the next object from this stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitInputStream

public BitInputStream(java.io.InputStream in)
               throws java.io.IOException
Creates a BitInputStream that reads bits from the specified InputStream.

Parameters:
in - the input stream to read from
Throws:
java.io.IOException - - if an I/O error occurs while writing stream header
java.lang.SecurityException - - if untrusted subclass illegally overrides security-sensitive methods
java.lang.NullPointerException - - if in is null

BitInputStream

public BitInputStream(java.lang.String name)
               throws java.io.IOException
Creates a BitInputStream that reads bits from the file with the given name.

Parameters:
name - the name of the file to read the bits from
Throws:
java.io.IOException - if an I/O error occurs
java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
Method Detail

hasNext

public boolean hasNext()
Returns true if there are more bits in this stream. After a call to hasNext the method readObject should not be called.

Returns:
true, if there are more bits in this stream

readBit

public int readBit()
            throws java.io.IOException
Reads the next bit from this stream.

Returns:
0 if the bit is 0, 1 if the bit is 1
Throws:
java.io.IOException - if an I/O error occurs, or if attempting to read past the end of file

readObject

public java.lang.Object readObject()
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException
Reads the next object from this stream.

Returns:
the object read from this stream
Throws:
java.io.IOException - if an I/O error occurs
java.lang.ClassNotFoundException
See Also:
ObjectInputStream.readObject()

close

public void close()
           throws java.io.IOException
Closes the underlying input stream.

Throws:
java.io.IOException - if an I/O error occurs