Class SegmentedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- it.unimi.dsi.fastutil.io.MeasurableInputStream
-
- it.unimi.dsi.io.SegmentedInputStream
-
- All Implemented Interfaces:
it.unimi.dsi.fastutil.io.MeasurableStream,java.io.Closeable,java.lang.AutoCloseable
public class SegmentedInputStream extends it.unimi.dsi.fastutil.io.MeasurableInputStreamExhibits a singleInputStreamas a number of streams divided intoreset()-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 callingreset()moves from one segment to the following one, whereas callingclose()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
Constructors Constructor Description SegmentedInputStream(java.io.InputStream in)Creates a segmented input stream with no markers.SegmentedInputStream(java.io.InputStream in, long... delimiter)Creats a stream with one marker in.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBlock(long... delimiter)Adds a new block defined by its array of segment delimiters.intavailable()voidclose()Skips to the next block, closing this segmented input stream if there are no more blocks.booleanhasMoreBlocks()Checks whether there are more blocks.longlength()voidnextBlock()Skips to the first segment of the next block, if any.longposition()intread()intread(byte[] b, int off, int len)voidreset()Moves into the next segment of the current block.longskip(long n)
-
-
-
Constructor Detail
-
SegmentedInputStream
public SegmentedInputStream(java.io.InputStream in)
Creates a segmented input stream with no markers.- Parameters:
in- the underlying input stream.
-
SegmentedInputStream
public SegmentedInputStream(java.io.InputStream in, long... delimiter) throws java.lang.NullPointerException, java.io.IOException, java.lang.IllegalStateExceptionCreats a stream with one marker in.- Parameters:
in- the underlying input stream.delimiter- an array of segment delimiters.- Throws:
java.lang.NullPointerExceptionjava.io.IOExceptionjava.lang.IllegalStateException
-
-
Method Detail
-
nextBlock
public void nextBlock() throws java.io.IOExceptionSkips to the first segment of the next block, if any. In such case, it returns true, or false otherwise.- Throws:
java.io.IOException
-
hasMoreBlocks
public boolean hasMoreBlocks()
Checks whether there are more blocks.- Returns:
- true if we there are more blocks.
-
addBlock
public void addBlock(long... delimiter) throws java.lang.IllegalArgumentException, java.io.IOExceptionAdds 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:
java.lang.IllegalArgumentException- if the elements ofdelimiterare negative or not increasing.java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
length
public long length() throws java.io.IOException- Throws:
java.io.IOException
-
position
public long position() throws java.io.IOException- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionSkips to the next block, closing this segmented input stream if there are no more blocks.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
reset
public void reset() throws java.io.IOExceptionMoves into the next segment of the current block.- Overrides:
resetin classjava.io.InputStream- Throws:
java.io.IOException
-
-