Package it.unimi.dsi.law.warc.io
Class BoundedCountingInputStream
java.lang.Object
java.io.InputStream
it.unimi.dsi.fastutil.io.MeasurableInputStream
it.unimi.dsi.law.warc.io.BoundedCountingInputStream
- All Implemented Interfaces:
MeasurableStream
,Closeable
,AutoCloseable
public class BoundedCountingInputStream extends MeasurableInputStream
A class that decorates an
InputStream
to obtain a
MeasurableInputStream
.
This class serves two purpose: wrap an input stream so that no more than a certain number of
bytes may be read from it (and that the total number of bytes read so far is available). Moreover,
if a CRC32
is given, the content is also checked using it.
Observe that the underlying stream can be null
if the empty constructor is
called but setInput(InputStream, long, CRC32)
is not called; this will lead to
NullPointerException
in almost every call.
-
Field Summary
-
Constructor Summary
Constructors Constructor Description BoundedCountingInputStream()
Builds the bounded stream.BoundedCountingInputStream(InputStream is, long bound)
Builds the bounded stream.BoundedCountingInputStream(InputStream is, long bound, CRC32 crc)
Builds the bounded stream. -
Method Summary
Modifier and Type Method Description int
available()
long
length()
long
position()
int
read()
int
read(byte[] buf, int offset, int length)
void
setInput(InputStream is, long bound)
Resets the bounded stream fields, for reusing it.void
setInput(InputStream is, long bound, CRC32 crc)
Resets the bounded stream fields, for reusing it.Methods inherited from class java.io.InputStream
close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Field Details
-
crc
A class to compute the crc of read bytes.
-
-
Constructor Details
-
BoundedCountingInputStream
public BoundedCountingInputStream()Builds the bounded stream.Before actually using an object constructed with this,
setInput(InputStream, long, CRC32)
must be called. -
BoundedCountingInputStream
Builds the bounded stream.- Parameters:
is
- the stream.bound
- the maximum number of bytes that can be read.crc
- if notnull
, it will be used to compute the crc of read bytes.- Throws:
IOException
-
BoundedCountingInputStream
Builds the bounded stream.- Parameters:
is
- the stream.bound
- the maximum number of bytes that can be read.- Throws:
IOException
-
-
Method Details
-
setInput
Resets the bounded stream fields, for reusing it.- Parameters:
is
- the stream.bound
- the maximum number of bytes that can be read.crc
- if notnull
, it will be used to compute the crc of read bytes.- Throws:
IOException
-
setInput
Resets the bounded stream fields, for reusing it.- Parameters:
is
- the stream.bound
- the maximum number of bytes that can be read.- Throws:
IOException
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
length
- Throws:
IOException
-
position
public long position()
-