com.gc.iotools.stream.is
Class ChunkInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.gc.iotools.stream.is.ChunkInputStream
All Implemented Interfaces:
Closeable

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.

The result is that only the bytes between 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.

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 "
 }
 

Since:
1.0.8
Author:
dvd.smnt

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

ChunkInputStream

public ChunkInputStream(InputStream src,
                        byte[] startMarker,
                        byte[] stopMarker)
Constructs a ChunkInputStream.

Parameters:
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.

ChunkInputStream

public ChunkInputStream(InputStream src,
                        byte[] startMarker,
                        byte[] stopMarker,
                        boolean showMarkers,
                        boolean automaticFetch)
Gets an instance of the ChunkInputStream. If startMarker!=null the operating mode is set to automaticFetch=true

Parameters:
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()
See Also:
fetchNextChunk()
Method Detail

available

public int available()
              throws IOException
.

Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException
.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

fetchNextChunk

public boolean fetchNextChunk()
                       throws IOException
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.

Returns:
true if another chunk is available, false otherwise.
Throws:
IOException - exception thrown if it is impossible to read from the inner stream for some unknown reason.

mark

public void mark(int readlimit)
.

Overrides:
mark in class InputStream

markSupported

public boolean markSupported()
.

Overrides:
markSupported in class InputStream

read

public int read()
         throws IOException
.

Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
.

Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
.

Overrides:
read in class InputStream
Throws:
IOException

reset

public void reset()
           throws IOException
.

Overrides:
reset in class InputStream
Throws:
IOException

skip

public long skip(long n)
          throws IOException
.

Overrides:
skip in class InputStream
Throws:
IOException


Copyright © 2008-2009. All Rights Reserved.