Class InputBitStream
- java.lang.Object
-
- it.unimi.dsi.io.InputBitStream
-
- All Implemented Interfaces:
it.unimi.dsi.fastutil.booleans.BooleanIterator
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
,java.util.Iterator<java.lang.Boolean>
,java.util.PrimitiveIterator<java.lang.Boolean,it.unimi.dsi.fastutil.booleans.BooleanConsumer>
- Direct Known Subclasses:
DebugInputBitStream
public class InputBitStream extends java.lang.Object implements it.unimi.dsi.fastutil.booleans.BooleanIterator, java.io.Flushable, java.io.Closeable
Bit-level input stream.This class wraps any
InputStream
so that you can treat it as bit stream. Constructors and methods closely resemble those ofInputStream
. Data can be read from such a stream in several ways: reading a (long) natural number 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 formark(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 anEOFException
.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 natural numbers using some coding, instead, they are stored in the standard way, that is, in the lower bits.Additional features:
- This class provides an internal buffer. By setting a buffer of length 0 at creation time, you can actually bypass the buffering system: Note, however, that several classes providing buffering have synchronised methods, so using a wrapper instead of the internal buffer is likely to lead to a performance drop.
- To work around the schizophrenic relationship between streams and random
access files in
java.io
, this class provides aflush()
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 theFileChannel
underlying the given input stream, in this order. If either reference can be successfully fetched, you can use directly theposition()
method with argumentpos
with the same semantics of aflush()
, followed by a call toposition(pos / 8)
(where the latter method belongs either to the underlying stream or to its underlying file channel), followed by askip(pos % 8)
. However, since the reflective checks are quite heavy they can be disabled using a suitable constructor. - Finally, this class implements partially the interface of a boolean iterator.
More precisely,
nextBoolean()
will return the same bit asreadBit()
, and also the same exceptions, whereashasNext()
will always return true: you must be prepared to catch aRuntimeException
wrapping anIOException
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.
- Since:
- 0.1
- Author:
- Sebastiano Vigna
- See Also:
InputStream
,OutputBitStream
-
-
Field Summary
Fields Modifier and Type Field 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 java.nio.channels.FileChannel
fileChannel
The cached file channel underlyingis
, if any.protected int
fill
Current number of bits in the bit buffer (stored low).static int[]
GAMMA
protected java.io.InputStream
is
The underlyingInputStream
.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
-
Constructor Summary
Constructors Modifier Constructor Description protected
InputBitStream()
This (non-public) constructor exists just to provide fake initialisation for classes such asDebugInputBitStream
.InputBitStream(byte[] a)
Creates a new input bit stream wrapping a given byte array.InputBitStream(java.io.File file)
Creates a new input bit stream reading from a file.InputBitStream(java.io.FileInputStream is)
Creates a new input bit stream wrapping a given file input stream using a buffer of sizeDEFAULT_BUFFER_SIZE
.InputBitStream(java.io.FileInputStream is, int bufSize)
Creates a new input bit stream wrapping a given file input stream with a specified buffer size.InputBitStream(java.io.File file, int bufSize)
Creates a new input bit stream reading from a file.InputBitStream(java.io.InputStream is)
Creates a new input bit stream wrapping a given input stream using a buffer of sizeDEFAULT_BUFFER_SIZE
.InputBitStream(java.io.InputStream is, boolean testForPosition)
Creates a new input bit stream wrapping a given input stream using a buffer of sizeDEFAULT_BUFFER_SIZE
.InputBitStream(java.io.InputStream is, int bufSize)
Creates a new input bit stream wrapping a given input stream with a specified buffer size.InputBitStream(java.io.InputStream is, int bufSize, boolean testForPosition)
Creates a new input bit stream wrapping a given input stream with a specified buffer size.InputBitStream(java.lang.String name)
Creates a new input bit stream reading from a file.InputBitStream(java.lang.String name, int bufSize)
Creates a new input bit stream reading from a file.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method 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.static boolean
checkLength(java.lang.String resource, int[] array, java.lang.String resouceFullPath)
void
close()
Closes the bit stream.void
copyTo(OutputBitStream obs, long length)
Copies a given number of bits from this input bit stream into a given output 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()
long
position()
Returns this stream bit position.void
position(long position)
Sets this stream bit position, if it is based on aRepositionableStream
or on aFileChannel
.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 amount of δ-coded natural numbers.int
readGamma()
Reads a natural number in γ coding.void
readGammas(int[] a, int count)
Reads a given amount of γ-coded natural numbers.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 amount of shifted-γ-coded natural numbers.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 amount of γ-coded natural numbers.void
reset()
Repositions this bit stream to the position at the time themark(int)
method was last called.int
skip(int n)
Deprecated.This method is simply an expensive, try/catch-surrounded version ofskip(long)
that is made necessary by the interface byBooleanIterator
.long
skip(long n)
Skips the given number of bits.void
skipDeltas(int n)
Skips a given amount of δ-coded natural numbers.void
skipDeltas(long n)
Skips a given amount of δ-coded natural numbers.void
skipGammas(int n)
Skips a given amount of γ-coded natural numbers.void
skipGammas(long n)
Skips a given amount of γ-coded natural numbers.void
skipShiftedGammas(int n)
Skips a given amount of shifted-γ-coded natural numbers.void
skipShiftedGammas(long n)
Skips a given amount of shifted-γ-coded natural numbers.void
skipZetas(int k, int n)
Skips a given amount of ζ-coded natural numbers.void
skipZetas(int k, long n)
Skips a given amount of ζ-coded natural numbers.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Field Detail
-
GAMMA
public static final int[] GAMMA
-
DELTA
public static final int[] DELTA
-
ZETA_3
public static final int[] ZETA_3
-
SHIFTED_GAMMA
public static final int[] SHIFTED_GAMMA
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default size of the byte buffer in bytes (8Ki).- See Also:
- Constant Field Values
-
is
protected final java.io.InputStream is
The underlyingInputStream
.
-
fileChannel
protected final java.nio.channels.FileChannel fileChannel
The cached file channel underlyingis
, if any.
-
repositionableStream
protected final it.unimi.dsi.fastutil.io.RepositionableStream repositionableStream
is
cast to a positionable stream, if possible.
-
wrapping
protected final boolean wrapping
True if we are wrapping an array.
-
buffer
protected byte[] buffer
The stream buffer.
-
fill
protected int fill
Current number of bits in the bit buffer (stored low).
-
pos
protected int pos
Current position in the byte buffer.
-
avail
protected int avail
Current number of bytes available in the byte buffer.
-
position
protected long position
Current position of the first byte in the byte buffer.
-
-
Constructor Detail
-
InputBitStream
protected InputBitStream()
This (non-public) constructor exists just to provide fake initialisation for classes such asDebugInputBitStream
.
-
InputBitStream
public InputBitStream(java.io.InputStream is)
Creates a new input bit stream wrapping a given input stream using a buffer of sizeDEFAULT_BUFFER_SIZE
.This constructor performs the reflective tests that are necessary to support
position(long)
.- Parameters:
is
- the input stream to wrap.
-
InputBitStream
public InputBitStream(java.io.InputStream is, boolean testForPosition)
Creates a new input bit stream wrapping a given input stream using a buffer of sizeDEFAULT_BUFFER_SIZE
.- Parameters:
is
- the input stream to wrap.testForPosition
- if false, the reflective test that is necessary to supportposition(long)
in caseis
does not implementRepositionableStream
will not be performed.
-
InputBitStream
public InputBitStream(java.io.InputStream is, int bufSize)
Creates a new input bit stream wrapping a given input stream with a specified buffer size.This constructor performs the reflective tests that are necessary to support
position(long)
.- Parameters:
is
- the input stream to wrap.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.
-
InputBitStream
public InputBitStream(java.io.InputStream is, int bufSize, boolean testForPosition)
Creates a new input bit stream wrapping a given input stream with a specified buffer size.- Parameters:
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 test that is necessary to supportposition(long)
in caseis
does not implementRepositionableStream
will not be performed.
-
InputBitStream
public InputBitStream(java.io.FileInputStream is)
Creates a new input bit stream wrapping a given file input stream using a buffer of sizeDEFAULT_BUFFER_SIZE
.This constructor invokes directly
FileInputStream.getChannel()
to supportposition(long)
.- Parameters:
is
- the file input stream to wrap.
-
InputBitStream
public InputBitStream(java.io.FileInputStream is, int bufSize)
Creates a new input bit stream wrapping a given file input stream with a specified buffer size.This constructor invokes directly
FileInputStream.getChannel()
to supportposition(long)
.- Parameters:
is
- the file input stream to wrap.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.
-
InputBitStream
public InputBitStream(byte[] a)
Creates a new input bit stream wrapping a given byte array.- Parameters:
a
- the byte array to wrap.
-
InputBitStream
public InputBitStream(java.lang.String name, int bufSize) throws java.io.FileNotFoundException
Creates a new input bit stream reading from a file.This constructor invokes directly
FileInputStream.getChannel()
to supportposition(long)
.- Parameters:
name
- the name of the file.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.- Throws:
java.io.FileNotFoundException
-
InputBitStream
public InputBitStream(java.lang.String name) throws java.io.FileNotFoundException
Creates a new input bit stream reading from a file.This constructor invokes directly
FileInputStream.getChannel()
to supportposition(long)
.- Parameters:
name
- the name of the file.- Throws:
java.io.FileNotFoundException
-
InputBitStream
public InputBitStream(java.io.File file) throws java.io.FileNotFoundException
Creates a new input bit stream reading from a file.This constructor invokes directly
FileInputStream.getChannel()
to supportposition(long)
.- Parameters:
file
- the file.- Throws:
java.io.FileNotFoundException
-
InputBitStream
public InputBitStream(java.io.File file, int bufSize) throws java.io.FileNotFoundException
Creates a new input bit stream reading from a file.This constructor invokes directly
FileInputStream.getChannel()
to supportposition(long)
.- Parameters:
file
- the file.bufSize
- the size in byte of the buffer; it may be 0, denoting no buffering.- Throws:
java.io.FileNotFoundException
-
-
Method Detail
-
checkLength
public static boolean checkLength(java.lang.String resource, int[] array, java.lang.String resouceFullPath)
-
flush
public void flush()
Flushes the bit stream. All state information associated with the stream is reset. This includes bytes prefetched from the stream, bits in the bit buffer and unget'd bits.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.- Specified by:
flush
in interfacejava.io.Flushable
-
close
public void close() throws java.io.IOException
Closes the bit stream. All resources associated with the stream are released.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
available
public long available() throws java.io.IOException
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.- Returns:
- the number of bits that can be read from this bit stream without blocking.
- Throws:
java.io.IOException
-
readBits
public long readBits()
Returns the number of bits read from this bit stream.- Returns:
- the number of bits read so far.
-
readBits
public void readBits(long readBits)
Sets the number of bits read from this bit stream.This method is provided so that, for instance, the user can reset via
readBits(0)
the read-bits count after aflush()
.- Parameters:
readBits
- the new value for the number of bits read so far.
-
align
public void align()
Aligns the stream. After a call to this function, the stream is byte aligned. Bits that have been read to align are discarded.
-
read
public void read(byte[] bits, int len) throws java.io.IOException
Reads a sequence of bits. Bits will be read in the natural way: the first bit is bit 7 of the first byte, the eightth bit is bit 0 of the first byte, the ninth bit is bit 7 of the second byte and so on.- Parameters:
bits
- an array of bytes to store the result.len
- the number of bits to read.- Throws:
java.io.IOException
-
readBit
public int readBit() throws java.io.IOException
Reads a bit.- Returns:
- the next bit from the stream.
- Throws:
java.io.IOException
-
readInt
public int readInt(int len) throws java.io.IOException
Reads a fixed number of bits into an integer.- Parameters:
len
- a bit length.- Returns:
- an integer whose lower
len
bits are taken from the stream; the rest is zeroed. - Throws:
java.io.IOException
-
readLong
public long readLong(int len) throws java.io.IOException
Reads a fixed number of bits into a long.- Parameters:
len
- a bit length.- Returns:
- a long whose lower
len
bits are taken from the stream; the rest is zeroed. - Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException
Skips the given number of bits.- Parameters:
n
- the number of bits to skip.- Returns:
- the actual number of skipped bits.
- Throws:
java.io.IOException
-
position
public void position(long position) throws java.io.IOException
Sets this stream bit position, if it is based on aRepositionableStream
or on aFileChannel
.Given an underlying stream that implements
RepositionableStream
or that can provide aFileChannel
via thegetChannel()
method, a call to this method has the same semantics of aflush()
, followed by a call toposition(position / 8)
on the byte stream, followed by askip(position % 8)
.Note that this method does not change the value returned by
readBits()
.- Parameters:
position
- the new position expressed as a bit offset.- Throws:
java.lang.UnsupportedOperationException
- if the underlying byte stream does not implementRepositionableStream
or if the channel it returns is not aFileChannel
.java.io.IOException
- See Also:
FileChannel.position(long)
-
position
public long position()
Returns this stream bit position.- Returns:
- this stream bit position.
-
markSupported
public boolean markSupported()
-
mark
public void mark(int readLimit) throws java.io.IOException
Marks the current position in this input stream. A subsequent call to thereset()
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.- Parameters:
readLimit
- the maximum limit of bytes that can be read before the mark position becomes invalid.- Throws:
java.io.IOException
- if you try to mark outside byte boundaries.
-
reset
public void reset() throws java.io.IOException
Repositions this bit stream to the position at the time themark(int)
method was last called.This method will just
flush the stream
and delegate the reset to the underlyingInputStream
.- Throws:
java.io.IOException
-
readUnary
public int readUnary() throws java.io.IOException
Reads a natural number in unary coding.- Returns:
- the next unary-encoded natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeUnary(int)
-
readLongUnary
public long readLongUnary() throws java.io.IOException
Reads a long natural number in unary coding. Note that by unary coding we mean that 1 encodes 0, 01 encodes 1 and so on.- Returns:
- the next unary-encoded long natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeUnary(int)
-
readGamma
public int readGamma() throws java.io.IOException
Reads a natural number in γ coding.- Returns:
- the next γ-encoded natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeGamma(int)
,skipGammas(int)
-
readLongGamma
public long readLongGamma() throws java.io.IOException
Reads a long natural number in γ coding.- Returns:
- the next γ-encoded long natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeGamma(int)
,skipGammas(int)
-
skipGammas
public void skipGammas(long n) throws java.io.IOException
Skips a given amount of γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadGamma()
orreadLongGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
n
- the number of γ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readGamma()
-
skipGammas
public void skipGammas(int n) throws java.io.IOException
Skips a given amount of γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadGamma()
orreadLongGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
n
- the number of γ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readGamma()
-
readGammas
public void readGammas(int[] a, int count) throws java.io.IOException
Reads a given amount of γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced.- Parameters:
a
- an array of at leastcount
integers where the result will be written starting at the first position.count
- the number of γ-coded natural numbers to be read.- Throws:
java.io.IOException
- See Also:
readGamma()
-
readShiftedGamma
public int readShiftedGamma() throws java.io.IOException
Reads a natural number in shifted γ coding.- Returns:
- the next shifted-γ–encoded natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeShiftedGamma(int)
,skipShiftedGammas(int)
-
readLongShiftedGamma
public long readLongShiftedGamma() throws java.io.IOException
Reads a natural number in shifted γ coding.- Returns:
- the next shifted-γ–encoded natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeShiftedGamma(int)
,skipShiftedGammas(int)
-
skipShiftedGammas
public void skipShiftedGammas(long n) throws java.io.IOException
Skips a given amount of shifted-γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadShiftedGamma()
orreadLongShiftedGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
n
- the number of shifted-γ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readShiftedGamma()
-
skipShiftedGammas
public void skipShiftedGammas(int n) throws java.io.IOException
Skips a given amount of shifted-γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadShiftedGamma()
orreadLongShiftedGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
n
- the number of shifted-γ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readShiftedGamma()
-
readShiftedGammas
public void readShiftedGammas(int[] a, int count) throws java.io.IOException
Reads a given amount of shifted-γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadShiftedGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced.- Parameters:
a
- an array of at leastcount
integers where the result will be written starting at the first position.count
- the number of shifted-γ-coded natural numbers to be read.- Throws:
java.io.IOException
- See Also:
readShiftedGamma()
-
readDelta
public int readDelta() throws java.io.IOException
Reads a natural number in δ coding.- Returns:
- the next δ-encoded natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeDelta(int)
,skipDeltas(int)
-
readLongDelta
public long readLongDelta() throws java.io.IOException
Reads a long natural number in δ coding.- Returns:
- the next δ-encoded long natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeDelta(int)
,skipDeltas(int)
-
skipDeltas
public void skipDeltas(long n) throws java.io.IOException
Skips a given amount of δ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadDelta()
orreadLongDelta()
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
n
- the number of δ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readDelta()
-
skipDeltas
public void skipDeltas(int n) throws java.io.IOException
Skips a given amount of δ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadDelta()
orreadLongDelta()
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
n
- the number of δ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readDelta()
-
readDeltas
public void readDeltas(int[] a, int count) throws java.io.IOException
Reads a given amount of δ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadDelta()
, as precomputed tables are used directly, so the number of method calls is greatly reduced.- Parameters:
a
- an array of at leastcount
integers where the result will be written starting at the first position.count
- the number of δ-coded natural numbers to be read.- Throws:
java.io.IOException
- See Also:
readDelta()
-
readMinimalBinary
public int readMinimalBinary(int b) throws java.io.IOException
Reads a natural number in a limited range using a minimal binary coding.- Parameters:
b
- a strict upper bound.- Returns:
- the next minimally binary encoded natural number.
- Throws:
java.lang.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.java.io.IOException
- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
readMinimalBinary
public int readMinimalBinary(int b, int log2b) throws java.io.IOException
Reads a natural number in a limited range using a minimal binary coding. This method is faster thanreadMinimalBinary(int)
because it does not have to computelog2b
.- Parameters:
b
- a strict upper bound.log2b
- the floor of the base-2 logarithm of the bound.- Returns:
- the next minimally binary encoded natural number.
- Throws:
java.lang.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.java.io.IOException
- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
readLongMinimalBinary
public long readLongMinimalBinary(long b) throws java.io.IOException
Reads a long natural number in a limited range using a minimal binary coding.- Parameters:
b
- a strict upper bound.- Returns:
- the next minimally binary encoded long natural number.
- Throws:
java.lang.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.java.io.IOException
- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
readLongMinimalBinary
public long readLongMinimalBinary(long b, int log2b) throws java.io.IOException
Reads a long natural number in a limited range using a minimal binary coding. This method is faster thanreadLongMinimalBinary(long)
because it does not have to computelog2b
.- Parameters:
b
- a strict upper bound.log2b
- the floor of the base-2 logarithm of the bound.- Returns:
- the next minimally binary encoded long natural number.
- Throws:
java.lang.IllegalArgumentException
- if you try to read a negative number or use a nonpositive base.java.io.IOException
- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
readGolomb
public int readGolomb(int b) throws java.io.IOException
Reads a natural number in Golomb coding.This method implements also the case in which
b
is 0: in this case, nothing will be read, and 0 will be returned.- Parameters:
b
- the modulus for the coding.- Returns:
- the next Golomb-encoded natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a nonpositive modulus.java.io.IOException
- See Also:
OutputBitStream.writeGolomb(int, int)
-
readGolomb
public int readGolomb(int b, int log2b) throws java.io.IOException
Reads a natural number in Golomb coding. This method is faster thanreadGolomb(int)
because it does not have to computelog2b
.This method implements also the case in which
b
is 0: in this case, nothing will be read, and 0 will be returned.- Parameters:
b
- the modulus for the coding.log2b
- the floor of the base-2 logarithm of the coding modulus.- Returns:
- the next Golomb-encoded natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a nonpositive modulus.java.io.IOException
- See Also:
OutputBitStream.writeGolomb(int, int)
-
readLongGolomb
public long readLongGolomb(long b) throws java.io.IOException
Reads a long natural number in Golomb coding.This method implements also the case in which
b
is 0: in this case, nothing will be read, and 0 will be returned.- Parameters:
b
- the modulus for the coding.- Returns:
- the next Golomb-encoded long natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a nonpositive modulus.java.io.IOException
- See Also:
OutputBitStream.writeGolomb(int, int)
-
readLongGolomb
public long readLongGolomb(long b, int log2b) throws java.io.IOException
Reads a long natural number in Golomb coding. This method is faster thanreadLongGolomb(long)
because it does not have to computelog2b
.This method implements also the case in which
b
is 0: in this case, nothing will be read, and 0 will be returned.- Parameters:
b
- the modulus for the coding.log2b
- the floor of the base-2 logarithm of the coding modulus.- Returns:
- the next Golomb-encoded long natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a nonpositive modulus.java.io.IOException
- See Also:
OutputBitStream.writeGolomb(int, int)
-
readSkewedGolomb
public int readSkewedGolomb(int b) throws java.io.IOException
Reads a natural number in skewed Golomb coding.This method implements also the case in which
b
is 0: in this case, nothing will be read, and 0 will be returned.- Parameters:
b
- the modulus for the coding.- Returns:
- the next skewed Golomb-encoded natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a negative modulus.java.io.IOException
- See Also:
OutputBitStream.writeSkewedGolomb(int, int)
-
readLongSkewedGolomb
public long readLongSkewedGolomb(long b) throws java.io.IOException
Reads a long natural number in skewed Golomb coding.This method implements also the case in which
b
is 0: in this case, nothing will be read, and 0 will be returned.- Parameters:
b
- the modulus for the coding.- Returns:
- the next skewed Golomb-encoded long natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a negative modulus.java.io.IOException
- See Also:
OutputBitStream.writeSkewedGolomb(int, int)
-
readZeta
public int readZeta(int k) throws java.io.IOException
Reads a natural number in ζ coding.- Parameters:
k
- the shrinking factor.- Returns:
- the next ζ-encoded natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a nonpositive shrinking factor.java.io.IOException
- See Also:
OutputBitStream.writeZeta(int, int)
-
readLongZeta
public long readLongZeta(int k) throws java.io.IOException
Reads a long natural number in ζ coding.- Parameters:
k
- the shrinking factor.- Returns:
- the next ζ-encoded long natural number.
- Throws:
java.lang.IllegalArgumentException
- if you use a nonpositive shrinking factor.java.io.IOException
- See Also:
OutputBitStream.writeZeta(int, int)
-
skipZetas
public void skipZetas(int k, long n) throws java.io.IOException
Skips a given amount of ζ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadZeta(int)
orreadLongZeta(int)
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
k
- the shrinking factor.n
- the number of ζ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readZeta(int)
-
skipZetas
public void skipZetas(int k, int n) throws java.io.IOException
Skips a given amount of ζ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadZeta(int)
orreadLongZeta(int)
, as precomputed tables are used directly, so the number of method calls is greatly reduced, and the result is discarded, soskip(long)
can be invoked instead of more specific decoding methods.- Parameters:
k
- the shrinking factor.n
- the number of ζ-coded natural numbers to be skipped.- Throws:
java.io.IOException
- See Also:
readZeta(int)
-
readZetas
public void readZetas(int k, int[] a, int count) throws java.io.IOException
Reads a given amount of γ-coded natural numbers.This method should be significantly quicker than iterating
n
times onreadGamma()
, as precomputed tables are used directly, so the number of method calls is greatly reduced.- Parameters:
k
- the shrinking factor.a
- an array of at leastcount
integers where the result will be written starting at the first position.count
- the number of ζ-coded natural numbers to be read.- Throws:
java.io.IOException
- See Also:
readGamma()
-
readNibble
public int readNibble() throws java.io.IOException
Reads a natural number in variable-length nibble coding.- Returns:
- the next variable-length nibble-encoded natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeNibble(int)
-
readLongNibble
public long readLongNibble() throws java.io.IOException
Reads a long natural number in variable-length nibble coding.- Returns:
- the next variable-length nibble-encoded long natural number.
- Throws:
java.io.IOException
- See Also:
OutputBitStream.writeNibble(int)
-
hasNext
public boolean hasNext()
- Specified by:
hasNext
in interfacejava.util.Iterator<java.lang.Boolean>
-
nextBoolean
public boolean nextBoolean()
- Specified by:
nextBoolean
in interfaceit.unimi.dsi.fastutil.booleans.BooleanIterator
-
skip
@Deprecated public int skip(int n)
Deprecated.This method is simply an expensive, try/catch-surrounded version ofskip(long)
that is made necessary by the interface byBooleanIterator
.Skips over the given number of bits.- Specified by:
skip
in interfaceit.unimi.dsi.fastutil.booleans.BooleanIterator
- Parameters:
n
- the number of bits to skip.- Returns:
- the number of bits actually skipped.
-
copyTo
public void copyTo(OutputBitStream obs, long length) throws java.io.IOException
Copies a given number of bits from this input bit stream into a given output bit stream.- Parameters:
obs
- an output bit stream.length
- the number of bits to copy.- Throws:
java.io.EOFException
- if there are not enough bits to copy.java.io.IOException
-
-