Class PacketOutputStream

java.lang.Object
java.io.OutputStream
org.refcodes.serial.PacketOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, org.refcodes.mixin.BlockSizeAccessor, org.refcodes.mixin.PacketSizeAccessor, org.refcodes.numerical.EndianessAccessor, PacketLengthWidthAccessor, PacketMagicBytesAccessor, PacketSegmentPackagerAccessor, SequenceNumberAccessor, SequenceNumberConcatenateModeAccessor, SequenceNumberInitValueAccessor, SequenceNumberWidthAccessor
Direct Known Subclasses:
StopAndWaitPacketOutputStream

public class PacketOutputStream extends OutputStream implements PacketLengthWidthAccessor, org.refcodes.mixin.PacketSizeAccessor, org.refcodes.numerical.EndianessAccessor, org.refcodes.mixin.BlockSizeAccessor, SequenceNumberInitValueAccessor, SequenceNumberConcatenateModeAccessor, SequenceNumberAccessor, SequenceNumberWidthAccessor, PacketSegmentPackagerAccessor, PacketMagicBytesAccessor
The PacketOutputStream wraps an OutputStream and chunks any data to be written into packets with a sequence number, a block of data and a CRC checksum. An according StopAndWaitPacketInputStream then reverts the packetised data stream while performing CRC checksum validation as well as sequence number validation. ATTENTION: In order to send the last packet (block of data), call flush() so that it is transmitted even if the block is not completely filled with bytes.
  • Field Details

    • _blockOffset

      protected int _blockOffset
    • _blockSequence

      protected Sequence _blockSequence
    • _endianess

      protected org.refcodes.numerical.Endianess _endianess
    • _outputStream

      protected OutputStream _outputStream
    • _packetSegment

      protected Segment _packetSegment
    • _sequenceNumber

      protected int _sequenceNumber
    • _sequenceNumberSegment

      protected NumberSegment _sequenceNumberSegment
    • _sequenceNumberWidth

      protected int _sequenceNumberWidth
    • _allocSegment

    • _boundedSequence

      protected BoundedSequenceDecorator _boundedSequence
  • Constructor Details

    • PacketOutputStream

      public PacketOutputStream(OutputStream aOutputStream, TransmissionMetrics aTransmissionMetrics)
      Constructs an according PacketOutputStream instance wrapping the given OutputStream. The configuration attributes are taken from the TransmissionMetrics configuration object, though only those attributes are supported which are also supported by the other constructors!
      Parameters:
      aOutputStream - The OutputStream to be wrapped.
      aTransmissionMetrics - The TransmissionMetrics to be used for configuring this instance.
    • PacketOutputStream

      public PacketOutputStream(OutputStream aOutputStream, int aBlockSize, int aPacketLengthWidth, byte[] aPacketMagicBytes, int aSequenceNumberInitValue, int aSequenceNumberWidth, org.refcodes.mixin.ConcatenateMode aSequenceNumberConcatenateMode, SegmentPackager aPacketSegmentPackager, org.refcodes.numerical.Endianess aEndianess)
      Constructs an according PacketOutputStream instance wrapping the given OutputStream.
      Parameters:
      aOutputStream - The OutputStream 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 a Sequence from this Transmission and the decorated Transmission.
      aPacketSegmentPackager - An (optional) SegmentPackager used to modify a packet's data e.g. with a CRC checksum.
      aEndianess - The Endianess to use for integer (double) numbers and the like.
  • Method Details

    • builder

      public static PacketOutputStream.Builder builder()
      Creates builder to build PacketOutputStream.
      Returns:
      created builder
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      In case a packet is not complete (e.g. it's block's data did filled till the getBlockSize() length), then the package is sent with it's block data "as is" (a last block's data might get filled up with zeros).
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • getBlockSize

      public int getBlockSize()
      Specified by:
      getBlockSize in interface org.refcodes.mixin.BlockSizeAccessor
    • getEndianess

      public org.refcodes.numerical.Endianess getEndianess()
      Specified by:
      getEndianess in interface org.refcodes.numerical.EndianessAccessor
    • getPacketMagicBytes

      public byte[] getPacketMagicBytes()
      Retrieves the magic bytes from the packet magic bytes property.
      Specified by:
      getPacketMagicBytes in interface PacketMagicBytesAccessor
      Returns:
      The magic bytes stored by the packet magic bytes property.
    • getPacketSegmentPackager

      public SegmentPackager getPacketSegmentPackager()
      Retrieves the SegmentPackager from the packet SegmentPackager property.
      Specified by:
      getPacketSegmentPackager in interface PacketSegmentPackagerAccessor
      Returns:
      The SegmentPackager stored by the packet SegmentPackager property.
    • getPacketSize

      public int getPacketSize()
      Specified by:
      getPacketSize in interface org.refcodes.mixin.PacketSizeAccessor
    • getSequenceNumber

      public int getSequenceNumber()
      Retrieves the sequence number from the sequence number property.
      Specified by:
      getSequenceNumber in interface SequenceNumberAccessor
      Returns:
      The sequence number stored by the sequence number property.
    • getSequenceNumberConcatenateMode

      public org.refcodes.mixin.ConcatenateMode getSequenceNumberConcatenateMode()
      Retrieves the ConcatenateMode from the sequence number ConcatenateMode property.
      Specified by:
      getSequenceNumberConcatenateMode in interface SequenceNumberConcatenateModeAccessor
      Returns:
      The ConcatenateMode stored by the sequence number ConcatenateMode property.
    • getSequenceNumberInitValue

      public int getSequenceNumberInitValue()
      Retrieves the sequence number initial value from the sequence number initial value property.
      Specified by:
      getSequenceNumberInitValue in interface SequenceNumberInitValueAccessor
      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 interface SequenceNumberWidthAccessor
      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 interface PacketLengthWidthAccessor
      Returns:
      The packet length width (in bytes) stored by the packet length width (in bytes) property.
    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • doTransmitPacket

      protected void doTransmitPacket() throws IOException
      Do transmit packet.
      Throws:
      IOException - Signals that an I/O exception has occurred.
      TransmissionSequenceException - the transmission sequence exception