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
-
Method Summary
Modifier and TypeMethodDescriptionint
Get the current bit offset.void
close()
int
Write zero bits to this output stream up to the next byte boundary.void
write
(byte[] buf, int off, int len) void
write
(int b) void
writeBit
(boolean bit) Write a single bit.void
writeBits
(long bits, int len) Write bits in along
value.void
Write some bits from aBitSet
.Methods inherited from class java.io.FilterOutputStream
flush, write
Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
BitwiseOutputStream
Constructor.- Parameters:
out
- underlying output
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in 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
- ifbits
is nullIllegalArgumentException
- iflen
is negative
-
writeBits
Write bits in along
value.The first bit in
bits
written is at index zero, etc. Bits at indexlen
and higher are ignored.- Parameters:
bits
- value containing the bits to write (low-order bit first)len
- the number of bits inbits
to write- Throws:
IOException
- if an I/O error occursIllegalArgumentException
- iflen
is 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
-