src
Class BitOutputStream

java.lang.Object
  extended by src.BitOutputStream

public class BitOutputStream
extends java.lang.Object

The BitOutputStream allows writing individual bits to a general Java OutputStream. This class is able to write a single bit to a stream (even though a byte has to be filled until the data is flushed to the underlying output stream). It is also able to write 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 writing of serializable objects.


Constructor Summary
BitOutputStream(java.io.OutputStream out)
          Creates a BitOutputStream that writes bits to the specified OutputStream.
BitOutputStream(java.lang.String name)
          Creates a BitOutputStream stream that writes bits to the file with the given name.
 
Method Summary
 void close()
          Flushes the data and closes the underlying output stream.
 void writeBit(int bit)
          Writes a single bit to the stream.
 void writeObject(java.lang.Object obj)
          Writes an object to this stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitOutputStream

public BitOutputStream(java.io.OutputStream out)
                throws java.io.IOException
Creates a BitOutputStream that writes bits to the specified OutputStream.

Parameters:
out - the output stream to write to
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 out is null

BitOutputStream

public BitOutputStream(java.lang.String name)
                throws java.io.IOException
Creates a BitOutputStream stream that writes bits to the file with the given name.

Parameters:
name - the name of the file to write the bits to
Throws:
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
java.io.IOException
Method Detail

writeBit

public void writeBit(int bit)
              throws java.io.IOException
Writes a single bit to the stream. It will only be flushed to the underlying OutputStream when a byte has been completed (or when this stream is closed).

Parameters:
bit - the next bit to write (either 1 or 0)
Throws:
java.io.IOException - if an I/O error occurs

writeObject

public void writeObject(java.lang.Object obj)
                 throws java.io.IOException
Writes an object to this stream.

Parameters:
obj - the object to be written to this stream
Throws:
java.io.IOException - if an I/O error occurs

close

public void close()
           throws java.io.IOException
Flushes the data and closes the underlying output stream.

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