- java.lang.Object
-
- java.io.InputStream
-
- net.morimekta.io.BitPackingInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class BitPackingInputStream extends InputStream
An input stream that reads individual bits in groups of up to 31 bits at a time. The 31 limit is to allow for negative numbers to still representEOF
.
-
-
Constructor Summary
Constructors Constructor Description BitPackingInputStream(InputStream in)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
align()
Ignore the remainder of any partially read byte (octet), and continue to read from the beginning of next byte.int
available()
int
available(int bits)
Return the number of available chunks of given number of bits.void
close()
int
read()
int
readBits(int bits)
Read a number of bits return as a bit endian integer.-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
Constructor Detail
-
BitPackingInputStream
public BitPackingInputStream(InputStream in)
-
-
Method Detail
-
readBits
public int readBits(int bits) throws IOException
Read a number of bits return as a bit endian integer.- Parameters:
bits
- Number of bits to read. 1 is same as boolean, 8 same as a byte. Maximum value of 31.- Returns:
- The read bits. -1 if not enough bit data to read. Will consume bits regardless.
- Throws:
IOException
- If unable to read from stream.IllegalArgumentException
- If bad number of bits requested.
-
available
public int available(int bits) throws IOException
Return the number of available chunks of given number of bits.- Parameters:
bits
- The number of bits per chunk.- Returns:
- The number of readable chunks.
- Throws:
IOException
- If unable to get available bytes.
-
align
public void align()
Ignore the remainder of any partially read byte (octet), and continue to read from the beginning of next byte.
-
read
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
-