Class BitOutputStream

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

public class BitOutputStream
extends java.io.OutputStream

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 0's to make the total number of bits a multiple of 8.


Field Summary
static int BYTE_SIZE
           
static int EOF
           
 
Constructor Summary
BitOutputStream(java.io.OutputStream output)
          Creates a BitOutputStream to send output to the stream in 'bit mode'.
BitOutputStream(java.io.OutputStream output, boolean bitMode)
          Creates a BitOutputStream to send output to the stream in the given mode.
 
Method Summary
 void close()
          Closes this output stream for writing and flushes any data to be written.
protected  void finalize()
          Runs when the object is garbage collected / program shuts down.
 void flush()
          Flushes the buffer.
 boolean inBitMode()
          Returns whether this stream is in real 'bit mode', writing a bit to the file for each call to writeBit.
 void setBitMode(boolean bitMode)
          Sets whether this stream is in real 'bit mode', writing a bit to the file for each 0 or 1 written in writeBit (as described under inBitMode).
 void write(int b)
           
 void writeBit(int bit)
          Writes given bit to output.
 void writeBits(java.lang.String bits)
          Writes every bit in the given string of 0s and 1s.
 void writeBytes(java.lang.String bytes)
          Writes every character in the given string of 0s and 1s as a byte.
 
Methods inherited from class java.io.OutputStream
write, write
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTE_SIZE

public static final int BYTE_SIZE
See Also:
Constant Field Values

EOF

public static final int EOF
See Also:
Constant Field Values
Constructor Detail

BitOutputStream

public BitOutputStream(java.io.OutputStream output)
Creates a BitOutputStream to send output to the stream in 'bit mode'. Precondition: The given file is legal and can be written.

Parameters:
output - the target output stream to write.
Throws:
java.lang.RuntimeException - if the file cannot be opened for writing.

BitOutputStream

public BitOutputStream(java.io.OutputStream output,
                       boolean bitMode)
Creates a BitOutputStream to send output to the stream in the given mode. Precondition: The given file is legal and can be written.

Parameters:
output - the target output stream to write.
bitMode - true to write bits at a time; false to write ASCII characters (bytes) at a time for debugging.
Throws:
java.lang.RuntimeException - if the file cannot be opened for writing.
Method Detail

close

public void close()
           throws java.io.IOException
Closes this output stream for writing and flushes any data to be written.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - if the file cannot be closed.

flush

public void flush()
           throws java.io.IOException
Flushes the buffer. If numDigits < BYTE_SIZE, this will effectively pad the output with extra 0s, so this should be called only when numDigits == BYTE_SIZE or when we are closing the output.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if the bits cannot be written.

inBitMode

public boolean inBitMode()
Returns whether this stream is in real 'bit mode', writing a bit to the file for each call to writeBit. The alternative is 'byte mode', where a full byte (ASCII character) of '0' or '1' is written for each call to writeBit, to make it easier to debug your program.

Returns:
true if in bit mode, false if in byte mode.

setBitMode

public void setBitMode(boolean bitMode)
Sets whether this stream is in real 'bit mode', writing a bit to the file for each 0 or 1 written in writeBit (as described under inBitMode).

Parameters:
bitMode - true to use bit mode, false to use character mode.

write

public void write(int b)
           throws java.io.IOException
Specified by:
write in class java.io.OutputStream
Throws:
java.io.IOException

writeBit

public void writeBit(int bit)
              throws java.io.IOException
Writes given bit to output.

Parameters:
bit - the bit value to write.
Throws:
java.lang.IllegalArgumentException - if bit is not 0 or 1
java.io.IOException - if the bit cannot be written.

writeBits

public void writeBits(java.lang.String bits)
               throws java.io.IOException
Writes every bit in the given string of 0s and 1s.

Parameters:
bits - A string entirely of 0s and 1s, such as "01001100110".
Throws:
java.lang.IllegalArgumentException - if the string contains any characters other than '0' or '1'.
java.io.IOException

writeBytes

public void writeBytes(java.lang.String bytes)
                throws java.io.IOException
Writes every character in the given string of 0s and 1s as a byte.

Parameters:
bytes - A string entirely of 0s and 1s, such as "01001100110".
Throws:
java.lang.IllegalArgumentException - if the string contains any characters other than '0' or '1'.
java.io.IOException

finalize

protected void finalize()
Runs when the object is garbage collected / program shuts down. Included to ensure that the stream is closed.

Overrides:
finalize in class java.lang.Object