Class BitOutputStream

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

public class BitOutputStream
extends java.io.PrintStream

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


 
Field Summary
static int BYTE_SIZE
           
static boolean DEBUG
           
static char EOF
           
 
Fields inherited from class java.io.FilterOutputStream
out
 
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 hasSeenEOF()
          Returns true if this output stream has ever written an EOF character; students should not call this method.
 boolean inBitMode()
          Returns whether this stream is in real 'bit mode', writing a bit to the file for each call to writeBit.
 void print(char c)
          Prints the given character to this output stream.
 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 setEOFEncoding(java.lang.String eofEncoding)
          Sets this bit output stream to use the given pattern of bits as its encoding for the end of a file (EOF); students should not call this method.
static java.lang.String toPrintable(char ch)
          Converts the given character into a format that prints well on the screen.
 void write(int b)
          Writes the given byte to output.
 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.PrintStream
append, append, append, checkError, clearError, format, format, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

public static final boolean DEBUG
See Also:
Constant Field Values

BYTE_SIZE

public static final int BYTE_SIZE
See Also:
Constant Field Values

EOF

public static final char 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:
BitOutputStream.BitIOException - 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:
BitOutputStream.BitIOException - if the file cannot be opened for writing.
Method Detail

toPrintable

public static java.lang.String toPrintable(char ch)
Converts the given character into a format that prints well on the screen. For most characters, this just means enclosing it in ' ' marks. For special characters such as \n, replaces them with a \ and an n. Also replaces the special EOF character with the string "EOF".

Parameters:
ch - the character to convert, such as '\n'
Returns:
the formatted string, such as "'\\n'"

close

public void close()
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.PrintStream

flush

public void flush()
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.PrintStream

hasSeenEOF

public boolean hasSeenEOF()
Returns true if this output stream has ever written an EOF character; students should not call this method. This is a hack so that a BitInputStream knows when it is out of bits.

Returns:
true if EOF has been seen, otherwise false.

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). Ignores the caller and always uses 'byte' mode if writing to System.out or if the JVM bitstream.bitmode environment variable is set.

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

setEOFEncoding

public void setEOFEncoding(java.lang.String eofEncoding)
Sets this bit output stream to use the given pattern of bits as its encoding for the end of a file (EOF); students should not call this method.

Parameters:
eofEncoding - the EOF encoding to use

write

public void write(int b)
Writes the given byte to output.

Overrides:
write in class java.io.PrintStream
Parameters:
b - the byte value to write.
Throws:
BitOutputStream.BitIOException - if unable to write to the underlying output stream

print

public void print(char c)
Prints the given character to this output stream.

Overrides:
print in class java.io.PrintStream
Parameters:
c - The character to print.

writeBit

public void writeBit(int bit)
Writes given bit to output.

Parameters:
bit - the bit value to write.
Throws:
java.lang.IllegalArgumentException - if bit is not 0 or 1

writeBits

public void writeBits(java.lang.String bits)
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'.

writeBytes

public void writeBytes(java.lang.String bytes)
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'.

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