Package com.helger.commons.io.stream
Class NonBlockingBitOutputStream
java.lang.Object
com.helger.commons.io.stream.NonBlockingBitOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
BitOutputStream
The
For a thread-safe version see
NonBlockingBitOutputStream allows writing individual bits to a
general Java OutputStream. Like the various Stream-classes from Java, the
BitOutputStream has to be created based on another OutputStream. This class
is able to write a single bit to a stream (even though a byte has to be
filled until the data is flushed to the underlying output stream). It is also
able to write an integer value to the stream using the specified number of
bits.For a thread-safe version see
BitOutputStream- Author:
- Andreas Jakl, Philip Helger
-
Constructor Summary
ConstructorsConstructorDescriptionNonBlockingBitOutputStream(OutputStream aOS, ByteOrder aByteOrder) Create a new bit output stream based on an existing Java OutputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Flush the data and close the underlying output stream.voidflush()Write the current cache to the stream and reset the buffer.toString()voidwriteBit(int aBit) Write a single bit to the stream.voidwriteBits(int aValue, int nNumBits) Write the specified number of bits from the int value to the stream.
-
Constructor Details
-
NonBlockingBitOutputStream
Create a new bit output stream based on an existing Java OutputStream.- Parameters:
aOS- the output stream this class should use. May not benull.aByteOrder- The non-nullbyte order to use.
-
-
Method Details
-
getByteOrder
- Returns:
- The byte order used. Never
null.
-
writeBit
Write a single bit to the stream. It will only be flushed to the underlying OutputStream when a byte has been completed or when flush() manually.- Parameters:
aBit- 1 if the bit should be set, 0 if not- Throws:
IOException- In case writing to the output stream failed
-
writeBits
Write the specified number of bits from the int value to the stream. Corresponding to the InputStream, the bits are written starting at the highest bit ( >> aNumberOfBits ), going down to the lowest bit ( >> 0 ).- Parameters:
aValue- the int containing the bits that should be written to the stream.nNumBits- how many bits of the integer should be written to the stream.- Throws:
IOException- In case writing to the output stream failed
-
flush
Write the current cache to the stream and reset the buffer.- Specified by:
flushin interfaceFlushable- Throws:
IOException- In case writing to the output stream failed
-
close
public void close()Flush the data and close the underlying output stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
-