Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.lutris.util
Class BMByteSearchStream  view BMByteSearchStream download BMByteSearchStream.java

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended bycom.lutris.util.BMByteSearchStream
All Implemented Interfaces:
java.io.Closeable

public class BMByteSearchStream
extends java.io.FilterInputStream

Implements the Boyer-Moore pattern matching algorithm for a given byte pattern. This object implements searches on byte-oriented input streams.

The algorithm was obtained from "Computer Algorithms - Introduction to Design and Analysis, Second Edition" by Sara Baase.


Field Summary
static int AT_PATTERN
          "At Pattern" value.
static int EOF
          EOF value.
private  int patternKeep
          Number of bytes to overlap buffer reads by to ensure that the pattern gets matched even if it crosses a read boundary.
private  int patternLength
          The length of the search pattern.
private  int patternPos
          The current position of the pattern within the current buffer contents.
private  byte[] readByte
          Buffer to hold a single character in order to implement the single character read() method.
private  byte[] scanBuf
          Scan buffer -- Should be at least ten times the length of the search pattern.
private  int scanBufCount
          Number of bytes currently in the scan buffer.
private  int scanBufPos
          Scan buffer position.
private  int scanBufSize
          Total size of the scan buffer.
private  BMByteSearch searcher
          Boyer-Moore byte string searcher for scanning the scan buffer.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
BMByteSearchStream(java.io.InputStream inputSource, java.lang.String pattern, int buflen)
          Creates a Boyer-Moore byte stream scanner for a given pattern.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this input stream without blocking.
private  int loadBuf()
          Reads more data from the underlying input source, keeping track of buffering and pattern matching.
private static int min(int i1, int i2)
          Compares two integers and returns the lesser value.
 java.lang.String peekAheadString(int length)
          Returns the next length bytes of the input buffer as a string.
 int read()
          Reads the next byte of data from this input stream.
 int read(byte[] buffer)
          Reads up to buffer.length bytes of data from this input stream into an array of bytes.
 int read(byte[] buffer, int offset, int length)
          Reads length bytes of data from this input stream into an array of bytes.
 int readTo(byte[] buffer, int offset, int length)
          Reads data into a buffer until the search pattern or the end of file is detected.
 void setPattern(BMByteSearch search)
          Set the search pattern.
 void setPattern(java.lang.String pattern)
          Set the search pattern.
 long skip(long n)
          Skips over and discards n bytes of data from the input stream.
 int skipPattern()
          Skips all bytes up to and including the search pattern or EOF.
 int skipTo()
          Skips all bytes up to but not including the search pattern or EOF.
 
Methods inherited from class java.io.FilterInputStream
close, mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final int EOF
EOF value. Traditionally -1.

See Also:
Constant Field Values

AT_PATTERN

public static final int AT_PATTERN
"At Pattern" value. Indicates that the stream position is currently at the beginning of a detected pattern occurence.

See Also:
Constant Field Values

searcher

private BMByteSearch searcher
Boyer-Moore byte string searcher for scanning the scan buffer.


scanBuf

private byte[] scanBuf
Scan buffer -- Should be at least ten times the length of the search pattern.


scanBufPos

private int scanBufPos
Scan buffer position.


scanBufCount

private int scanBufCount
Number of bytes currently in the scan buffer.


scanBufSize

private int scanBufSize
Total size of the scan buffer.


patternLength

private int patternLength
The length of the search pattern.


patternKeep

private int patternKeep
Number of bytes to overlap buffer reads by to ensure that the pattern gets matched even if it crosses a read boundary. This should normally be the length of the pattern.


patternPos

private int patternPos
The current position of the pattern within the current buffer contents. This may be less than the current buffer position if the pattern has already been skipped.


readByte

private byte[] readByte
Buffer to hold a single character in order to implement the single character read() method.

Constructor Detail

BMByteSearchStream

public BMByteSearchStream(java.io.InputStream inputSource,
                          java.lang.String pattern,
                          int buflen)
Creates a Boyer-Moore byte stream scanner for a given pattern. Creates a buffer of length buflen for the scanning buffer.

Method Detail

read

public int read()
         throws java.io.IOException
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown


read

public int read(byte[] buffer)
         throws java.io.IOException
Reads up to buffer.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available


read

public int read(byte[] buffer,
                int offset,
                int length)
         throws java.io.IOException
Reads length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.


skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.


available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read from this input stream without blocking. This consists of any bytes left in the buffer plus the result of the underlying stream's available method.


readTo

public int readTo(byte[] buffer,
                  int offset,
                  int length)
           throws java.io.IOException
Reads data into a buffer until the search pattern or the end of file is detected. Returns -1 if at the search pattern or end-of-file.


skipTo

public int skipTo()
           throws java.io.IOException
Skips all bytes up to but not including the search pattern or EOF. Returns the number of bytes skipped. Repeated calls to this method will leave the stream at the same postion until another call explicitly reads or skips the pattern data.


skipPattern

public int skipPattern()
                throws java.io.IOException
Skips all bytes up to and including the search pattern or EOF.


setPattern

public void setPattern(java.lang.String pattern)
Set the search pattern. After this call, all new scans will be for the new pattern. Characters outside the values 0-255 are truncated into signed byte values in the Latin-1 encoding.


setPattern

public void setPattern(BMByteSearch search)
Set the search pattern. After this call, all new scans will be for the new pattern. Characters outside the values 0-255 are truncated into signed byte values in the Latin-1 encoding.


peekAheadString

public java.lang.String peekAheadString(int length)
                                 throws java.io.IOException
Returns the next length bytes of the input buffer as a string. If fewer than length bytes remain on the input stream, then only the remaining bytes are returned. If the input stream is at EOF and there are no more bytes in the buffer then an empty string is returned.


loadBuf

private int loadBuf()
             throws java.io.IOException
Reads more data from the underlying input source, keeping track of buffering and pattern matching. Before reading new bytes, any remaining bytes in the buffer are copied to position 0. The scanBufPos, scanBufCount, and patternPos fields are reset to appropriate values.


min

private static final int min(int i1,
                             int i2)
Compares two integers and returns the lesser value.