assignment4
Class BitOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by assignment4.BitOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class BitOutputStream
extends java.io.OutputStream

Write bits-at-a-time where the number of bits is between 1 and 32. Client programs must call flush or close when finished writing or not all bits will be written. This class is intended to be used with BitInputStream to facilitate reading and writing data in a bits-at-a-time manner.

Updated for version 2.0 to extend java.io.OutputStream

Any exceptions generated are rethrown as RuntimeException objects so client code does not have to catch or rethrow them.


Constructor Summary
BitOutputStream(java.io.OutputStream out)
          Create a stream that writes-through to the OutputStream object passed as a parameter.
BitOutputStream(java.lang.String filename)
          Construct a bit-at-a-time output stream with specified file name.
 
Method Summary
 void close()
          Releases system resources associated with file and flushes bits not yet written.
 void flush()
          Flushes bits not yet written, must be called by client programs if close isn't called.
 void write(int b)
          Required by OutputStream subclasses, write the low 8-bits to the underlying outputstream
 void writeBits(int howManyBits, int value)
          Write specified number of bits from value to a file.
 
Methods inherited from class java.io.OutputStream
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitOutputStream

public BitOutputStream(java.io.OutputStream out)
Create a stream that writes-through to the OutputStream object passed as a parameter.

Parameters:
out - is the output stream to which bits are written

BitOutputStream

public BitOutputStream(java.lang.String filename)
Construct a bit-at-a-time output stream with specified file name.

Parameters:
filename - is the name of the file being written
Throws:
java.lang.RuntimeException - if opening file fails for either FileNotFound or for Security exceptoins
Method Detail

write

public void write(int b)
           throws java.io.IOException
Required by OutputStream subclasses, write the low 8-bits to the underlying outputstream

Specified by:
write in class java.io.OutputStream
Throws:
java.io.IOException

flush

public void flush()
Flushes bits not yet written, must be called by client programs if close isn't called.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.lang.RuntimeException - if there's a problem writing bits

close

public void close()
Releases system resources associated with file and flushes bits not yet written. Either this function or flush must be called or not all bits will be written

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

writeBits

public void writeBits(int howManyBits,
                      int value)
Write specified number of bits from value to a file.

Parameters:
howManyBits - is number of bits to write (1-32)
value - is source of bits, rightmost bits are written
Throws:
java.lang.RuntimeException - if there's an I/O problem writing bits