Class BitwiseOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
OutputStream.
Instances support writing arbitrary numbers of individual bits. Bits are written to the underlying
OutputStream in groups of eight (i.e., whole bytes, obviously), where the bits in each written
byte are ordered from least significant to most significant bit.
Of course, instances also support writing traditional byte-oriented data: any bytes written are handled as if each of the eight bits were written individually, in order from least significant to most significant.
When instances are closed, if the output bitstream is not currently aligned to a byte boundary (i.e.,
bitOffset() would return a non-zero value), then padding of up to seven zero bits is written (as if by
padToByteBoundary()), and then the underlying stream is closed.
As an example, writing the bits 0b101001101111 and then invoking close() would result in
0x6d 0x0e being written to the underlying stream. The same output would result if 0b111,
0x4d, and then 0b001 were written.
- See Also:
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintGet the current bit offset.voidclose()intWrite zero bits to this output stream up to the next byte boundary.voidwrite(byte[] buf, int off, int len) voidwrite(int b) voidwriteBit(boolean bit) Write a single bit.voidwriteBits(long bits, int len) Write bits in alongvalue.voidWrite some bits from aBitSet.Methods inherited from class java.io.FilterOutputStream
flush, writeMethods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
BitwiseOutputStream
Constructor.- Parameters:
out- underlying output
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException
-
writeBits
Write some bits from aBitSet.- Parameters:
bits- where to get the bitslen- the number of bits to write- Throws:
IOException- if an I/O error occursIllegalArgumentException- ifbitsis nullIllegalArgumentException- iflenis negative
-
writeBits
Write bits in alongvalue.The first bit in
bitswritten is at index zero, etc. Bits at indexlenand higher are ignored.- Parameters:
bits- value containing the bits to write (low-order bit first)len- the number of bits inbitsto write- Throws:
IOException- if an I/O error occursIllegalArgumentException- iflenis negative or greater than 64
-
writeBit
Write a single bit.- Parameters:
bit- the bit to write- Throws:
IOException- if an I/O error occurs
-
bitOffset
public int bitOffset()Get the current bit offset.- Returns:
- current bit offset (from zero to seven)
-
padToByteBoundary
Write zero bits to this output stream up to the next byte boundary.If the number of bits written so far is a multiple of eight, this method does nothing. Otherwise it writes zero bits until the next write operation will be byte-aligned.
- Returns:
- the number of zero bits written (from zero to seven)
- Throws:
IOException- if an I/O error occurs
-