fi.helsinki.cs.ohtu.mpeg2.util
Class BitOutputStream

java.lang.Object
  extended by fi.helsinki.cs.ohtu.mpeg2.util.BitOutputStream

public class BitOutputStream
extends java.lang.Object

This class implements single bit writing capabilities on top of standard OutputStreams.


Nested Class Summary
static class BitOutputStream.BitOrder
          This enum specifies the bit order used.
 
Field Summary
private  int bitLocation
           
private  int bitNumber
           
private  boolean closed
           
private  byte currentByte
           
private  BitOutputStream.BitOrder order
           
private  java.io.OutputStream os
           
 
Constructor Summary
BitOutputStream(java.io.OutputStream os, BitOutputStream.BitOrder order)
          Creates a new BitOutputStream that writes to the given OutputStream.
 
Method Summary
 void byteAlign()
          Aligns the stream to the next byte border.
 void close()
          Closes the stream.
 void flush()
          Flushes the stream.
 void writeBit(int bitToWrite)
          Writes one bit to the stream.
 void writeBits(int[] bits)
          Writes multiple bits to the stream.
 void writeByte(byte byteToWrite)
          Writes a whole byte to the stream.
 void writeLowBits(long value, int bitCount)
          Writes a given number of low-order bits from the given integer to the stream.
 void writeRangeBits(long value, int oneHigherBit, int lowBit)
          Writes a range of bits from the given integer to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

os

private java.io.OutputStream os

order

private BitOutputStream.BitOrder order

bitLocation

private int bitLocation

bitNumber

private int bitNumber

currentByte

private byte currentByte

closed

private boolean closed
Constructor Detail

BitOutputStream

public BitOutputStream(java.io.OutputStream os,
                       BitOutputStream.BitOrder order)
Creates a new BitOutputStream that writes to the given OutputStream.

Parameters:
os - The OutputStream to write to.
order - The BitOrder to use.
Method Detail

writeBit

public void writeBit(int bitToWrite)
              throws java.io.IOException
Writes one bit to the stream.

Parameters:
bitToWrite - The bit to write: 0 or 1.
Throws:
java.io.IOException

writeBits

public void writeBits(int[] bits)
               throws java.io.IOException
Writes multiple bits to the stream.

Parameters:
bits - The bits to write: each either 0 or 1.
Throws:
java.io.IOException

writeLowBits

public void writeLowBits(long value,
                         int bitCount)
                  throws java.io.IOException
Writes a given number of low-order bits from the given integer to the stream. This is equivalent to writeRangeBits(value, bitCount, 0);

Parameters:
value - Integer to extract the bits to write from.
bitCount - How many bits to write.
Throws:
java.io.IOException

writeRangeBits

public void writeRangeBits(long value,
                           int oneHigherBit,
                           int lowBit)
                    throws java.io.IOException
Writes a range of bits from the given integer to the stream. The bits are written to the stream as if writeBit was called for the most significant bit of the bits first, and then on progressively lower-order bits, until the lowest bit is written. The bits will follow the last written bit with no padding in between. In other words, they are not aligned in any way. Naturally, the indices of bits written is limited to 64, the number of bits in a long integer. Therefore, all values of oneHigherBit larger than 64 are treated as 64. Similarly, all values of lowBit lower than 0 are treated as 0. The total number of bits written will thus be oneHigherBit - lowBit. However, if this value would be negative, zero bits will be written.

Parameters:
value - Integer to extract the bits to write from.
oneHigherBit - Number of the bit just left of the range desired to be written.
lowBit - Number of the rightmost bit in the range to be written.
Throws:
java.io.IOException

writeByte

public void writeByte(byte byteToWrite)
               throws java.io.IOException
Writes a whole byte to the stream. The byte written is not necessarily aligned. This is equivalent to calling writeLowBits(byteToWrite, 8);

Parameters:
byteToWrite - The byte to write.
Throws:
java.io.IOException

byteAlign

public void byteAlign()
               throws java.io.IOException
Aligns the stream to the next byte border. Writes as many padding zero bits as needed to make the stream byte-aligned. This means that following a call to this method, the next bit written will be written to a fresh byte to which no bit has been previously written.

Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flushes the stream.

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the stream. If the bits waiting to be written are not aligned to the byte they will be padded with zeroes and written. Also closes the underlying stream.

Throws:
java.io.IOException