|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.InputStream
com.gc.iotools.stream.is.ChunkInputStream
public final class ChunkInputStream
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
endMarkers
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 Summary | |
|---|---|
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. |
|
| Method Summary | |
|---|---|
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)
. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ChunkInputStream(InputStream src,
byte[] startMarker,
byte[] stopMarker)
ChunkInputStream.
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.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.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.showMarkers - if set to true start and end markers are shown in
the outer stream.automaticFetch - enables automatic fetching of startMarkers. If
false startMarkers must be fetched
manually invoking fetchNextChunk()fetchNextChunk()| Method Detail |
|---|
public int available()
throws IOException
available in class InputStreamIOException
public void close()
throws IOException
close in interface Closeableclose in class InputStreamIOException
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 InputStreampublic boolean markSupported()
markSupported in class InputStream
public int read()
throws IOException
read in class InputStreamIOException
public int read(byte[] b)
throws IOException
read in class InputStreamIOException
public int read(byte[] b,
int off,
int len)
throws IOException
read in class InputStreamIOException
public void reset()
throws IOException
reset in class InputStreamIOException
public long skip(long n)
throws IOException
skip in class InputStreamIOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||