java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.io.PushbackInputStream
i2p.susi.util.EOFOnMatchInputStream
- All Implemented Interfaces:
ReadCounter, Closeable, AutoCloseable
InputStream that returns EOF when input matches specified bytes. The reader never sees bytes from a full match. Extends PushbackInputStream for convenience but uses its buffer as a FIFO, not a stack. Do not call unread() methods externally.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ReadCounterStream the bytes are read from.private final byte[]Bytes that end the stream when seen.private final intBytes kept while looking for the match.Fields inherited from class PushbackInputStream
buf, posFields inherited from class FilterInputStream
inConstructor Summary
ConstructorsConstructorDescriptionEOFOnMatchInputStream(InputStream in, byte[] match) Non-counter mode.EOFOnMatchInputStream(InputStream in, ReadCounter ctr, byte[] match) Counter mode.Method Summary
Modifier and TypeMethodDescriptionlonggetRead()If constructed with a counter, returns the count (not necessarily starting at 0) minus the buffered/matched count.private intpop()FIFO output.intread()intread(byte[] buf, int off, int len) Read bytes into a buffer.private static byte[]reverse(byte[] m) Reverses bytes, for the Boyer-Moore skip table.longskip(long n) Skip bytes in the stream.booleanwasFound()Whether the stream stopped because the search pattern matched.Methods inherited from class PushbackInputStream
available, close, mark, markSupported, reset, transferTo, unread, unread, unreadMethods inherited from class FilterInputStream
readMethods inherited from class InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes
Field Details
match
private final byte[] matchBytes that end the stream when seen.size
private final int sizeBytes kept while looking for the match.cis
Stream the bytes are read from.
Constructor Details
EOFOnMatchInputStream
Non-counter mode.getRead()will return 0.- Parameters:
in- the underlying stream to read until the match is seenmatch- will be copied
EOFOnMatchInputStream
Counter mode. getRead() will the ReadCounter's value, not including the match bytes.- Parameters:
in- the underlying stream to read until the match is seenctr- counts the bytes read from the underlying stream, or null for non-counter modematch- will be copied
Method Details
reverse
private static byte[] reverse(byte[] m) Reverses bytes, for the Boyer-Moore skip table.- Parameters:
m- the bytes to reverse- Returns:
- them in the other order
getRead
public long getRead()If constructed with a counter, returns the count (not necessarily starting at 0) minus the buffered/matched count. Otherwise returns 0.- Specified by:
getReadin interfaceReadCounter- Returns:
- the number of bytes read from the underlying stream that were not buffered here or matched against the search pattern, or 0 if this stream was not wrapped around a counting stream
wasFound
public boolean wasFound()Whether the stream stopped because the search pattern matched.- Returns:
- true if we returned EOF because we hit the match
read
- Overrides:
readin classPushbackInputStream- Throws:
IOException
pop
private int pop()FIFO output. Pop the oldest (not the newest). Only call if pos < size. We never call super.read(), it returns the newest.- Returns:
- the oldest byte in the buffer, unsigned 0-255
read
Read bytes into a buffer.- Overrides:
readin classPushbackInputStream- Parameters:
buf- the buffer to read intooff- the start offset in the bufferlen- the maximum number of bytes to read- Returns:
- the number of bytes read, or -1 on end of stream
- Throws:
IOException- on I/O error
skip
Skip bytes in the stream.- Overrides:
skipin classPushbackInputStream- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
IOException- on I/O error