Top Description Fields Constructors Methods
org.eclipse.jetty.util

public Class SearchPattern

extends Object
Class Inheritance
Imports
java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.util.Arrays

Fast search for patterns within strings, arrays of bytes and ByteBuffers.

Uses an implementation of the Boyer–Moore–Horspool algorithm with a 256 character alphabet.

The algorithm has an average-case complexity of O(n) on random text and O(nm) in the worst case, where m = pattern length and n = length of data to search.

Field Summary

Modifier and TypeField and Description
private static final int
private final byte[]
private final int[]

Constructor Summary

AccessConstructor and Description
private
SearchPattern(byte[]
byte array containing the pattern used for matching
pattern
)

Method Summary

Modifier and TypeMethod and Description
public static SearchPattern

Returns:

a new SearchPattern instance using the given pattern
compile
(byte[]
byte array containing the pattern to search
pattern
)

Creates a SearchPattern instance which can be used to find matches of the pattern in data.

public static SearchPattern

Returns:

a new SearchPattern instance using the given pattern
compile
(String
string containing the pattern to search
pattern
)

Creates a SearchPattern instance which can be used to find matches of the pattern in data.

public int

Returns:

the length of the partial pattern matched and 0 for no match.
endsWith
(byte[]
The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.
data
,
int
The offset within the data to start the search
offset
,
int
The length of the data to search
length
)

Search for a partial match of the pattern at the end of the data.

public int

Returns:

how many bytes of the pattern were matched at the end of the ByteBuffer, or 0 for no match
endsWith
(ByteBuffer
the ByteBuffer to search into
buffer
)

Searches for a partial match of the pattern at the end of the ByteBuffer.

public int
getLength()

Returns: The length of the pattern in bytes.

public byte[]
getPattern()

Returns: the pattern to search.

public int

Returns:

The index within the data array at which the first instance of the pattern or -1 if not found
match
(byte[]
The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.
data
,
int
The offset within the data to start the search
offset
,
int
The length of the data to search
length
)

Search for a complete match of the pattern within the data

public int

Returns:

the number of bytes after the buffer's position at which the full pattern was found, or -1 if the full pattern was not found
match
(ByteBuffer
the ByteBuffer to search into
buffer
)

Searches for a full match of the pattern in the ByteBuffer.

public int

Returns:

the length of the partial pattern matched and 0 for no match.
startsWith
(byte[]
The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.
data
,
int
The offset within the data to start the search
offset
,
int
The length of the data to search
length
,
int
The length of the partial pattern already matched
matched
)

Search for a possibly partial match of the pattern at the start of the data.

public int

Returns:

how many bytes of the pattern were matched (including those already matched) at the beginning of the ByteBuffer, or 0 for no match
startsWith
(ByteBuffer
the ByteBuffer to search into
buffer
,
int
how many bytes of the pattern were already matched
matched
)

Searches for a partial match of the pattern at the beginning of the ByteBuffer.

private void
validateArgs(byte[]
The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.
data
,
int
The offset within the data to start the search
offset
,
int
The length of the data to search
length
)

Performs legality checks for standard arguments input into SearchPattern methods.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALPHABET_SIZEback to summary
private static final int ALPHABET_SIZE
patternback to summary
private final byte[] pattern
tableback to summary
private final int[] table

Constructor Detail

SearchPatternback to summary
private SearchPattern(byte[] pattern)
Parameters
pattern:byte[]

byte array containing the pattern used for matching

Method Detail

compileback to summary
public static SearchPattern compile(byte[] pattern)

Creates a SearchPattern instance which can be used to find matches of the pattern in data.

Parameters
pattern:byte[]

byte array containing the pattern to search

Returns:SearchPattern

a new SearchPattern instance using the given pattern

compileback to summary
public static SearchPattern compile(String pattern)

Creates a SearchPattern instance which can be used to find matches of the pattern in data.

The pattern string must only contain ASCII characters.

Parameters
pattern:String

string containing the pattern to search

Returns:SearchPattern

a new SearchPattern instance using the given pattern

endsWithback to summary
public int endsWith(byte[] data, int offset, int length)

Search for a partial match of the pattern at the end of the data.

Parameters
data:byte[]

The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

offset:int

The offset within the data to start the search

length:int

The length of the data to search

Returns:int

the length of the partial pattern matched and 0 for no match.

endsWithback to summary
public int endsWith(ByteBuffer buffer)

Searches for a partial match of the pattern at the end of the ByteBuffer.

The ByteBuffer may contain arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

The position and limit of the ByteBuffer are not changed.

Parameters
buffer:ByteBuffer

the ByteBuffer to search into

Returns:int

how many bytes of the pattern were matched at the end of the ByteBuffer, or 0 for no match

getLengthback to summary
public int getLength()
Returns:int

The length of the pattern in bytes.

getPatternback to summary
public byte[] getPattern()
Returns:byte[]

the pattern to search.

matchback to summary
public int match(byte[] data, int offset, int length)

Search for a complete match of the pattern within the data

Parameters
data:byte[]

The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

offset:int

The offset within the data to start the search

length:int

The length of the data to search

Returns:int

The index within the data array at which the first instance of the pattern or -1 if not found

matchback to summary
public int match(ByteBuffer buffer)

Searches for a full match of the pattern in the ByteBuffer.

The ByteBuffer may contain arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

The position and limit of the ByteBuffer are not changed.

Parameters
buffer:ByteBuffer

the ByteBuffer to search into

Returns:int

the number of bytes after the buffer's position at which the full pattern was found, or -1 if the full pattern was not found

startsWithback to summary
public int startsWith(byte[] data, int offset, int length, int matched)

Search for a possibly partial match of the pattern at the start of the data.

Parameters
data:byte[]

The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

offset:int

The offset within the data to start the search

length:int

The length of the data to search

matched:int

The length of the partial pattern already matched

Returns:int

the length of the partial pattern matched and 0 for no match.

startsWithback to summary
public int startsWith(ByteBuffer buffer, int matched)

Searches for a partial match of the pattern at the beginning of the ByteBuffer.

The ByteBuffer may contain arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

The position and limit of the ByteBuffer are not changed.

Parameters
buffer:ByteBuffer

the ByteBuffer to search into

matched:int

how many bytes of the pattern were already matched

Returns:int

how many bytes of the pattern were matched (including those already matched) at the beginning of the ByteBuffer, or 0 for no match

validateArgsback to summary
private void validateArgs(byte[] data, int offset, int length)

Performs legality checks for standard arguments input into SearchPattern methods.

Parameters
data:byte[]

The data in which to search for. The data may be arbitrary binary data, but the pattern will always be StandardCharsets#US_ASCII encoded.

offset:int

The offset within the data to start the search

length:int

The length of the data to search