public class DetectorStreamBufferImpl extends Object implements DetectorStreamBuffer
DetectorStreamBuffer
interface. DetectorStreamProcessor
in the chain has its own
DetectorStreamBuffer
instance. Therefore it holds the according DetectorStreamProcessor
building a
pair of buffer+processor. Further it holds an instance of the predecessor and thereby represents the chain itself.Modifier and Type | Class and Description |
---|---|
protected class |
DetectorStreamBufferImpl.CurrentByteArray
This inner class is a view on the current
ByteArray . |
protected static class |
DetectorStreamBufferImpl.SeekMode
Enum with available modes for a
seek . |
Modifier and Type | Field and Description |
---|---|
private LinkedList<ByteArray> |
arrayQueue
|
private ByteArrayPool |
byteArrayPool
The
ByteArrayPool . |
private DetectorStreamBufferImpl |
chainSuccessor
The successor in the chain or
null if this is the last. |
private byte[] |
currentArray
The current
ByteArray to work on. |
private int |
currentArrayIndex
The
index in currentArray . |
private int |
currentArrayMax
The
maximum index in currentArray . |
private int |
currentArrayMin
The start-index in
currentArray . |
private ByteArray |
currentArrayView |
private ByteArray |
currentByteArray |
private DetectorStreamProcessor |
processor
The actual processor served by this buffer.
|
private long |
seekCount |
private DetectorStreamBufferImpl.SeekMode |
seekMode |
private long |
streamPosition |
Constructor and Description |
---|
DetectorStreamBufferImpl(DetectorStreamProcessor processor,
DetectorStreamBufferImpl successor,
ByteArrayPool byteArrayPool)
The constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
append(ByteArray nextArray)
This method queues the given
ByteArray at the end of this buffer. |
int |
fill(byte[] buffer,
int offset,
int length)
This method fills the given
buffer starting at offset with the next bytes from this
ComposedByteBuffer . |
ByteArray |
getByteArray(int index)
This method gets the
ByteArray at the given index . |
int |
getByteArrayCount()
This method gets the number of
ByteArray s currently available via ComposedByteBuffer.getByteArray(int) . |
int |
getBytesAvailable()
This method gets the number of bytes available.
|
long |
getStreamPosition()
This method gets the absolute position of the current pointer in the logical stream (the data provided by this
interface that origins from the previous member of the chain).
|
boolean |
hasNext()
This method determines if there is a
next byte available. |
void |
insert(byte... data)
This method inserts the given bytes at the current position into the stream.
|
void |
insert(ByteArray data)
This method inserts the given
data at the current position into the stream. |
byte |
next()
This method gets the current byte in the iteration.
|
private boolean |
nextArray()
This method switches over to the next internal
byte-array . |
byte |
peek()
This method gets the current byte in the iteration.
|
void |
process(Map<String,Object> metadata,
boolean eos) |
protected void |
release(ByteArray byteArray)
This method is called when a
ByteArray is wiped out of the chain. |
void |
remove(long byteCount)
This method removes the number of bytes given by
byteCount from the stream starting at the current
position. |
protected void |
seek(long byteCount,
DetectorStreamBufferImpl.SeekMode mode)
|
long |
skip()
This method skips all bytes
available in this buffer. |
long |
skip(long byteCount)
This method skips the number of bytes given by
byteCount in the stream starting at the current position. |
private DetectorStreamProcessor processor
private DetectorStreamBufferImpl chainSuccessor
null
if this is the last.private long streamPosition
private long seekCount
seek(long, SeekMode)
private DetectorStreamBufferImpl.SeekMode seekMode
seek(long, SeekMode)
private ByteArray currentByteArray
private byte[] currentArray
ByteArray
to work on.private int currentArrayMin
currentArray
.private int currentArrayIndex
index
in currentArray
.private int currentArrayMax
maximum index
in currentArray
.private final LinkedList<ByteArray> arrayQueue
private final ByteArray currentArrayView
private ByteArrayPool byteArrayPool
ByteArrayPool
.public DetectorStreamBufferImpl(DetectorStreamProcessor processor, DetectorStreamBufferImpl successor, ByteArrayPool byteArrayPool)
processor
- is the DetectorStreamProcessor
served by this buffer.successor
- is the successor in the chain or null
if this is the last buffer/processor pair in the
chain.byteArrayPool
- is the ByteArrayPool
to use.public long skip(long byteCount)
DetectorStreamBuffer
byteCount
in the stream starting at the current position.
The given number of bytes will be untouched in stream (queued for the next processor
in the chain).skip
in interface ByteIterator
skip
in interface DetectorStreamBuffer
byteCount
- is the number of bytes to ignore. This value can be greater than the currently
available bytes
. You may supply Long.MAX_VALUE
to ignore to the end of
the stream.byteCount
.InputStream.skip(long)
public long skip()
DetectorStreamBuffer
available
in this buffer. This is logically the same as
skip
(ByteProvider.getBytesAvailable()
)
but more efficient.skip
in interface DetectorStreamBuffer
public ByteArray getByteArray(int index)
ComposedByteBuffer
ByteArray
at the given index
. The
DetectorStreamBuffer
is composed out of ByteArray
s. This method allows
efficient processing of bytes from byte[]
rather then calling ByteIterator.hasNext()
and ByteIterator.next()
and
repetitive. Additionally this way allows full lookahead up to the end of the buffer without consuming the data.
ByteIterator.next()
or ByteIterator.skip(long)
will invalidate the returned ByteArray
.
Please do NOT call these methods while working with ByteArray
s.getByteArray
in interface ComposedByteBuffer
index
- is the index of the requested ByteArray
. It has to be in the range from 0
to
ComposedByteBuffer.getByteArrayCount()
- 1
. A value of 0
indicates the current ByteArray
this
buffer is currently pointing to. For that current buffer ByteArray.getBytes()
[
ByteArray.getCurrentIndex()
]
will have the same result as ByteIterator.peek()
.ByteArray
.ComposedByteBuffer.getByteArrayCount()
public int getByteArrayCount()
ComposedByteBuffer
ByteArray
s currently available via ComposedByteBuffer.getByteArray(int)
. ByteIterator.next()
or ByteIterator.skip(long)
will invalidate the returned ByteArray
.
Please do NOT call these methods while working with ByteArray
s.getByteArrayCount
in interface ComposedByteBuffer
ByteArray
s.public int getBytesAvailable()
ByteProvider
getBytesAvailable
in interface ByteProvider
public boolean hasNext()
ByteIterator
next byte
available.hasNext
in interface ByteIterator
true
if there is a next byte
available, false
otherwise (if the end of this
buffer has been reached).protected void release(ByteArray byteArray)
ByteArray
is wiped out of the chain.byteArray
- is the array to release.private boolean nextArray()
byte-array
.true
if a new buffer is available, false
if the buffer queue is empty.public byte next() throws NoSuchElementException
ByteIterator
ByteIterator
points
to the next byte in the iteration or to the end if there is no such byte available
. ByteIterator.hasNext()
returns true
.next
in interface ByteIterator
NoSuchElementException
- if there is no such byte available
.Iterator.next()
public byte peek() throws NoSuchElementException
ByteIterator
ByteIterator.next()
this method does NOT modify the state of
this ByteIterator
. Therefore the peeked byte does NOT get consumed and repetitive calls will return the
same value. ByteIterator.hasNext()
returns true
.peek
in interface ByteIterator
NoSuchElementException
- if there is no such byte available
.ByteIterator.next()
public void insert(byte... data)
DetectorStreamBuffer
DetectorStreamBuffer.insert(ByteArray)
.insert
in interface DetectorStreamBuffer
data
- is a ByteArray
with the data to insert.public void insert(ByteArray data)
DetectorStreamBuffer
data
at the current position into the stream.insert
in interface DetectorStreamBuffer
data
- is a ByteArray
with the data to insert.protected void seek(long byteCount, DetectorStreamBufferImpl.SeekMode mode)
byteCount
- is the number of bytes to seek.mode
- is the DetectorStreamBufferImpl.SeekMode
.public void remove(long byteCount)
DetectorStreamBuffer
byteCount
from the stream starting at the current
position.remove
in interface DetectorStreamBuffer
byteCount
- is the number of bytes to remove. This value can be greater than the currently
available bytes
. You may supply Long.MAX_VALUE
to remove the rest of
the stream.DetectorStreamBuffer.skip(long)
public long getStreamPosition()
DetectorStreamBuffer
inserted
.getStreamPosition
in interface DetectorStreamBuffer
protected void append(ByteArray nextArray)
ByteArray
at the end of this buffer.nextArray
- is the ByteArray
to append.public void process(Map<String,Object> metadata, boolean eos) throws IOException
metadata
- is the Map
with the meta-data.eos
- - true
if the end of the stream has been reached and the given buffer
has to beIOException
- in case of an Input/Output error. Should only be used internally.DetectorStreamProcessor.process(DetectorStreamBuffer, Map, boolean)
public int fill(byte[] buffer, int offset, int length)
ComposedByteBuffer
buffer
starting at offset
with the next
bytes from this
ComposedByteBuffer
. The bytes that are filled into the given buffer
will therefore be consumed.
This method can be used to write data from this buffer to an OutputStream
or supply it to the
consumer of an InputStream
.fill
in interface ComposedByteBuffer
buffer
- is the buffer to fill.offset
- is the index in the given buffer
where to fill in the first byte. See
ByteArray.getCurrentIndex()
.length
- is the expected number of bytes to fill into buffer
. Has to be positive and less or equal to
buffer.length - offset
. However at most the number of available bytes
can be filled even if length
is greater.buffer
. Will be positive and less or
equal to the given length
. Should be only less than maxLength
if NOT enough
bytes are available
.Copyright © 2001–2016 mmm-Team. All rights reserved.