jwo.utils.gifutils
Class BitOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by jwo.utils.gifutils.BitOutputStream
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
GIFOutputStream

public class BitOutputStream
extends FilterOutputStream

Allows the writing of 1 to 32 bits at a time, on any bit boundary. Bits are written to bytes, least significant bit first.

Version:
2.3, 26th August, 1998, modified 11th April, 2006.
Author:
Benjamin E. Norman, Minor modificiations, Jo Wood.

Field Summary
protected  int cachedBits
          Stores any cached bits which need to be written to the underlying stream.
protected  int numCachedBits
          Stores the number of valid bits in 'cachedBits' and is never more than 7.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
BitOutputStream(OutputStream os)
          Creates a bit output stream based on the given underlying output stream.
 
Method Summary
 void flush()
          Flushes any cached bits and bytes.
 void write(int byteValue)
          Writes a byte at the current bit boundary.
 void writeBit(int bit)
          Writes a single bit (1 or 0) to the underlying output stream.
 void writeBits(int value, int numBits)
          Writes the given number of bits (1 to 32) from the given value to the underlying output stream.
 
Methods inherited from class java.io.FilterOutputStream
close, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cachedBits

protected int cachedBits
Stores any cached bits which need to be written to the underlying stream.


numCachedBits

protected int numCachedBits
Stores the number of valid bits in 'cachedBits' and is never more than 7. Only partial bytes are cached.

Constructor Detail

BitOutputStream

public BitOutputStream(OutputStream os)
Creates a bit output stream based on the given underlying output stream.

Parameters:
os - Output stream to use.
Method Detail

write

public void write(int byteValue)
           throws IOException
Writes a byte at the current bit boundary.

Overrides:
write in class FilterOutputStream
Parameters:
byteValue - Value to write to output stream.
Throws:
IOException - if problem writing to output stream.

writeBits

public void writeBits(int value,
                      int numBits)
               throws IOException
Writes the given number of bits (1 to 32) from the given value to the underlying output stream.

Parameters:
value - Value to write to output stream.
numBits - Number of bits to write.
Throws:
IllegalArgumentException - if numBits is not between 1 and 32.
IOException - if problem writing to output stream.

writeBit

public void writeBit(int bit)
              throws IOException
Writes a single bit (1 or 0) to the underlying output stream.

Parameters:
bit - Bit to write.
Throws:
IOException - if problem writing to output stream.

flush

public void flush()
           throws IOException
Flushes any cached bits and bytes.

Specified by:
flush in interface Flushable
Overrides:
flush in class FilterOutputStream
Throws:
IOException - if problem flushing output stream.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009