Class BitwiseInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
InputStream.
Instances support reading arbitrary numbers of individual bits. Bits are read from the underlying
InputStream in groups of eight (i.e., whole bytes, obviously), where the bits in each
byte are assumed to be ordered from least significant to most significant bit.
Of course, instances also support reading traditional byte-oriented data: any bytes read are handled as if each of the eight bits were read individually, in order from least significant to most significant.
Because the underlying input is byte-oriented, the total number of bits read will always be a multiple of eight. Attempting to read one or more whole bytes when less than eight bits remain will result in EOF being returnd.
- See Also:
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbit()Read a single bit that is expected to be there.intGet the current bit offset.longbits(int len) Read up to 64 bits that are expected to be there.voidclose()voidmark(int readlimit) intread()intread(byte[] buf, int off, int len) intreadBit()Read a single bit, or detect EOF.readBits(int len) Read some number of bits and return them in aBitSet.intreadBits(AtomicLong result, int len) Read up to 64 bits.voidreset()longskip(long remain) intDiscard bits from this input stream up to the next byte boundary.Methods inherited from class java.io.FilterInputStream
available, markSupported, readMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
BitwiseInputStream
Constructor.- Parameters:
in- underlying input
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
markin classFilterInputStream
-
reset
- Overrides:
resetin classFilterInputStream- Throws:
IOException
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
skip
- Overrides:
skipin classFilterInputStream- Throws:
IOException
-
readBits
Read some number of bits and return them in aBitSet.If EOF is encountered before reading
lenbits, anEOFExceptionis thrown.- Parameters:
len- the number of bits to read- Throws:
IOException- if an I/O error occursEOFException- if EOF is encountered beforelenbits can be readIllegalArgumentException- iflenis negative
-
readBits
Read up to 64 bits.Up to
lenbits will be read and stored inresult; the first bit read will be at index zero, etc. The actual number of bits read is returned, and all higher bits inresultwill be set to zero.If EOF is encountered before reading any bits, -1 is returned.
- Parameters:
result- value in which to set the bits read (low-order bit first)len- the desired number of bits to read- Returns:
- the number of bits actually read
- Throws:
IOException- if an I/O error occursIllegalArgumentException- iflenis negative or greater than 64
-
bits
Read up to 64 bits that are expected to be there.This will read
lenbits and return them in alongvalue. The first bit read will be at index zero, etc. All higher bits will be zero.- Parameters:
len- the number of bits to read- Returns:
- the
lenbits that were read, starting at bit index zero - Throws:
EOFException- if EOF is encounteredIOException- if an I/O error occursIllegalArgumentException- iflenis negative or greater than 64
-
bit
Read a single bit that is expected to be there.- Returns:
- the bit read
- Throws:
EOFException- if EOF is encounteredIOException- if an I/O error occurs
-
readBit
Read a single bit, or detect EOF.- Returns:
- a bit value of 0 or 1, or -1 on EOF
- 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)
-
skipToByteBoundary
Discard bits from this input stream up to the next byte boundary.If the number of bits read so far is a multiple of eight, this method does nothing. Otherwise it skips up to seven bits so that the next read operation will be byte-aligned.
- Returns:
- the number of bits skipped (from zero to seven)
- Throws:
IOException- if an I/O error occurs
-