org.jscience.io
Class InterceptInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.jscience.io.InterceptInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
ReplaceInputStream

public class InterceptInputStream
extends java.io.InputStream

A specialized stream that allows to provide custom actions upon the occurence of a given byte pattern. The custom actions occur through registered Handler objects; bytes can be skipped from the stream (to not have the given pattern occur in the stream) and bytes can be inserted into the stream through given methods. The class may be subclassed to provide more sophisticated pattern matching, as this implementation only acts upon exact matches of a search pattern found.

See Also:
ReplaceInputStream

Nested Class Summary
static interface InterceptInputStream.Handler
          used to handle pattern occurences for an InterceptInputStream
 
Constructor Summary
InterceptInputStream(java.io.InputStream in, byte[] searchPattern)
          Creates a new InterceptInputStream object.
 
Method Summary
 void addHandler(InterceptInputStream.Handler handler)
          DOCUMENT ME!
protected  int checkForMatch(byte[] buffer1, byte[] buffer2, byte[] searchPattern, int cursor)
          checks whether the exact searchPattern can be found in buffer1 or in the intersection between buffer1 and buffer2.
 void close()
          DOCUMENT ME!
 void insertBytes(byte[] bytes)
          inserts the given bytes into the stream, so that these bytes are read next before all others that would normally follow at this point.
 boolean markSupported()
          returns false
 int read()
          DOCUMENT ME!
 long readSoFar()
          returns the total number of bytes read from the original stream so far (not considering inserted bytes but counting skipped bytes)
 void reset()
          throws IOException
 long skip(long numberOfBytes)
          skips bytes only from the source input stream - ignoring inserted bytes
 
Methods inherited from class java.io.InputStream
available, mark, read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InterceptInputStream

public InterceptInputStream(java.io.InputStream in,
                            byte[] searchPattern)
Creates a new InterceptInputStream object.

Parameters:
in - DOCUMENT ME!
searchPattern - DOCUMENT ME!
Method Detail

addHandler

public void addHandler(InterceptInputStream.Handler handler)
DOCUMENT ME!

Parameters:
handler - DOCUMENT ME!

close

public void close()
           throws java.io.IOException
DOCUMENT ME!

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - DOCUMENT ME!

markSupported

public boolean markSupported()
returns false

Overrides:
markSupported in class java.io.InputStream
Returns:
DOCUMENT ME!

reset

public void reset()
           throws java.io.IOException
throws IOException

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - DOCUMENT ME!

insertBytes

public void insertBytes(byte[] bytes)
inserts the given bytes into the stream, so that these bytes are read next before all others that would normally follow at this point. The inserted bytes are unchecked in regards to the search pattern.

Parameters:
bytes - DOCUMENT ME!

skip

public long skip(long numberOfBytes)
          throws java.io.IOException
skips bytes only from the source input stream - ignoring inserted bytes

Overrides:
skip in class java.io.InputStream
Parameters:
numberOfBytes - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

read

public int read()
         throws java.io.IOException
DOCUMENT ME!

Specified by:
read in class java.io.InputStream
Returns:
DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

readSoFar

public long readSoFar()
returns the total number of bytes read from the original stream so far (not considering inserted bytes but counting skipped bytes)

Returns:
DOCUMENT ME!

checkForMatch

protected int checkForMatch(byte[] buffer1,
                            byte[] buffer2,
                            byte[] searchPattern,
                            int cursor)
checks whether the exact searchPattern can be found in buffer1 or in the intersection between buffer1 and buffer2. May be overwritten to do some more sophisticated pattern matching instead of just testing for an exact match.

Parameters:
buffer1 - byte array containing the current stream buffer
buffer2 - byte array containing the following buffer (to enable pattern search in intersections of two buffers, so that all occurences within the stream can be found)
searchPattern - the pattern that is used for the pattern matching
cursor - the current position of the stream within buffer1
Returns:
the index relative to buffer1 where the first matching pattern begins between the cursor until the end of buffer1 (while considering buffer2 for overlaps) or -1 if no pattern match was found
See Also:
IOUtils.indexOf(byte[],byte[],int,int), (byte[], byte[], byte[])