Class SegmentedInputStream
- All Implemented Interfaces:
MeasurableStream
,Closeable
,AutoCloseable
InputStream
as a number of streams divided into reset()
-separated
segments.
An instance of this class wraps a given input stream (usually a replicable one, such as
a FileInputStream
) and exposes its contents as a number of separated input
streams. Each such stream, called a block, is defined by a start and a stop position (gaps
between blocks are possible). Inside each block we can have one or more segments: each
segment is again a separate input stream, but calling reset()
moves
from one segment to the following one, whereas calling close()
moves from a block to the following one.
An instance of this class is enriched with blocks by calling addBlock(long[])
. This
can also be done on-the-fly, while the underlying input stream is being scanned.
- Author:
- Alessio Orlandi, Luca Natali, Sebastiano Vigna
-
Constructor Summary
ConstructorDescriptionCreates a segmented input stream with no markers.SegmentedInputStream
(InputStream in, long... delimiter) Creats a stream with one marker in. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBlock
(long... delimiter) Adds a new block defined by its array of segment delimiters.int
void
close()
Skips to the next block, closing this segmented input stream if there are no more blocks.boolean
Checks whether there are more blocks.long
length()
void
Skips to the first segment of the next block, if any.long
position()
int
read()
int
read
(byte[] b, int off, int len) void
reset()
Moves into the next segment of the current block.long
skip
(long n) Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
SegmentedInputStream
Creates a segmented input stream with no markers.- Parameters:
in
- the underlying input stream.
-
SegmentedInputStream
public SegmentedInputStream(InputStream in, long... delimiter) throws NullPointerException, IOException, IllegalStateException Creats a stream with one marker in.- Parameters:
in
- the underlying input stream.delimiter
- an array of segment delimiters.- Throws:
NullPointerException
IOException
IllegalStateException
-
-
Method Details
-
nextBlock
Skips to the first segment of the next block, if any. In such case, it returns true, or false otherwise.- Throws:
IOException
-
hasMoreBlocks
public boolean hasMoreBlocks()Checks whether there are more blocks.- Returns:
- true if we there are more blocks.
-
addBlock
Adds a new block defined by its array of segment delimiters.The block has length defined by the difference between the last and first delimiter.
This method performs the initial call to
nextBlock()
when the first marker is put in.- Parameters:
delimiter
- a list of segment delimiters.- Throws:
IllegalArgumentException
- if the elements ofdelimiter
are negative or not increasing.IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
length
- Throws:
IOException
-
position
- Throws:
IOException
-
close
Skips to the next block, closing this segmented input stream if there are no more blocks.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
reset
Moves into the next segment of the current block.- Overrides:
reset
in classInputStream
- Throws:
IOException
-