Class FrameWriter

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.nio.ByteBuffer frameBytes  
    • Constructor Summary

      Constructors 
      Constructor Description
      FrameWriter()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract java.nio.ByteBuffer buildFrame​(byte[] header, int headerOffset, int headerLength, byte[] payload, int payloadOffset, int payloadLength)  
      void clear()
      Release the byte buffer.
      boolean isComplete()  
      void withHeaderAndPayload​(byte[] header, byte[] payload)
      Provide (maybe empty) header and payload to the frame.
      void withHeaderAndPayload​(byte[] header, int headerOffset, int headerLength, byte[] payload, int payloadOffset, int payloadLength)
      Provide extra header and payload to the frame.
      void withOnlyPayload​(byte[] payload)
      Provide only payload to the frame.
      abstract void withOnlyPayload​(byte[] payload, int offset, int length)
      Provide only payload to the frame.
      void write​(TNonblockingTransport transport)
      Nonblocking write to the underlying transport.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • frameBytes

        protected java.nio.ByteBuffer frameBytes
    • Constructor Detail

      • FrameWriter

        public FrameWriter()
    • Method Detail

      • withHeaderAndPayload

        public void withHeaderAndPayload​(byte[] header,
                                         byte[] payload)
        Provide (maybe empty) header and payload to the frame. This can be called only when isComplete returns true (last frame has been written out).
        Parameters:
        header - Some extra header bytes (without the 4 bytes for payload length), which will be the start of the frame. It can be empty, depending on the message format
        payload - Payload as a byte array
        Throws:
        java.lang.IllegalStateException - if it is called when isComplete returns false
        java.lang.IllegalArgumentException - if header or payload is invalid
      • withHeaderAndPayload

        public void withHeaderAndPayload​(byte[] header,
                                         int headerOffset,
                                         int headerLength,
                                         byte[] payload,
                                         int payloadOffset,
                                         int payloadLength)
        Provide extra header and payload to the frame.
        Parameters:
        header - byte array containing the extra header
        headerOffset - starting offset of the header portition
        headerLength - length of the extra header
        payload - byte array containing the payload
        payloadOffset - starting offset of the payload portion
        payloadLength - length of the payload
        Throws:
        java.lang.IllegalStateException - if preivous frame is not yet complete (isComplete returns fals)
        java.lang.IllegalArgumentException - if header or payload is invalid
      • withOnlyPayload

        public void withOnlyPayload​(byte[] payload)
        Provide only payload to the frame. Throws UnsupportedOperationException if the frame expects a header.
        Parameters:
        payload - payload as a byte array
      • withOnlyPayload

        public abstract void withOnlyPayload​(byte[] payload,
                                             int offset,
                                             int length)
        Provide only payload to the frame. Throws UnsupportedOperationException if the frame expects a header.
        Parameters:
        payload - The underlying byte array as a recipient of the payload
        offset - The offset in the byte array starting from where the payload is located
        length - The length of the payload
      • buildFrame

        protected abstract java.nio.ByteBuffer buildFrame​(byte[] header,
                                                          int headerOffset,
                                                          int headerLength,
                                                          byte[] payload,
                                                          int payloadOffset,
                                                          int payloadLength)
      • isComplete

        public boolean isComplete()
        Returns:
        true when no more data needs to be written out
      • clear

        public void clear()
        Release the byte buffer.