ByteDestination
, BytesDestination
, org.refcodes.mixin.Loggable
AbstractBytesReceiver
public abstract class AbstractBytesDestination extends Object implements BytesDestination, org.refcodes.mixin.Loggable
AbstractBytesDestination
is a base abstract implementation of the
BytesDestination
interface providing common functionality for
concrete real implementations. A blocking queue is used internally to which
received datagrams are put via pushDatagram(byte)
and which can be
retrieved via receiveByte()
. The pushDatagram(byte)
method
is your hook when extending this class. Make sure your code fetches the
datagrams quick enough to prevent filling up of the queue. In case the queue
is filled up, adding elements via pushDatagram(byte)
to the queue is
blocked until elements are taken from the queue via receiveByte()
.
So cautions are taken to prevent a memory leak.Modifier and Type | Field | Description |
---|---|---|
protected LinkedBlockingQueue<Byte> |
_datagramQueue |
|
static int |
DATAGRAM_QUEUE_SIZE |
Constructor | Description |
---|---|
AbstractBytesDestination() |
Constructs a
AbstractBytesDestination with a default sized
blocking queue enabling a maximum of DATAGRAM_QUEUE_SIZE
datagrams.
-------------------------------------------------------------------------
Make sure your code fetches the datagrams quick enough to prevent filling
up of the queue. |
AbstractBytesDestination(int aCapacity) |
Constructs a
AbstractBytesDestination with a custom sized
blocking queue enabling a maximum of datagrams as specified by the
capacity parameter.
-------------------------------------------------------------------------
Make sure your code fetches the datagrams quick enough to prevent filling
up of the queue. |
Modifier and Type | Method | Description |
---|---|---|
protected void |
pushDatagram(byte aDatagram) |
Pushes a datagram into the receiver and puts it into the blocking queue
containing the so far received datagrams.
|
protected void |
pushDatagrams(byte[] aDatagrams) |
Pushes datagrams into the receiver and puts them into the blocking queue
containing the so far received datagrams.
|
protected void |
pushDatagrams(byte[] aDatagrams,
int aOffset,
int aLength) |
Pushes datagrams into the receiver and puts them into the blocking queue
containing the so far received datagrams.
|
byte[] |
receiveAllBytes() |
Reads (receives) the all currently available data.
|
byte |
receiveByte() |
Reads (receives) the next byte, in case none byte is available, then this
method blocks until one is available.
|
byte[] |
receiveBytes(int aMaxLength) |
Similar to
BytesDestination.receiveAllBytes() though at maximum the amount of
data as of the provided length returned. |
receiveBytes
public static final int DATAGRAM_QUEUE_SIZE
protected LinkedBlockingQueue<Byte> _datagramQueue
public AbstractBytesDestination()
AbstractBytesDestination
with a default sized
blocking queue enabling a maximum of DATAGRAM_QUEUE_SIZE
datagrams.
-------------------------------------------------------------------------
Make sure your code fetches the datagrams quick enough to prevent filling
up of the queue. In case the queue is filled up, adding elements via
pushDatagram(byte)
to the queue is blocked until elements are
taken from the queue via receiveByte()
.public AbstractBytesDestination(int aCapacity)
AbstractBytesDestination
with a custom sized
blocking queue enabling a maximum of datagrams as specified by the
capacity parameter.
-------------------------------------------------------------------------
Make sure your code fetches the datagrams quick enough to prevent filling
up of the queue. In case the queue is filled up, adding elements via
pushDatagram(byte)
to the queue is blocked until elements are
taken from the queue via receiveByte()
.aCapacity
- The capacity of the queue holding the received
datagrams.public byte receiveByte() throws IOException
receiveByte
in interface ByteDestination
receiveByte
in interface BytesDestination
IOException
- Thrown in case opening or accessing an open line
(connection, junction, link) caused problems.public byte[] receiveAllBytes() throws IOException
receiveAllBytes
in interface BytesDestination
DatagramsTransmitter
or DatagramReceiver
counterpart.IOException
- Thrown in case opening or accessing an open line
(connection, junction, link) caused problems.public byte[] receiveBytes(int aMaxLength) throws IOException
BytesDestination.receiveAllBytes()
though at maximum the amount of
data as of the provided length returned.receiveBytes
in interface BytesDestination
aMaxLength
- The block-size which is not to exceeded by the returned
data. A value of -1 specifies to retrieve all available datagrams
(same behavior as method BytesDestination.receiveAllBytes()
.DatagramsTransmitter
or DatagramReceiver
counterpart.IOException
- Thrown in case opening or accessing an open line
(connection, junction, link) caused problems.protected void pushDatagram(byte aDatagram) throws IOException
receiveByte()
: Use AbstractBytesReceiver
extension's
AbstractBytesReceiver.available()
to test beforehand whether
there is a datagram available.aDatagram
- The datagram to be pushed at the end of the blocking
queue; to be retrieved with the receiveByte()
method.IOException
- the open exceptionprotected void pushDatagrams(byte[] aDatagrams) throws IOException
receiveByte()
: Use AbstractBytesReceiver
extension's
AbstractBytesReceiver.available()
to test beforehand whether
there is a datagram available.aDatagrams
- The datagrams to be pushed at the end of the blocking
queue; to be retrieved with the receiveByte()
method.IOException
- the open exceptionprotected void pushDatagrams(byte[] aDatagrams, int aOffset, int aLength) throws IOException
receiveByte()
: Use AbstractBytesReceiver
extension's
AbstractBytesReceiver.available()
to test beforehand whether
there is a datagram available.aDatagrams
- The datagrams to be pushed at the end of the blocking
queue; to be retrieved with the receiveByte()
method.aOffset
- The offset to start taking data from to be pushed.aLength
- The number of elements to be pushed from the offset
onwards.IOException
- the open exceptionCopyright © 2021. All rights reserved.