java.lang.Object
java.io.InputStream
org.refcodes.serial.PacketInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.refcodes.mixin.BlockSizeAccessor
,org.refcodes.mixin.PacketSizeAccessor
,org.refcodes.numerical.EndianessAccessor
,PacketLengthWidthAccessor
,PacketMagicBytesAccessor
,PacketSegmentPackagerAccessor
,SequenceNumberAccessor
,SequenceNumberConcatenateModeAccessor
,SequenceNumberInitValueAccessor
,SequenceNumberWidthAccessor
- Direct Known Subclasses:
StopAndWaitPacketInputStream
public class PacketInputStream
extends InputStream
implements PacketLengthWidthAccessor, org.refcodes.numerical.EndianessAccessor, org.refcodes.mixin.BlockSizeAccessor, SequenceNumberAccessor, SequenceNumberWidthAccessor, SequenceNumberInitValueAccessor, SequenceNumberConcatenateModeAccessor, org.refcodes.mixin.PacketSizeAccessor, PacketSegmentPackagerAccessor, PacketMagicBytesAccessor
The
PacketInputStream
wraps an InputStream
and chunks any
data to be written into packets with a sequence number and a block of data.
An according PacketInputStream
then reverts the packetised data
stream while performing sequence number validation. A SegmentPackager
can be used to add functionality such as CRC checksum support.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.refcodes.mixin.BlockSizeAccessor
org.refcodes.mixin.BlockSizeAccessor.BlockSizeBuilder<B extends org.refcodes.mixin.BlockSizeAccessor.BlockSizeBuilder<B>>, org.refcodes.mixin.BlockSizeAccessor.BlockSizeMutator, org.refcodes.mixin.BlockSizeAccessor.BlockSizeProperty
Nested classes/interfaces inherited from interface org.refcodes.numerical.EndianessAccessor
org.refcodes.numerical.EndianessAccessor.EndianessBuilder<B extends org.refcodes.numerical.EndianessAccessor.EndianessBuilder<B>>, org.refcodes.numerical.EndianessAccessor.EndianessMutator, org.refcodes.numerical.EndianessAccessor.EndianessProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.PacketLengthWidthAccessor
PacketLengthWidthAccessor.PacketLengthWidthBuilder<B extends PacketLengthWidthAccessor.PacketLengthWidthBuilder<B>>, PacketLengthWidthAccessor.PacketLengthWidthMutator, PacketLengthWidthAccessor.PacketLengthWidthProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.PacketMagicBytesAccessor
PacketMagicBytesAccessor.PacketMagicBytesBuilder<B extends PacketMagicBytesAccessor.PacketMagicBytesBuilder<B>>, PacketMagicBytesAccessor.PacketMagicBytesMutator, PacketMagicBytesAccessor.PacketMagicBytesProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.PacketSegmentPackagerAccessor
PacketSegmentPackagerAccessor.PacketSegmentPackagerBuilder<B extends PacketSegmentPackagerAccessor.PacketSegmentPackagerBuilder<B>>, PacketSegmentPackagerAccessor.PacketSegmentPackagerMutator, PacketSegmentPackagerAccessor.PacketSegmentPackagerProperty
Nested classes/interfaces inherited from interface org.refcodes.mixin.PacketSizeAccessor
org.refcodes.mixin.PacketSizeAccessor.PacketSizeBuilder<B extends org.refcodes.mixin.PacketSizeAccessor.PacketSizeBuilder<B>>, org.refcodes.mixin.PacketSizeAccessor.PacketSizeMutator, org.refcodes.mixin.PacketSizeAccessor.PacketSizeProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.SequenceNumberAccessor
SequenceNumberAccessor.SequenceNumberBuilder<B extends SequenceNumberAccessor.SequenceNumberBuilder<B>>, SequenceNumberAccessor.SequenceNumberMutator, SequenceNumberAccessor.SequenceNumberProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.SequenceNumberConcatenateModeAccessor
SequenceNumberConcatenateModeAccessor.SequenceNumberConcatenateModeBuilder<B extends SequenceNumberConcatenateModeAccessor.SequenceNumberConcatenateModeBuilder<B>>, SequenceNumberConcatenateModeAccessor.SequenceNumberConcatenateModeMutator, SequenceNumberConcatenateModeAccessor.SequenceNumberConcatenateModeProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.SequenceNumberInitValueAccessor
SequenceNumberInitValueAccessor.SequenceNumberInitValueBuilder<B extends SequenceNumberInitValueAccessor.SequenceNumberInitValueBuilder<B>>, SequenceNumberInitValueAccessor.SequenceNumberInitValueMutator, SequenceNumberInitValueAccessor.SequenceNumberInitValueProperty
Nested classes/interfaces inherited from interface org.refcodes.serial.SequenceNumberWidthAccessor
SequenceNumberWidthAccessor.SequenceNumberWidthBuilder<B extends SequenceNumberWidthAccessor.SequenceNumberWidthBuilder<B>>, SequenceNumberWidthAccessor.SequenceNumberWidthMutator, SequenceNumberWidthAccessor.SequenceNumberWidthProperty
-
Field Summary
Modifier and TypeFieldDescriptionprotected AllocSectionDecoratorSegment<SequenceSection>
protected int
protected BoundedSequenceDecorator
protected InputStream
protected Segment
protected int
protected int
protected NumberSegment
-
Constructor Summary
ConstructorDescriptionPacketInputStream
(InputStream aInputStream, int aBlockSize, int aPacketLengthWidth, byte[] aPacketMagicBytes, int aSequenceNumberInitValue, int aSequenceNumberWidth, org.refcodes.mixin.ConcatenateMode aSequenceNumberConcatenateMode, SegmentPackager aPacketSegmentPackager, org.refcodes.numerical.Endianess aEndianess) Constructs an accordingPacketInputStream
instance wrapping the givenInputStream
.PacketInputStream
(InputStream aInputStream, TransmissionMetrics aTransmissionMetrics) Constructs an accordingPacketInputStream
instance wrapping the givenInputStream
. -
Method Summary
Modifier and TypeMethodDescriptionint
static PacketInputStream.Builder
builder()
Creates builder to buildPacketInputStream
.void
close()
protected void
int
org.refcodes.numerical.Endianess
int
Retrieves the packet length width (in bytes) from the packet length width (in bytes) property.byte[]
Retrieves the magic bytes from the packet magic bytes property.Retrieves theSegmentPackager
from the packetSegmentPackager
property.int
int
Retrieves the sequence number from the sequence number property.org.refcodes.mixin.ConcatenateMode
Retrieves theConcatenateMode
from the sequence numberConcatenateMode
property.int
Retrieves the sequence number initial value from the sequence number initial value property.int
Retrieves the sequence number width (in bytes) from the sequence number width (in bytes) property.void
mark
(int readlimit) boolean
int
read()
void
reset()
Methods inherited from class java.io.InputStream
nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
-
Field Details
-
_blockOffset
protected int _blockOffset -
_inputStream
-
_packetSegment
-
_sequenceNumber
protected int _sequenceNumber -
_sequenceNumberInitValue
protected int _sequenceNumberInitValue -
_sequenceNumberSegment
-
_allocSegment
-
_boundedSequence
-
-
Constructor Details
-
PacketInputStream
Constructs an accordingPacketInputStream
instance wrapping the givenInputStream
. The configuration attributes are taken from theTransmissionMetrics
configuration object, though only those attributes are supported which are also supported by the other constructors!- Parameters:
aInputStream
- TheInputStream
to be wrapped.aTransmissionMetrics
- TheTransmissionMetrics
to be used for configuring this instance.
-
PacketInputStream
public PacketInputStream(InputStream aInputStream, int aBlockSize, int aPacketLengthWidth, byte[] aPacketMagicBytes, int aSequenceNumberInitValue, int aSequenceNumberWidth, org.refcodes.mixin.ConcatenateMode aSequenceNumberConcatenateMode, SegmentPackager aPacketSegmentPackager, org.refcodes.numerical.Endianess aEndianess) Constructs an accordingPacketInputStream
instance wrapping the givenInputStream
.- Parameters:
aInputStream
- TheInputStream
to be wrapped.aBlockSize
- The block size of a data block for each packet.aPacketLengthWidth
- The width (bytes) for declaring the (max) length of a package.aPacketMagicBytes
- The magic bytes identifying a packet and distinguishing a packet from a last package.aSequenceNumberInitValue
- The initial sequence number from where to start counting the blocks.aSequenceNumberWidth
- The width (in bytes) to be used for sequence number values.aSequenceNumberConcatenateMode
- The mode of concatenation to use when creating aSequence
from thisTransmission
and the decoratedTransmission
.aPacketSegmentPackager
- An (optional)SegmentPackager
used to modify a packet's data e.g. with a CRC checksum.aEndianess
- TheEndianess
to use for integer (double) numbers and the like.
-
-
Method Details
-
builder
Creates builder to buildPacketInputStream
.- Returns:
- created builder
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
getBlockSize
public int getBlockSize()- Specified by:
getBlockSize
in interfaceorg.refcodes.mixin.BlockSizeAccessor
-
getEndianess
public org.refcodes.numerical.Endianess getEndianess()- Specified by:
getEndianess
in interfaceorg.refcodes.numerical.EndianessAccessor
-
getPacketMagicBytes
public byte[] getPacketMagicBytes()Retrieves the magic bytes from the packet magic bytes property.- Specified by:
getPacketMagicBytes
in interfacePacketMagicBytesAccessor
- Returns:
- The magic bytes stored by the packet magic bytes property.
-
getPacketSegmentPackager
Retrieves theSegmentPackager
from the packetSegmentPackager
property.- Specified by:
getPacketSegmentPackager
in interfacePacketSegmentPackagerAccessor
- Returns:
- The
SegmentPackager
stored by the packetSegmentPackager
property.
-
getPacketSize
public int getPacketSize()- Specified by:
getPacketSize
in interfaceorg.refcodes.mixin.PacketSizeAccessor
-
getSequenceNumber
public int getSequenceNumber()Retrieves the sequence number from the sequence number property.- Specified by:
getSequenceNumber
in interfaceSequenceNumberAccessor
- Returns:
- The sequence number stored by the sequence number property.
-
getSequenceNumberConcatenateMode
public org.refcodes.mixin.ConcatenateMode getSequenceNumberConcatenateMode()Retrieves theConcatenateMode
from the sequence numberConcatenateMode
property.- Specified by:
getSequenceNumberConcatenateMode
in interfaceSequenceNumberConcatenateModeAccessor
- Returns:
- The
ConcatenateMode
stored by the sequence numberConcatenateMode
property.
-
getSequenceNumberInitValue
public int getSequenceNumberInitValue()Retrieves the sequence number initial value from the sequence number initial value property.- Specified by:
getSequenceNumberInitValue
in interfaceSequenceNumberInitValueAccessor
- Returns:
- The sequence number initial value stored by the sequence number initial value property.
-
getSequenceNumberWidth
public int getSequenceNumberWidth()Retrieves the sequence number width (in bytes) from the sequence number width (in bytes) property.- Specified by:
getSequenceNumberWidth
in interfaceSequenceNumberWidthAccessor
- Returns:
- The sequence number width (in bytes) stored by the sequence number width (in bytes) property.
-
getPacketLengthWidth
public int getPacketLengthWidth()Retrieves the packet length width (in bytes) from the packet length width (in bytes) property.- Specified by:
getPacketLengthWidth
in interfacePacketLengthWidthAccessor
- Returns:
- The packet length width (in bytes) stored by the packet length width (in bytes) property.
-
mark
public void mark(int readlimit) - Overrides:
mark
in classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classInputStream
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
reset
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
doReceivePacket
- Throws:
IOException
-