public class NaiveSearchRingBuffer extends Object
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
}
}
Modifier and Type | Field and Description |
---|---|
private int[] |
buffer |
private int |
bufferSize |
private int |
insertionPointer |
private byte[] |
lookingFor |
Constructor and Description |
---|
NaiveSearchRingBuffer(byte[] lookingFor) |
Modifier and Type | Method and Description |
---|---|
boolean |
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 begin
|
byte[] |
getBufferContents() |
int |
getOldestByte() |
boolean |
isFilled() |
private final byte[] lookingFor
private final int[] buffer
private int insertionPointer
private int bufferSize
public byte[] getBufferContents()
public int getOldestByte()
public boolean isFilled()
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 searchingpublic void clear()
public boolean addAndCompare(byte data)
data
- the data to add to the buffertrue
if this byte completes the byte sequence, false
otherwise.Copyright © 2016 Apache NiFi Project. All rights reserved.