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
-
Method Summary
Modifier and TypeMethodDescriptionboolean
bit()
Read a single bit that is expected to be there.int
Get the current bit offset.long
bits
(int len) Read up to 64 bits that are expected to be there.void
close()
void
mark
(int readlimit) int
read()
int
read
(byte[] buf, int off, int len) int
readBit()
Read a single bit, or detect EOF.readBits
(int len) Read some number of bits and return them in aBitSet
.int
readBits
(AtomicLong result, int len) Read up to 64 bits.void
reset()
long
skip
(long remain) int
Discard bits from this input stream up to the next byte boundary.Methods inherited from class java.io.FilterInputStream
available, markSupported, read
Methods 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
mark
in classFilterInputStream
-
reset
- Overrides:
reset
in classFilterInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classFilterInputStream
- Throws:
IOException
-
readBits
Read some number of bits and return them in aBitSet
.If EOF is encountered before reading
len
bits, anEOFException
is thrown.- Parameters:
len
- the number of bits to read- Throws:
IOException
- if an I/O error occursEOFException
- if EOF is encountered beforelen
bits can be readIllegalArgumentException
- iflen
is negative
-
readBits
Read up to 64 bits.Up to
len
bits 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 inresult
will 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
- iflen
is negative or greater than 64
-
bits
Read up to 64 bits that are expected to be there.This will read
len
bits and return them in along
value. 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
len
bits that were read, starting at bit index zero - Throws:
EOFException
- if EOF is encounteredIOException
- if an I/O error occursIllegalArgumentException
- iflen
is 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
-