Class BitOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended byBitOutputStream

public class BitOutputStream
extends java.io.OutputStream

This class is a buffered output stream that allows for users to easily write bits and bytes to the underlying stream. All methods use computer arithmetic to maipulate the bits, and have a default buffer of 64 bits (long).

 Usage: Identical to the BufferedOutputStream.
        ie.  BitOutputStream out = new BitOutputStream(new FileOutputStream(file));
 


Field Summary
protected  long buf
          The 'long' buffer that stores the bits.
protected  int bufLength
          A 'long' is represented by 64 bits, thus the length of the buffer is 64 bits.
protected  long[] byteLReduce
          The reduce arrays used to reduce large values.
protected  int count
          The current location in the buffer.
protected  long[] outReduce
          The reduce arrays used to reduce large values.
 
Constructor Summary
BitOutputStream(java.io.OutputStream os)
          Initialize all variables needed for this stream.
 
Method Summary
 void close()
          Closes this output stream and releases any system resources associated with this stream.
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out.
 java.lang.String toString()
          Returns a String representation of the state of the current buffer in binary form, as well as any variables associated with the buffer.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array to this output stream.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 void writeBit(boolean b)
          Writes a specified bit to the buffer, and if needed, the output stream specified.
 void writeBit(byte b)
          Writes a specified bit to the buffer, and if needed, the output stream specified.
 void writeBits(boolean[] b, int off, int len)
          Writes a series of bits to the buffer, and if needed, the output stream specified.
 void writeBits(java.lang.String str, int off, int len)
          Writes a series of bits to the buffer, and if needed, the output stream specified.
 void writeByte(byte a)
          Writes a UNSIGNED byte to the buffer.
 void writeBytes(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void writeInt(int i)
          Writes an int to the buffer, and if needed, the output stream specified.
 void writeLong(long l)
          Writes a long to the buffer and/or output stream.
 void writeString(java.lang.String str, int off, int len)
          Writes a String to the buffer, in char form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected long buf
The 'long' buffer that stores the bits.


bufLength

protected int bufLength
A 'long' is represented by 64 bits, thus the length of the buffer is 64 bits.


count

protected int count
The current location in the buffer.


byteLReduce

protected long[] byteLReduce
The reduce arrays used to reduce large values.


outReduce

protected long[] outReduce
The reduce arrays used to reduce large values.

Constructor Detail

BitOutputStream

public BitOutputStream(java.io.OutputStream os)
Initialize all variables needed for this stream.

Parameters:
os - the OutputStream to write to.
Method Detail

close

public void close()
           throws java.io.IOException
Closes this output stream and releases any system resources associated with this stream.

Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out.

Throws:
java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes from the specified byte array to this output stream.

Parameters:
b - the byte array to be written.
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this output stream.

Parameters:
b - the byte to be written.
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Parameters:
b - the byte array to be written.
off - the offset from the beginning of the array.
len - the number of bytes of the array to be written.
Throws:
java.io.IOException

writeBytes

public void writeBytes(byte[] b,
                       int off,
                       int len)
                throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Parameters:
b - the byte array to be written.
off - the offset from the beginning of the array.
len - the number of bytes of the array to be written.
Throws:
java.io.IOException

writeByte

public void writeByte(byte a)
               throws java.io.IOException
Writes a UNSIGNED byte to the buffer. If the buffer is full, then it will be written to the specified output stream.

Parameters:
a - the byte to be written.
Throws:
java.io.IOException

writeBit

public void writeBit(boolean b)
              throws java.io.IOException
Writes a specified bit to the buffer, and if needed, the output stream specified. If boolean 'b' is true, 1 is written, 0 otherwise.

Parameters:
b - the bit to be written.
Throws:
java.io.IOException

writeBit

public void writeBit(byte b)
              throws java.io.IOException
Writes a specified bit to the buffer, and if needed, the output stream specified. If byte 'b' is == 0, bit -> 0, If byte 'b' is != 0, bit -> 1.

Parameters:
b - the byte that represents the bit to be written. See method description.
Throws:
java.io.IOException

writeBits

public void writeBits(boolean[] b,
                      int off,
                      int len)
               throws java.io.IOException
Writes a series of bits to the buffer, and if needed, the output stream specified.

Parameters:
b - the boolean array representing the bits from left to right.
off - the offset from the beginning of the array.
len - the number of bits of the array to be written.
Throws:
java.io.IOException

writeBits

public void writeBits(java.lang.String str,
                      int off,
                      int len)
               throws java.io.IOException
Writes a series of bits to the buffer, and if needed, the output stream specified. This method ONLY accepts String representations of bits. ie. "010010111" To write a general String, use the writeString() method.

Parameters:
str - the String to be written.
off - the offset from the beginning of the String.
len - the number of characters of the String to be written.
Throws:
java.io.IOException

writeInt

public void writeInt(int i)
              throws java.lang.Exception
Writes an int to the buffer, and if needed, the output stream specified.

Parameters:
i - the int to be written.
Throws:
java.lang.Exception

writeLong

public void writeLong(long l)
               throws java.lang.Exception
Writes a long to the buffer and/or output stream.

Parameters:
l - the long to be written.
Throws:
java.lang.Exception

writeString

public void writeString(java.lang.String str,
                        int off,
                        int len)
                 throws java.lang.Exception
Writes a String to the buffer, in char form.

Parameters:
str - the String to be written.
off - the offset from the beginning of the String.
len - the number of characters of the String to be written.
Throws:
java.lang.Exception

toString

public java.lang.String toString()
Returns a String representation of the state of the current buffer in binary form, as well as any variables associated with the buffer.