public class LittleEndianBitOutputStream extends OutputStream implements BitOutput
OutputStream
that implements BitOutput
and hence
can be used to write individual bits to the output. The bits are stored in
little-endian order.Constructor and Description |
---|
LittleEndianBitOutputStream(OutputStream wrapped) |
Modifier and Type | Method and Description |
---|---|
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)
|
void |
write(byte[] barr,
int off,
int len)
|
void |
write(int b)
|
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.
|
flush
public LittleEndianBitOutputStream(OutputStream wrapped)
public long getNumberOfBytesWritten()
public int getUnfinishedByte()
BitOutput
BitOutput.getNumberOfBitsInUnfinishedByte()
returns how many bit positions
that are used.
If the current position is at a byte boundary, 0 is returned.
getUnfinishedByte
in interface BitOutput
public int getNumberOfBitsInUnfinishedByte()
BitOutput
If the current position is at a byte boundary, 0 is returned.
getNumberOfBitsInUnfinishedByte
in interface BitOutput
public void padToByteBoundary() throws IOException
BitOutput
padToByteBoundary
in interface BitOutput
IOException
- On I/O errors.public void writeBit(boolean val) throws IOException
BitOutput
writeBit
in interface BitOutput
val
- The bit (true == 1
, false == 0
).IOException
- On I/O errors.public void writeBits(int val, int no) throws IOException, IndexOutOfBoundsException
BitOutput
writeBits
in interface BitOutput
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).IOException
- On I/O errorsIndexOutOfBoundsException
- If no
is less than 0 or greater
than 8.BitOutput.writeBitsLittleEndian(int, int)
public void writeBitsLittleEndian(int val, int no) throws IndexOutOfBoundsException, IOException
BitOutput
writeBitsLittleEndian
in interface BitOutput
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)IndexOutOfBoundsException
- If no
is less than 0 or more
than 32.IOException
- On I/O errors.BitOutput.writeBits(int, int)
public void writeBytes(byte[] barr, int off, int len) throws IndexOutOfBoundsException, IOException
BitOutput
BitOutput.write(byte[], int, int)
, this method does not require that the
current position is at a byte boundary.writeBytes
in interface BitOutput
barr
- The bytes to write.off
- The offset in the byte array.len
- The number of bytes to write.IndexOutOfBoundsException
- If the offset or the length is negative
or if the offset + length is larger than the byte array.IOException
- On I/O errorsBitOutput.write(byte[], int, int)
public void write(int b) throws IOException
BitOutput
OutputStream.write(int)
.
This method requires that the current position of the output is at a byte boundary.
write
in interface BitOutput
write
in class OutputStream
b
- The byte to write (0 - 255).IOException
- On I/O errors or if the current position is not at a
byte boundary.OutputStream.write(int)
public void write(byte[] barr) throws IOException
BitOutput
OutputStream.write(byte[])
.
This method requires that the current position of the output is at a byte boundary.
write
in interface BitOutput
write
in class OutputStream
barr
- The bytes to write.IOException
- On I/O errors or if the current position is not at a
byte boundary.OutputStream.write(byte[])
public void write(byte[] barr, int off, int len) throws IOException
BitOutput
OutputStream.write(byte[], int, int)
.
This method requires that the current position of the output is at a byte boundary.
write
in interface BitOutput
write
in class OutputStream
barr
- The bytes to write.off
- The offset in the byte array.len
- The number of bytes to write.IOException
- On I/O errors or if the current position is not at a
byte boundary.OutputStream.write(byte[], int, int)
,
BitOutput.writeBytes(byte[], int, int)
public void close() throws IOException
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()
.
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException