Package org.refcodes.io
Class AbstractByteProvider
- java.lang.Object
-
- org.refcodes.io.AbstractByteProvider
-
- All Implemented Interfaces:
ByteBlockProvider,ByteDatagramProvider,ByteProvider,org.refcodes.mixin.Loggable
- Direct Known Subclasses:
AbstractByteReceiver,ByteArrayProviderImpl
public abstract class AbstractByteProvider extends java.lang.Object implements ByteProvider, org.refcodes.mixin.Loggable
TheAbstractByteProvideris a base abstract implementation of theByteReceiverinterface providing common functionality for concrete real liveByteDatagramReceiverandByteBlockReceiver(=ByteReceiver) implementations. A blocking queue is used internally to which received datagrams are put viapushDatagram(byte)and which can be retrieved viareadDatagram(). ThepushDatagram(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 viapushDatagram(byte)to the queue is blocked until elements are taken from the queue viareadDatagram(). So cautions are taken to prevent a memory leak.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.LinkedBlockingQueue<java.lang.Byte>_datagramQueuestatic intDATAGRAM_QUEUE_SIZE
-
Constructor Summary
Constructors Constructor Description AbstractByteProvider()Constructs aAbstractByteProviderwith a default sized blocking queue enabling a maximum ofDATAGRAM_QUEUE_SIZEdatagrams. ------------------------------------------------------------------------- Make sure your code fetches the datagrams quick enough to prevent filling up of the queue.AbstractByteProvider(int aCapacity)Constructs aAbstractByteProviderwith 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidpushDatagram(byte aDatagram)Pushes a datagram into the receiver and puts it into the blocking queue containing the so far received datagrams.protected voidpushDatagrams(byte[] aDatagrams)Pushes datagrams into the receiver and puts them into the blocking queue containing the so far received datagrams.protected voidpushDatagrams(byte[] aDatagrams, int aOffset, int aLength)Pushes datagrams into the receiver and puts them into the blocking queue containing the so far received datagrams.bytereadDatagram()Reads (receives) the next byte passed from aDatagramSendercounterpart.byte[]readDatagrams()Caution: Reads till anOpenException(or anInterruptedExceptionin case of blocking till more data is available) occurs.byte[]readDatagrams(int aBlockSize)Similar toByteBlockProvider.readDatagrams()though at maximum the amount of data as provided by the block-size is returned.
-
-
-
Field Detail
-
DATAGRAM_QUEUE_SIZE
public static final int DATAGRAM_QUEUE_SIZE
- See Also:
- Constant Field Values
-
_datagramQueue
protected java.util.concurrent.LinkedBlockingQueue<java.lang.Byte> _datagramQueue
-
-
Constructor Detail
-
AbstractByteProvider
public AbstractByteProvider()
Constructs aAbstractByteProviderwith a default sized blocking queue enabling a maximum ofDATAGRAM_QUEUE_SIZEdatagrams. ------------------------------------------------------------------------- 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 viapushDatagram(byte)to the queue is blocked until elements are taken from the queue viareadDatagram().
-
AbstractByteProvider
public AbstractByteProvider(int aCapacity)
Constructs aAbstractByteProviderwith 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 viapushDatagram(byte)to the queue is blocked until elements are taken from the queue viareadDatagram().- Parameters:
aCapacity- The capacity of the queue holding the received datagrams.
-
-
Method Detail
-
readDatagram
public byte readDatagram() throws org.refcodes.component.OpenException, java.lang.InterruptedExceptionReads (receives) the next byte passed from aDatagramSendercounterpart. In case none byte is available, then this method blocks until one is available. To prevent blocking, use theByteDatagramReceiverextension'sReceivable.hasDatagram()method to test beforehand whether a byte is available (in a multi-threaded usage scenario,Receivable.hasDatagram()is not a reliable indicator whether this method will block or not). When aThreadis waiting for a byte to be read andThread.interrupt()is being called, then the operation is aborted and anInterruptedExceptionis thrown.- Specified by:
readDatagramin interfaceByteDatagramProvider- Returns:
- The next byte sent from the
ByteDatagramSendercounterpart. - Throws:
org.refcodes.component.OpenException- Thrown in case opening or accessing an open line (connection, junction, link) caused problems.java.lang.InterruptedException- Thrown when aThreadis waiting, sleeping, or otherwise occupied, and theThreadis interrupted, either before or during the activity.
-
readDatagrams
public byte[] readDatagrams() throws org.refcodes.component.OpenException, java.lang.InterruptedExceptionCaution: Reads till anOpenException(or anInterruptedExceptionin case of blocking till more data is available) occurs. Reads (receives) the next byte block passed from aBlockSenderorDatagramSendercounterpart. In case none byte block is available, then this method blocks until one is available. When aThreadis waiting for a byte to be read andThread.interrupt()is being called, then the operation is aborted and anInterruptedExceptionis thrown.- Specified by:
readDatagramsin interfaceByteBlockProvider- Specified by:
readDatagramsin interfaceByteProvider- Returns:
- The next byte block sent from the
BlockSenderorDatagramReceivercounterpart. - Throws:
org.refcodes.component.OpenException- Thrown in case opening or accessing an open line (connection, junction, link) caused problems.java.lang.InterruptedException- Thrown when aThreadis waiting, sleeping, or otherwise occupied, and theThreadis interrupted, either before or during the activity.
-
readDatagrams
public byte[] readDatagrams(int aBlockSize) throws org.refcodes.component.OpenException, java.lang.InterruptedExceptionSimilar toByteBlockProvider.readDatagrams()though at maximum the amount of data as provided by the block-size is returned.- Specified by:
readDatagramsin interfaceByteBlockProvider- Specified by:
readDatagramsin interfaceByteProvider- Parameters:
aBlockSize- 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 methodByteBlockProvider.readDatagrams().- Returns:
- The next short block sent from the
BlockSenderorDatagramReceivercounterpart. - Throws:
org.refcodes.component.OpenException- Thrown in case opening or accessing an open line (connection, junction, link) caused problems.java.lang.InterruptedException- Thrown when aThreadis waiting, sleeping, or otherwise occupied, and theThreadis interrupted, either before or during the activity.
-
pushDatagram
protected void pushDatagram(byte aDatagram) throws org.refcodes.component.OpenExceptionPushes a datagram into the receiver and puts it into the blocking queue containing the so far received datagrams. Them datagrams can be retrieved viareadDatagram(): UseAbstractByteReceiverextension'sAbstractByteReceiver.hasDatagram()to test beforehand whether there is a datagram available.- Parameters:
aDatagram- The datagram to be pushed at the end of the blocking queue; to be retrieved with thereadDatagram()method.- Throws:
org.refcodes.component.OpenException- the open exception
-
pushDatagrams
protected void pushDatagrams(byte[] aDatagrams) throws org.refcodes.component.OpenExceptionPushes datagrams into the receiver and puts them into the blocking queue containing the so far received datagrams. Them datagrams can be retrieved viareadDatagram(): UseAbstractByteReceiverextension'sAbstractByteReceiver.hasDatagram()to test beforehand whether there is a datagram available.- Parameters:
aDatagrams- The datagrams to be pushed at the end of the blocking queue; to be retrieved with thereadDatagram()method.- Throws:
org.refcodes.component.OpenException- the open exception
-
pushDatagrams
protected void pushDatagrams(byte[] aDatagrams, int aOffset, int aLength) throws org.refcodes.component.OpenExceptionPushes datagrams into the receiver and puts them into the blocking queue containing the so far received datagrams. Them datagrams can be retrieved viareadDatagram(): UseAbstractByteReceiverextension'sAbstractByteReceiver.hasDatagram()to test beforehand whether there is a datagram available.- Parameters:
aDatagrams- The datagrams to be pushed at the end of the blocking queue; to be retrieved with thereadDatagram()method.aOffset- The offset to start taking data from to be pushed.aLength- The number of elements to be pushed from the offset onwards.- Throws:
org.refcodes.component.OpenException- the open exception
-
-