Holger's
Java API

com.antelmann.io
Class InterceptInputStream

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

public class InterceptInputStream
extends InputStream

A specialized stream that allows to provide custom actions upon the occurrence 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.

Author:
Holger Antelmann
See Also:
ReplaceInputStream

Nested Class Summary
static interface InterceptInputStream.Handler
          used to handle pattern occurrences for an InterceptInputStream
 
Constructor Summary
InterceptInputStream(InputStream in, byte[] searchPattern)
           
 
Method Summary
 void addHandler(InterceptInputStream.Handler handler)
           
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()
           
 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()
           
 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(InputStream in,
                            byte[] searchPattern)
Method Detail

addHandler

public void addHandler(InterceptInputStream.Handler handler)

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

markSupported

public boolean markSupported()
returns false

Overrides:
markSupported in class InputStream

reset

public void reset()
           throws IOException
throws IOException

Overrides:
reset in class InputStream
Throws:
IOException

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.


skip

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

Overrides:
skip in class InputStream
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

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)


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:
Data.indexOf(byte[], byte[], int, int), (byte[], byte[], byte[])


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads