EAF 7.4 Implementation

com.lutris.util
Class BMByteSearchStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by com.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.
 
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.
 int availableTo()
          Returns the number of bytes that can be read from this input stream without blocking or encountering the search pattern.
 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
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.

Parameters:
inputSource - The input source stream to scan.
pattern - The pattern to scan for. Characters outside the Latin-1 encoding are truncated to signed bytes.
buflen - The length to use 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

Overrides:
read in class java.io.FilterInputStream
Returns:
The next byte of data, or -1 if the end of stream is reached.
Throws:
java.io.IOException - If an I/O error occurs.

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

Overrides:
read in class java.io.FilterInputStream
Parameters:
buffer - The buffer into which data are read.
Returns:
The number of bytes actually read, or -1 if there are no more bytes because the end of stream has been reached.
Throws:
java.io.IOException - If an I/O error occurs.

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.

Overrides:
read in class java.io.FilterInputStream
Parameters:
buffer - The buffer into which data are read.
offset - The start offset of the data.
length - The maximum number of bytes read.
Returns:
The total number of bytes read into the buffer, or -1 if there are no more bytes because the end of stream has been reached.
Throws:
java.io.IOException - If an I/O error occurs.

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.

Overrides:
skip in class java.io.FilterInputStream
Parameters:
n - The number of bytes to be skipped.
Returns:
The actual number of bytes skipped.
Throws:
java.io.IOException - If an I/O error occurs.

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.

Overrides:
available in class java.io.FilterInputStream
Returns:
The number of bytes that can be read without blocking.
Throws:
java.io.IOException - If an I/O error occurs.

availableTo

public int availableTo()
                throws java.io.IOException
Returns the number of bytes that can be read from this input stream without blocking or encountering the search pattern. If the search pattern has been found in the buffer, this is the number of bytes in the search buffer prior to the search pattern; otherwise, while there may be additional data in the input stream buffer that can be brought into the search buffer without blocking, this data may or may not contain the search pattern (or complete a partial search pattern located at the end of the buffer), so this is calculated as the number of bytes left in the search buffer less the length of the search pattern.

Returns:
The number of bytes that can be read for certain without blocking or encountering the search pattern.
Throws:
java.io.IOException - If an I/O error occurs.

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.

Parameters:
buffer - Buffer to read into.
offset - Offset in buffer to read into.
length - Number of bytes to try to read.
Returns:
The number of bytes actually read, or -1 if at the pattern or eof.
Throws:
java.io.IOException - Thrown if an I/O exception occurs.

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.

Returns:
The number of bytes skipped.
Throws:
java.io.IOException - Thrown if an I/O exception occurs.

skipPattern

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

Returns:
The number of bytes skipped.
Throws:
java.io.IOException - Thrown if an I/O exception occurs.

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.

Parameters:
pattern - The new pattern to search for.

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.

Parameters:
search - The precomputed Boyer-Moore searcher.

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.

Parameters:
length - The number of bytes to look ahead.
Returns:
A string containing the lookahead bytes as 8 bit characters.
Throws:
java.io.IOException - Thrown if an I/O exception occurs.

EAF 7.4 Implementation