Package org.apache.nifi.util
Class NaiveSearchRingBuffer
java.lang.Object
org.apache.nifi.util.NaiveSearchRingBuffer
A RingBuffer that can be used to scan byte sequences for subsequences.
This class implements an efficient naive search algorithm, which allows the user of the library to identify byte sequences in a stream on-the-fly so that the stream can be segmented without having to buffer the data.
The intended usage paradigm is:
final byte[] searchSequence = ...;
final CircularBuffer buffer = new CircularBuffer(searchSequence);
while ((int nextByte = in.read()) > 0) {
if ( buffer.addAndCompare(nextByte) ) {
// This byte is the last byte in the given sequence
} else {
// This byte does not complete the given sequence
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int[]
private long
private int
private final byte[]
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
addAndCompare
(byte data) Add the given byte to the buffer and notify whether or not the byte completes the desired byte sequence.void
clear()
Clears the internal buffer so that a new search may beginbyte[]
int
boolean
isFilled()
-
Field Details
-
lookingFor
private final byte[] lookingFor -
buffer
private final int[] buffer -
insertionPointer
private int insertionPointer -
bufferSize
private long bufferSize
-
-
Constructor Details
-
NaiveSearchRingBuffer
public NaiveSearchRingBuffer(byte[] lookingFor)
-
-
Method Details
-
getBufferContents
public byte[] getBufferContents()- Returns:
- the contents of the internal buffer, which represents the last X bytes added to the buffer, where X is the minimum of the number of bytes added to the buffer or the length of the byte sequence for which we are looking
-
getOldestByte
public int getOldestByte()- Returns:
- the oldest byte in the buffer
-
isFilled
public boolean isFilled()- Returns:
true
if the number of bytes that have been added to the buffer is at least equal to the length of the byte sequence for which we are searching
-
clear
public void clear()Clears the internal buffer so that a new search may begin -
addAndCompare
public boolean addAndCompare(byte data) Add the given byte to the buffer and notify whether or not the byte completes the desired byte sequence.- Parameters:
data
- the data to add to the buffer- Returns:
true
if this byte completes the byte sequence,false
otherwise.
-