Class TcpMessageBuffer


  • public class TcpMessageBuffer
    extends java.lang.Object
    The message buffer hold state about a partially received message until the data is complete.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected byte[] body
      The body buffer to be filled while reading.
      protected byte[] header
      The header buffer to be filled while reading.
      protected int maxsize
      Maximum message size.
      protected int pos
      The amount of data already read in the current array.
      protected java.nio.ByteBuffer rb
      The read buffer for reading out the messages.
      protected java.nio.ByteBuffer wb
      The write buffer for the channel.
    • Constructor Summary

      Constructors 
      Constructor Description
      TcpMessageBuffer​(int maxsize)
      Create a message buffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Tuple2<byte[],​byte[]> read​(java.nio.channels.SocketChannel sc)
      Read messages from the channel.
      protected byte[] readBytes​(byte[] data)
      Read data in to the given array, if any.
      • Methods inherited from class java.lang.Object

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

      • wb

        protected java.nio.ByteBuffer wb
        The write buffer for the channel.
      • rb

        protected java.nio.ByteBuffer rb
        The read buffer for reading out the messages.
      • header

        protected byte[] header
        The header buffer to be filled while reading.
      • body

        protected byte[] body
        The body buffer to be filled while reading.
      • pos

        protected int pos
        The amount of data already read in the current array.
      • maxsize

        protected int maxsize
        Maximum message size.
    • Constructor Detail

      • TcpMessageBuffer

        public TcpMessageBuffer​(int maxsize)
        Create a message buffer.
    • Method Detail

      • read

        public Tuple2<byte[],​byte[]> read​(java.nio.channels.SocketChannel sc)
                                         throws java.io.IOException
        Read messages from the channel. Always reads the length as 4 byte int and then reads the required amount of data into an array.
        Returns:
        Two byte arrays, when the next message is complete, null as long as data is still pending.
        Throws:
        java.io.IOException - on read error.
      • readBytes

        protected byte[] readBytes​(byte[] data)
                            throws java.io.IOException
        Read data in to the given array, if any. Also adjusts the pos varaiable.
        Parameters:
        data - The already read data, if any.
        Returns:
        The maybe updated data array.
        Throws:
        java.io.IOException