org.at4j.support.io
Class LittleEndianBitOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.at4j.support.io.LittleEndianBitOutputStream
All Implemented Interfaces:
Closeable, Flushable, BitOutput

public class LittleEndianBitOutputStream
extends OutputStream
implements BitOutput

This is an OutputStream that implements BitOutput and hence can be used to write individual bits to the output. The bits are stored in little-endian order.

Since:
1.1
Author:
Karl Gustafsson

Constructor Summary
LittleEndianBitOutputStream(OutputStream wrapped)
           
 
Method Summary
 void close()
          Close the output stream.
 int getNumberOfBitsInUnfinishedByte()
          Get the number of bits that have been written to the last byte.
 long getNumberOfBytesWritten()
          Get the total number of whole bytes written by this stream so far.
 int getUnfinishedByte()
          Get the value of the unfinished byte.
 void padToByteBoundary()
          Pad the output with zeroes to the next byte boundary.
 void write(byte[] barr)
          See OutputStream.write(byte[]).
 void write(byte[] barr, int off, int len)
          See OutputStream.write(byte[], int, int).
 void write(int b)
          See OutputStream.write(int).
 void writeBit(boolean val)
          Write a single bit.
 void writeBits(int val, int no)
          Write up to eight bits.
 void writeBitsLittleEndian(int val, int no)
          Write up to 32 bits.
 void writeBytes(byte[] barr, int off, int len)
          Write an array of bytes to the output.
 
Methods inherited from class java.io.OutputStream
flush
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LittleEndianBitOutputStream

public LittleEndianBitOutputStream(OutputStream wrapped)
Method Detail

getNumberOfBytesWritten

public long getNumberOfBytesWritten()
Get the total number of whole bytes written by this stream so far.

Returns:
The number of whole bytes written.

getUnfinishedByte

public int getUnfinishedByte()
Description copied from interface: BitOutput
Get the value of the unfinished byte. The value is shifted so that the least significant bit positions are used. BitOutput.getNumberOfBitsInUnfinishedByte() returns how many bit positions that are used.

If the current position is at a byte boundary, 0 is returned.

Specified by:
getUnfinishedByte in interface BitOutput
Returns:
The value of the unfinished byte.

getNumberOfBitsInUnfinishedByte

public int getNumberOfBitsInUnfinishedByte()
Description copied from interface: BitOutput
Get the number of bits that have been written to the last byte.

If the current position is at a byte boundary, 0 is returned.

Specified by:
getNumberOfBitsInUnfinishedByte in interface BitOutput
Returns:
The number of bits that have been written to the last byte. This is a number between 0 and 7 (inclusive).

padToByteBoundary

public void padToByteBoundary()
                       throws IOException
Description copied from interface: BitOutput
Pad the output with zeroes to the next byte boundary. If the current position is already at a byte boundary, this method does nothing.

Specified by:
padToByteBoundary in interface BitOutput
Throws:
IOException - On I/O errors.

writeBit

public void writeBit(boolean val)
              throws IOException
Description copied from interface: BitOutput
Write a single bit.

Specified by:
writeBit in interface BitOutput
Parameters:
val - The bit (true == 1, false == 0).
Throws:
IOException - On I/O errors.

writeBits

public void writeBits(int val,
                      int no)
               throws IOException,
                      IndexOutOfBoundsException
Description copied from interface: BitOutput
Write up to eight bits.

Specified by:
writeBits in interface BitOutput
Parameters:
val - The value to write. The bits written are the no rightmost bits of val. It is not verified that val fits within its no rightmost bits. If it does not, the written value is simply truncated.
no - The number of bits to write. This must be between 0 and 8 (inclusive).
Throws:
IOException - On I/O errors
IndexOutOfBoundsException - If no is less than 0 or greater than 8.
See Also:
BitOutput.writeBitsLittleEndian(int, int)

writeBitsLittleEndian

public void writeBitsLittleEndian(int val,
                                  int no)
                           throws IndexOutOfBoundsException,
                                  IOException
Description copied from interface: BitOutput
Write up to 32 bits. The bits are written little endian with the most significant bit first.

Specified by:
writeBitsLittleEndian in interface BitOutput
Parameters:
val - The value to write. The bits written are the no rightmost bits of val. It is not verified that val fits within its no rightmost bits. If it does not, the written value is simply truncated.
no - The number of bits to write. This must be between 0 and 32 (inclusive)
Throws:
IndexOutOfBoundsException - If no is less than 0 or more than 32.
IOException - On I/O errors.
See Also:
BitOutput.writeBits(int, int)

writeBytes

public void writeBytes(byte[] barr,
                       int off,
                       int len)
                throws IndexOutOfBoundsException,
                       IOException
Description copied from interface: BitOutput
Write an array of bytes to the output. Unlike BitOutput.write(byte[], int, int), this method does not require that the current position is at a byte boundary.

Specified by:
writeBytes in interface BitOutput
Parameters:
barr - The bytes to write.
off - The offset in the byte array.
len - The number of bytes to write.
Throws:
IndexOutOfBoundsException - If the offset or the length is negative or if the offset + length is larger than the byte array.
IOException - On I/O errors
See Also:
BitOutput.write(byte[], int, int)

write

public void write(int b)
           throws IOException
Description copied from interface: BitOutput
See OutputStream.write(int).

This method requires that the current position of the output is at a byte boundary.

Specified by:
write in interface BitOutput
Specified by:
write in class OutputStream
Parameters:
b - The byte to write (0 - 255).
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.
See Also:
OutputStream.write(int)

write

public void write(byte[] barr)
           throws IOException
Description copied from interface: BitOutput
See OutputStream.write(byte[]).

This method requires that the current position of the output is at a byte boundary.

Specified by:
write in interface BitOutput
Overrides:
write in class OutputStream
Parameters:
barr - The bytes to write.
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.
See Also:
OutputStream.write(byte[])

write

public void write(byte[] barr,
                  int off,
                  int len)
           throws IOException
Description copied from interface: BitOutput
See OutputStream.write(byte[], int, int).

This method requires that the current position of the output is at a byte boundary.

Specified by:
write in interface BitOutput
Overrides:
write in class OutputStream
Parameters:
barr - The bytes to write.
off - The offset in the byte array.
len - The number of bytes to write.
Throws:
IOException - On I/O errors or if the current position is not at a byte boundary.
See Also:
OutputStream.write(byte[], int, int), BitOutput.writeBytes(byte[], int, int)

close

public void close()
           throws IOException
Close the output stream.

This method does not automatically pad the last written bits to a full byte. If there are bits written to it the stream must be padded before closing it. See padToByteBoundary().

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException