public class InputBitStream extends it.unimi.dsi.fastutil.booleans.AbstractBooleanIterator implements Flushable, Closeable
This class wraps any InputStream
so that you can treat it as
bit stream. Constructors and methods closely resemble those of
InputStream
. Data can be read from such a stream in several ways:
reading an integer or long in fixed-width, unary, γ, shifted γ, δ, ζ and (skewed)
Golomb coding, or reading a number of bits that will be stored in a vector of
bytes. There is limited support for mark(int)
/reset()
operations.
This class can also wrap a byte
array; this is much more lightweight than wrapping a FastByteArrayInputStream
wrapping the array. Overflowing the array
will cause an EOFException
.
Note that when reading using a vector of bytes bits are read in the
stream format (see OutputBitStream
): the first bit is bit 7 of the
first byte, the eighth bit is bit 0 of the first byte, the ninth bit is bit
7 of the second byte and so on. When reading integers using some coding,
instead, they are stored in the standard way, that is, in the lower
bits.
Additional features:
java.io
, this class provides a flush()
method that resets the internal state. At this point, you can safely reposition
the underlying stream and read again afterwards. For instance, this is safe
and will perform as expected:
FileInputStream fis = new FileInputStream( ... ); InputBitStream ibs = new InputBitStream( fis ); ... read operations on ibs ... ibs.flush(); fis.getChannel().position( ... ); ... other read operations on ibs ...
As a commodity, an instance of this class will try to cast the underlying byte
stream to a RepositionableStream
and to fetch by reflection the FileChannel
underlying the given input stream, in this
order. If either reference can be successfully fetched, you can use
directly the position()
method with argument
pos
with the same semantics of a flush()
, followed by
a call to position(pos / 8)
(where the latter method belongs
either to the underlying stream or to its underlying file channel), followed
by a skip(pos % 8)
. However, since the reflective checks are quite
heavy they can be disabled using a suitable constructor.
nextBoolean()
will return the same bit as readBit()
,
and also the same exceptions, whereas hasNext()
will always return true:
you must be prepared to catch a RuntimeException
wrapping an IOException
in case the file ends. It
is very difficult to implement completely an eager operator using a input-stream
based model.
This class is not synchronised. If multiple threads access an instance of this class concurrently, they must be synchronised externally.
InputStream
,
OutputBitStream
Modifier and Type | Field and Description |
---|---|
protected int |
avail
Current number of bytes available in the byte buffer.
|
protected byte[] |
buffer
The stream buffer.
|
static int |
DEFAULT_BUFFER_SIZE
The default size of the byte buffer in bytes (8Ki).
|
static int[] |
DELTA |
protected FileChannel |
fileChannel
The cached file channel underlying
is , if any. |
protected int |
fill
Current number of bits in the bit buffer (stored low).
|
static int[] |
GAMMA |
protected InputStream |
is
The underlying
InputStream . |
protected int |
pos
Current position in the byte buffer.
|
protected long |
position
Current position of the first byte in the byte buffer.
|
protected it.unimi.dsi.fastutil.io.RepositionableStream |
repositionableStream
is cast to a positionable stream, if possible. |
static int[] |
SHIFTED_GAMMA |
protected boolean |
wrapping
True if we are wrapping an array.
|
static int[] |
ZETA_3 |
Modifier | Constructor and Description |
---|---|
protected |
InputBitStream()
This (non-public) constructor exists just to provide fake initialisation for classes such as
DebugInputBitStream . |
|
InputBitStream(byte[] a)
Creates a new input bit stream wrapping a given byte array.
|
|
InputBitStream(byte[] a,
int off,
int len)
Creates a new input bit stream wrapping a slice of a given byte array
(BBT 8/30/2009).
|
|
InputBitStream(File file)
Creates a new input bit stream reading from a file.
|
|
InputBitStream(FileInputStream is)
Creates a new input bit stream wrapping a given file input stream using a buffer of size
DEFAULT_BUFFER_SIZE . |
|
InputBitStream(FileInputStream is,
int bufSize)
Creates a new input bit stream wrapping a given file input stream with a specified buffer size.
|
|
InputBitStream(File file,
int bufSize)
Creates a new input bit stream reading from a file.
|
|
InputBitStream(InputStream is)
Creates a new input bit stream wrapping a given input stream using a buffer of size
DEFAULT_BUFFER_SIZE . |
|
InputBitStream(InputStream is,
boolean testForPosition)
Creates a new input bit stream wrapping a given input stream using a buffer of size
DEFAULT_BUFFER_SIZE . |
|
InputBitStream(InputStream is,
int bufSize)
Creates a new input bit stream wrapping a given input stream with a specified buffer size.
|
|
InputBitStream(InputStream is,
int bufSize,
boolean testForPosition)
Creates a new input bit stream wrapping a given input stream with a specified buffer size.
|
|
InputBitStream(String name)
Creates a new input bit stream reading from a file.
|
|
InputBitStream(String name,
int bufSize)
Creates a new input bit stream reading from a file.
|
Modifier and Type | Method and Description |
---|---|
void |
align()
Aligns the stream.
|
long |
available()
Returns the number of bits that can be read (or skipped over) from this
bit stream without blocking by the next caller of a method.
|
void |
close()
Closes the bit stream.
|
void |
flush()
Flushes the bit stream.
|
boolean |
hasNext() |
void |
mark(int readLimit)
Marks the current position in this input stream.
|
boolean |
markSupported()
|
boolean |
nextBoolean() |
void |
position(long position)
Sets this stream bit position, if it is based on a
RepositionableStream or on a FileChannel . |
void |
read(byte[] bits,
int len)
Reads a sequence of bits.
|
int |
readBit()
Reads a bit.
|
long |
readBits()
Returns the number of bits read from this bit stream.
|
void |
readBits(long readBits)
Sets the number of bits read from this bit stream.
|
int |
readDelta()
Reads a natural number in δ coding.
|
void |
readDeltas(int[] a,
int count)
Reads a given number of δ-coded integers.
|
int |
readGamma()
Reads a natural number in γ coding.
|
void |
readGammas(int[] a,
int count)
Reads a given number of γ-coded integers.
|
int |
readGolomb(int b)
Reads a natural number in Golomb coding.
|
int |
readGolomb(int b,
int log2b)
Reads a natural number in Golomb coding.
|
int |
readInt(int len)
Reads a fixed number of bits into an integer.
|
long |
readLong(int len)
Reads a fixed number of bits into a long.
|
long |
readLongDelta()
Reads a long natural number in δ coding.
|
long |
readLongGamma()
Reads a long natural number in γ coding.
|
long |
readLongGolomb(long b)
Reads a long natural number in Golomb coding.
|
long |
readLongGolomb(long b,
int log2b)
Reads a long natural number in Golomb coding.
|
long |
readLongMinimalBinary(long b)
Reads a long natural number in a limited range using a minimal binary coding.
|
long |
readLongMinimalBinary(long b,
int log2b)
Reads a long natural number in a limited range using a minimal binary coding.
|
long |
readLongNibble()
Reads a long natural number in variable-length nibble coding.
|
long |
readLongShiftedGamma()
Reads a natural number in shifted γ coding.
|
long |
readLongSkewedGolomb(long b)
Reads a long natural number in skewed Golomb coding.
|
long |
readLongUnary()
Reads a long natural number in unary coding.
|
long |
readLongZeta(int k)
Reads a long natural number in ζ coding.
|
int |
readMinimalBinary(int b)
Reads a natural number in a limited range using a minimal binary coding.
|
int |
readMinimalBinary(int b,
int log2b)
Reads a natural number in a limited range using a minimal binary coding.
|
int |
readNibble()
Reads a natural number in variable-length nibble coding.
|
int |
readShiftedGamma()
Reads a natural number in shifted γ coding.
|
void |
readShiftedGammas(int[] a,
int count)
Reads a given number of shifted-γ-coded integers.
|
int |
readSkewedGolomb(int b)
Reads a natural number in skewed Golomb coding.
|
int |
readUnary()
Reads a natural number in unary coding.
|
int |
readZeta(int k)
Reads a natural number in ζ coding.
|
void |
readZetas(int k,
int[] a,
int count)
Reads a given number of γ-coded integers.
|
void |
reset()
Repositions this bit stream to the position at the time the
mark(int) method was last called. |
int |
skip(int n)
Deprecated.
This method is simply an expensive, try/catch-surrounded version
of
skip(long) that is made necessary by the interface
by BooleanIterator . |
long |
skip(long n)
Skips the given number of bits.
|
void |
skipDeltas(int n)
Skips a given number of δ-coded integers.
|
void |
skipGammas(int n)
Skips a given number of γ-coded integers.
|
void |
skipShiftedGammas(int n)
Skips a given number of shited-γ-coded integers.
|
void |
skipZetas(int k,
int n)
Skips a given number of ζ-coded integers.
|
public static final int[] GAMMA
public static final int[] DELTA
public static final int[] ZETA_3
public static final int[] SHIFTED_GAMMA
public static final int DEFAULT_BUFFER_SIZE
protected final InputStream is
InputStream
.protected final FileChannel fileChannel
is
, if any.protected final it.unimi.dsi.fastutil.io.RepositionableStream repositionableStream
is
cast to a positionable stream, if possible.protected final boolean wrapping
protected byte[] buffer
protected int fill
protected int pos
protected int avail
protected long position
protected InputBitStream()
DebugInputBitStream
.public InputBitStream(InputStream is)
DEFAULT_BUFFER_SIZE
.
This constructor performs the reflective tests that are necessary to support position(long)
.
is
- the input stream to wrap.public InputBitStream(InputStream is, boolean testForPosition)
DEFAULT_BUFFER_SIZE
.is
- the input stream to wrap.testForPosition
- if false, the reflective tests that are necessary to support position(long)
will not be performed.public InputBitStream(InputStream is, int bufSize)
This constructor performs the reflective tests that are necessary to support position(long)
.
is
- the input stream to wrap.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.public InputBitStream(InputStream is, int bufSize, boolean testForPosition)
is
- the input stream to wrap.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.testForPosition
- if false, the reflective tests that are necessary to support position(long)
will not be performed.public InputBitStream(FileInputStream is)
DEFAULT_BUFFER_SIZE
.
This constructor invokes directly FileInputStream.getChannel()
to support position(long)
.
is
- the file input stream to wrap.public InputBitStream(FileInputStream is, int bufSize)
This constructor invokes directly FileInputStream.getChannel()
to support position(long)
.
is
- the file input stream to wrap.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.public InputBitStream(byte[] a)
a
- the byte array to wrap.public InputBitStream(byte[] a, int off, int len)
a
- the byte array to wrap.off
- the byte offset of the first addressable byte in the array.len
- the #of addressable bytes in the array.public InputBitStream(String name, int bufSize) throws FileNotFoundException
This constructor invokes directly FileInputStream.getChannel()
to support position(long)
.
name
- the name of the file.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.FileNotFoundException
public InputBitStream(String name) throws FileNotFoundException
This constructor invokes directly FileInputStream.getChannel()
to support position(long)
.
name
- the name of the file.FileNotFoundException
public InputBitStream(File file) throws FileNotFoundException
This constructor invokes directly FileInputStream.getChannel()
to support position(long)
.
file
- the file.FileNotFoundException
public InputBitStream(File file, int bufSize) throws FileNotFoundException
This constructor invokes directly FileInputStream.getChannel()
to support position(long)
.
file
- the file.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.FileNotFoundException
public void flush()
This method is provided so that users of this class can easily wrap repositionable
streams (for instance, file-based streams, which can be repositioned using
the underlying FileChannel
). It is guaranteed that after calling
this method the underlying stream can be repositioned, and that the next read
will draw data from the stream.
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public long available() throws IOException
IOException
public long readBits()
public void readBits(long readBits)
This method is provided so that, for instance, the
user can reset via readBits(0)
the read-bits count
after a flush()
.
readBits
- the new value for the number of bits read so far.public void align()
public void read(byte[] bits, int len) throws IOException
bits
- an array of bytes to store the result.len
- the number of bits to read.IOException
public int readBit() throws IOException
IOException
public int readInt(int len) throws IOException
len
- a bit length.len
bits are taken from the stream; the rest is zeroed.IOException
public long readLong(int len) throws IOException
len
- a bit length.len
bits are taken from the stream; the rest is zeroed.IOException
public long skip(long n) throws IOException
n
- the number of bits to skip.IOException
public void position(long position) throws IOException
RepositionableStream
or on a FileChannel
.
Given an underlying stream that implements RepositionableStream
or that can provide a FileChannel
via the getChannel()
method,
a call to this method has the same semantics of a flush()
,
followed by a call to position(position / 8)
on
the byte stream, followed by a skip(position % 8)
.
position
- the new position expressed as a bit offset.UnsupportedOperationException
- if the underlying byte stream does not implement
RepositionableStream
and if the channel it returns is not a FileChannel
.IOException
FileChannel.position(long)
public boolean markSupported()
mark(int)
and reset()
methods.
This method will just delegate the test to the underlying InputStream
.
public void mark(int readLimit) throws IOException
reset()
method repositions this stream at the last marked position so
that subsequent reads re-read the same bits.
This method will just delegate the mark to the underlying InputStream
.
Moreover, it will throw an exception if you try to mark outsite byte boundaries.
readLimit
- the maximum limit of bytes that can be read before the mark position becomes invalid.IOException
- if you try to mark outside byte boundaries.public void reset() throws IOException
mark(int)
method was last called.
This method will just flush the stream
and delegate
the reset to the underlying InputStream
.
IOException
public int readUnary() throws IOException
IOException
OutputBitStream.writeUnary(int)
public long readLongUnary() throws IOException
IOException
OutputBitStream.writeUnary(int)
public int readGamma() throws IOException
IOException
OutputBitStream.writeGamma(int)
,
skipGammas(int)
public long readLongGamma() throws IOException
IOException
OutputBitStream.writeGamma(int)
,
skipGammas(int)
public void skipGammas(int n) throws IOException
This method should be significantly quicker than iterating n
times on
readGamma()
or readLongGamma()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced, and the result is discarded, so
skip(long)
can be invoked instead of more specific decoding methods.
n
- the number of γ-coded integers to be skipped.IOException
readGamma()
public void readGammas(int[] a, int count) throws IOException
This method should be significantly quicker than iterating n
times on
readGamma()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced.
a
- an array of at least count
integers where the result
wil be written starting at the first position.count
- the number of γ-coded integers to be read.IOException
readGamma()
public int readShiftedGamma() throws IOException
IOException
OutputBitStream.writeShiftedGamma(int)
,
skipShiftedGammas(int)
public long readLongShiftedGamma() throws IOException
IOException
OutputBitStream.writeShiftedGamma(int)
,
skipShiftedGammas(int)
public void skipShiftedGammas(int n) throws IOException
This method should be significantly quicker than iterating n
times on
readShiftedGamma()
or readLongShiftedGamma()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced, and the result is discarded, so
skip(long)
can be invoked instead of more specific decoding methods.
n
- the number of shited-γ-coded integers to be skipped.IOException
readShiftedGamma()
public void readShiftedGammas(int[] a, int count) throws IOException
This method should be significantly quicker than iterating n
times on
readShiftedGamma()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced.
a
- an array of at least count
integers where the result
wil be written starting at the first position.count
- the number of shifted-γ-coded integers to be read.IOException
readShiftedGamma()
public int readDelta() throws IOException
IOException
OutputBitStream.writeDelta(int)
,
skipDeltas(int)
public long readLongDelta() throws IOException
IOException
OutputBitStream.writeDelta(int)
,
skipDeltas(int)
public void skipDeltas(int n) throws IOException
This method should be significantly quicker than iterating n
times on
readDelta()
or readLongDelta()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced, and the result is discarded, so
skip(long)
can be invoked instead of more specific decoding methods.
n
- the number of δ-coded integers to be skipped.IOException
readDelta()
public void readDeltas(int[] a, int count) throws IOException
This method should be significantly quicker than iterating n
times on
readDelta()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced.
a
- an array of at least count
integers where the result
wil be written starting at the first position.count
- the number of δ-coded integers to be read.IOException
readDelta()
public int readMinimalBinary(int b) throws IOException
b
- a strict upper bound.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.IOException
OutputBitStream.writeMinimalBinary(int, int)
public int readMinimalBinary(int b, int log2b) throws IOException
readMinimalBinary(int)
because it does not
have to compute log2b
.b
- a strict upper bound.log2b
- the floor of the base-2 logarithm of the bound.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.IOException
OutputBitStream.writeMinimalBinary(int, int)
public long readLongMinimalBinary(long b) throws IOException
b
- a strict upper bound.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.IOException
OutputBitStream.writeMinimalBinary(int, int)
public long readLongMinimalBinary(long b, int log2b) throws IOException
readLongMinimalBinary(long)
because it does not
have to compute log2b
.b
- a strict upper bound.log2b
- the floor of the base-2 logarithm of the bound.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.IOException
OutputBitStream.writeMinimalBinary(int, int)
public int readGolomb(int b) throws IOException
This method implements also the case in which b
is 0: in this case,
nothing will be read, and 0 will be returned.
b
- the modulus for the coding.IllegalArgumentException
- if you use a nonpositive modulus.IOException
OutputBitStream.writeGolomb(int, int)
public int readGolomb(int b, int log2b) throws IOException
readGolomb(int)
because it does not
have to compute log2b
.
This method implements also the case in which b
is 0: in this case,
nothing will be read, and 0 will be returned.
b
- the modulus for the coding.log2b
- the floor of the base-2 logarithm of the coding modulus.IllegalArgumentException
- if you use a nonpositive modulus.IOException
OutputBitStream.writeGolomb(int, int)
public long readLongGolomb(long b) throws IOException
This method implements also the case in which b
is 0: in this case,
nothing will be read, and 0 will be returned.
b
- the modulus for the coding.IllegalArgumentException
- if you use a nonpositive modulus.IOException
OutputBitStream.writeGolomb(int, int)
public long readLongGolomb(long b, int log2b) throws IOException
readLongGolomb(long)
because it does not
have to compute log2b
.
This method implements also the case in which b
is 0: in this case,
nothing will be read, and 0 will be returned.
b
- the modulus for the coding.log2b
- the floor of the base-2 logarithm of the coding modulus.IllegalArgumentException
- if you use a nonpositive modulus.IOException
OutputBitStream.writeGolomb(int, int)
public int readSkewedGolomb(int b) throws IOException
This method implements also the case in which b
is 0: in this case,
nothing will be read, and 0 will be returned.
b
- the modulus for the coding.IllegalArgumentException
- if you use a negative modulus.IOException
OutputBitStream.writeSkewedGolomb(int, int)
public long readLongSkewedGolomb(long b) throws IOException
This method implements also the case in which b
is 0: in this case,
nothing will be read, and 0 will be returned.
b
- the modulus for the coding.IllegalArgumentException
- if you use a negative modulus.IOException
OutputBitStream.writeSkewedGolomb(int, int)
public int readZeta(int k) throws IOException
k
- the shrinking factor.IllegalArgumentException
- if you use a nonpositive shrinking factor.IOException
OutputBitStream.writeZeta(int, int)
public long readLongZeta(int k) throws IOException
k
- the shrinking factor.IllegalArgumentException
- if you use a nonpositive shrinking factor.IOException
OutputBitStream.writeZeta(int, int)
public void skipZetas(int k, int n) throws IOException
This method should be significantly quicker than iterating n
times on
readZeta(int)
or readLongZeta(int)
, as precomputed tables are used directly,
so the number of method calls is greatly reduced, and the result is discarded, so
skip(long)
can be invoked instead of more specific decoding methods.
k
- the shrinking factor.n
- the number of ζ-coded integers to be skipped.IOException
readZeta(int)
public void readZetas(int k, int[] a, int count) throws IOException
This method should be significantly quicker than iterating n
times on
readGamma()
, as precomputed tables are used directly,
so the number of method calls is greatly reduced.
k
- the shrinking factor.a
- an array of at least count
integers where the result
wil be written starting at the first position.count
- the number of ζ-coded integers to be read.IOException
readGamma()
public int readNibble() throws IOException
IOException
OutputBitStream.writeNibble(int)
public long readLongNibble() throws IOException
IOException
OutputBitStream.writeNibble(int)
public boolean nextBoolean()
nextBoolean
in interface it.unimi.dsi.fastutil.booleans.BooleanIterator
nextBoolean
in class it.unimi.dsi.fastutil.booleans.AbstractBooleanIterator
@Deprecated public int skip(int n)
skip(long)
that is made necessary by the interface
by BooleanIterator
.skip
in interface it.unimi.dsi.fastutil.booleans.BooleanIterator
skip
in class it.unimi.dsi.fastutil.booleans.AbstractBooleanIterator
n
- the number of bits to skip.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.