Package com.suse.common.io
Class ByteSequenceFinder
- java.lang.Object
-
- com.suse.common.io.ByteSequenceFinder
-
public class ByteSequenceFinder extends java.lang.ObjectFinds a byte sequence in a stream of bytes. Uses the Knuth–Morris–Pratt search algorithm for better performance.The algorithm remembers the past matched characters in order to avoid restarting from the beginning of the pattern when a mismatch is found. This is obtained by building a lookup of a partial match table called failure function.
-
-
Constructor Summary
Constructors Constructor Description ByteSequenceFinder()Default constructorByteSequenceFinder(byte[] sequenceIn)Create a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]getSequence()Retrieves the current sequence used by a search method.intsearch(byte[] bytes)Finds the first occurrence of the pattern in the specified byte array.intsearch(java.io.InputStream stream)Finds the first occurrence of the pattern in the specified stream.voidsetSequence(byte[] sequenceIn)Sets the sequence to be used by the next invocation of a search method.private static int[]updateFailureFunction(byte[] sequence)Computes the failure function.
-
-
-
Method Detail
-
search
public int search(byte[] bytes) throws java.io.IOExceptionFinds the first occurrence of the pattern in the specified byte array.- Parameters:
bytes- the byte array where the sequence is searched- Returns:
- the index where the sequence starts within the array,
-1if the sequence was not found. - Throws:
java.io.IOException- when an error processing the stream of bytes occurs
-
search
public int search(java.io.InputStream stream) throws java.io.IOExceptionFinds the first occurrence of the pattern in the specified stream. The stream will be consumed up to end of the sequence matched. It will be fully consumed if no match is found.- Parameters:
stream- the input stream to search- Returns:
- the position where the sequence starts within the stream and the stream will be positioned AFTER the
end of the sequence. Otherwise,
-1if the sequence was not found. - Throws:
java.io.IOException- when an error processing the stream occurs
-
getSequence
public byte[] getSequence()
Retrieves the current sequence used by a search method.- Returns:
- the current sequence to be searched
-
setSequence
public void setSequence(byte[] sequenceIn)
Sets the sequence to be used by the next invocation of a search method.- Parameters:
sequenceIn- the new sequence to search
-
updateFailureFunction
private static int[] updateFailureFunction(byte[] sequence)
Computes the failure function. Evaluates the sequence and find repeating prefixes.
-
-