public final class ChunkInputStream extends InputStream
This class is useful when you have an InputStream
and you want
to filter some parts of it basing on its content without reading it into
memory.
Basically it strips the initial bytes of the stream until a sequence of
bytes equal to startMarker
is found. When a sequence of bytes
equals to endMarker
is found the stream hide the bytes until a
new startMarker
is found.
startMarker
and
endMarker
are shown to the outer stream through the
read()
methods.
Example:
InputStream is = new ByteArrayInputStream("aa start bbb stopfff".getBytes()); ChunckInputStream chunkIs = new ChunkInputStream("rt".getBytes(), "stop".getBytes(), is); byte[] bytes = IOUtils.toByteArray(chunkIs); //here bytes contains " bbb "
The class has three operational modes. They can be selected invoking the constructor with four parameters. These two modes affect how this class handles multiple chunks in a file.
automaticFetch=true
(default). After an
endMarker
is found the stream automatically moves to the next
startMarker
if found. Usage pattern is shown in the example
above.automaticFetch=false
Each chunk need to be fetched
invoking explicitly a fetchNextChunk()
methods. The
stream is initially in an EOF state and
fetchNextChunk()
must be called at first. At this
point all the bytes of the stream are shown until an endMarker
is found. At this point the ChunkInputStream is in an EOF state until a
fetchNextChunk()
is invoked.automaticFetch=false
and startMarker=null
It
is similar to the previous case. It can be used to the src stream on the
endMarker
s
Example of automaticFetch=false
mode:
InputStream is = new ByteArrayInputStream("aa start bbb stopfff".getBytes()); ChunckInputStream chunkIs = new ChunkInputStream(is, "rt" .getBytes(), "stop".getBytes(), false, false); while (chunkIs.moveToNextChunk()) { byte[] bytes = IOUtils.toByteArray(chunkIs); //here bytes contains " bbb " }
Constructor and Description |
---|
ChunkInputStream(InputStream src,
byte[] startMarker,
byte[] stopMarker)
Constructs a
ChunkInputStream . |
ChunkInputStream(InputStream src,
byte[] startMarker,
byte[] stopMarker,
boolean showMarkers,
boolean automaticFetch)
Gets an instance of the ChunkInputStream.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
.
|
void |
close()
.
|
boolean |
fetchNextChunk()
This method must be called if
automaticFetch=false before
the stream can be used and each time an endMarker has been found to
proceed to next startMarker. |
void |
mark(int readlimit)
.
|
boolean |
markSupported()
.
|
int |
read()
.
|
int |
read(byte[] b)
.
|
int |
read(byte[] b,
int off,
int len)
.
|
void |
reset()
.
|
long |
skip(long n)
.
|
public ChunkInputStream(InputStream src, byte[] startMarker, byte[] stopMarker)
ChunkInputStream
.src
- Source InputStream. Must not be null
.startMarker
- When this sequence of bytes is found in the src
stream the bytes of the inner stream are shown until an
endMarker
is found. If this parameter is set to
null
the stream is initially in an EOF state
until a fetchNextChunk()
is performed.stopMarker
- when this sequence of bytes is found in the src
stream the bytes of the inner stream are hidden until a
startMarker
is found. If this parameter is set
to null
the stream is made available until the
inner stream reaches an EOF.public ChunkInputStream(InputStream src, byte[] startMarker, byte[] stopMarker, boolean showMarkers, boolean automaticFetch)
startMarker!=null
the operating mode is set to
automaticFetch=true
src
- Source stream. Must not be null
.startMarker
- When this sequence of bytes is found in the src
stream the bytes of the inner stream are shown until an
endMarker
is found. If this parameter is set to
null
the stream is initially in an EOF state
until a fetchNextChunk()
is performed.showMarkers
- if set to true
start and end markers are shown
in the outer stream.automaticFetch
- enables automatic fetching of startMarker
s. If
false
startMarker
s must be fetched
manually invoking fetchNextChunk()
stopMarker
- when this sequence of bytes is found in the src
stream the bytes of the inner stream are hidden until a
startMarker
is found. If this parameter is set
to null
the stream is made available until the
inner stream reaches an EOF.fetchNextChunk()
public int available() throws IOException
available
in class InputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public boolean fetchNextChunk() throws IOException
automaticFetch=false
before
the stream can be used and each time an endMarker has been found to
proceed to next startMarker.true
if another chunk is available,
false
otherwise.IOException
- exception thrown if it is impossible to read from the inner
stream for some unknown reason.public void mark(int readlimit)
mark
in class InputStream
public boolean markSupported()
markSupported
in class InputStream
public int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b) throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public void reset() throws IOException
reset
in class InputStream
IOException
public long skip(long n) throws IOException
skip
in class InputStream
IOException
Copyright © 2008–2016. All rights reserved.