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
public class NonBlockingBitOutputStream extends Object implements Closeable, Flushable
TheNonBlockingBitOutputStreamallows 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 seeBitOutputStream- Author:
- Andreas Jakl, Philip Helger
-
-
Constructor Summary
Constructors Constructor Description NonBlockingBitOutputStream(OutputStream aOS, ByteOrder aByteOrder)Create a new bit output stream based on an existing Java OutputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Flush the data and close the underlying output stream.voidflush()Write the current cache to the stream and reset the buffer.ByteOrdergetByteOrder()StringtoString()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 Detail
-
NonBlockingBitOutputStream
public NonBlockingBitOutputStream(@Nonnull OutputStream aOS, @Nonnull ByteOrder aByteOrder)
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 Detail
-
writeBit
public void writeBit(int aBit) throws IOExceptionWrite 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
public void writeBits(int aValue, @Nonnegative int nNumBits) throws IOExceptionWrite 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
public void flush() throws IOExceptionWrite 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
-
-