Class ChunkedConsumer

  • All Implemented Interfaces:
    BodyConsumer, ByteConsumer

    public class ChunkedConsumer
    extends UpdateConsumer
    The ChunkedConsumer is reads an decodes a stream using the chunked transfer coding. This is used so that any data sent in the chunked transfer coding can be decoded. All bytes are appended to an internal buffer so that they can be read without having to parse the encoding.
    
        length := 0
        read chunk-size, chunk-extension (if any) and CRLF
        while (chunk-size > 0) {
           read chunk-data and CRLF
           append chunk-data to entity-body
           length := length + chunk-size
           read chunk-size and CRLF
        }
        read entity-header
        while (entity-header not empty) {
           append entity-header to existing header fields
           read entity-header
        }
    
     
    The above algorithm is taken from RFC 2616 section 19.4.6. This coding scheme is used in HTTP pipelines so that dynamic content, that is, content with which a length cannot be determined does not require a connection close to delimit the message body.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Allocator allocator
      This is used to create the internal buffer for the body.
      private Buffer buffer
      This is the internal buffer used to capture the body read.
      private int chunk
      This is the number of bytes left in the current chunk.
      private int count
      This is the number of bytes appended to the line buffer.
      private boolean last
      This is used to determine if the zero length chunk was read.
      private byte[] line
      This is used to accumulate the bytes of the chunk size line.
      private boolean terminal
      This is used to determine whether a full chunk has been read.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ChunkedConsumer​(Allocator allocator)
      Constructor for the ChunkedConsumer object.
      private ChunkedConsumer​(Allocator allocator, int chunk)
      Constructor for the ChunkedConsumer object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void append​(byte[] array, int off, int len)
      This method is used to append the contents of the array to the internal buffer.
      Body getBody()
      This is used to acquire the body that has been consumed.
      private void parse()
      This method is used to convert the size in hexidecimal to a decimal int.
      private int toDecimal​(byte octet)
      This performs a conversion from a character to an integer.
      protected int update​(byte[] array, int off, int size)
      This is used to process the bytes that have been read from the cursor.
      • Methods inherited from class java.lang.Object

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

      • allocator

        private Allocator allocator
        This is used to create the internal buffer for the body.
      • buffer

        private Buffer buffer
        This is the internal buffer used to capture the body read.
      • terminal

        private boolean terminal
        This is used to determine whether a full chunk has been read.
      • last

        private boolean last
        This is used to determine if the zero length chunk was read.
      • line

        private byte[] line
        This is used to accumulate the bytes of the chunk size line.
      • count

        private int count
        This is the number of bytes appended to the line buffer.
      • chunk

        private int chunk
        This is the number of bytes left in the current chunk.
    • Constructor Detail

      • ChunkedConsumer

        public ChunkedConsumer​(Allocator allocator)
        Constructor for the ChunkedConsumer object. This is used to create a consumer that reads chunked encoded data and appended that data in decoded form to an internal buffer so that it can be read in a clean decoded fromat.
        Parameters:
        allocator - this is used to allocate the internal buffer
      • ChunkedConsumer

        private ChunkedConsumer​(Allocator allocator,
                                int chunk)
        Constructor for the ChunkedConsumer object. This is used to create a consumer that reads chunked encoded data and appended that data in decoded form to an internal buffer so that it can be read in a clean decoded fromat.
        Parameters:
        allocator - this is used to allocate the internal buffer
        chunk - this is the maximum size line allowed
    • Method Detail

      • getBody

        public Body getBody()
        This is used to acquire the body that has been consumed. This will return a body which can be used to read the content of the message, also if the request is multipart upload then all of the parts are provided as Attachment objects. Each part can then be read as an individual message.
        Returns:
        the body that has been consumed by this instance
      • append

        private void append​(byte[] array,
                            int off,
                            int len)
                     throws java.io.IOException
        This method is used to append the contents of the array to the internal buffer. The appended bytes can be acquired from the internal buffer using an InputStream, or the text of the appended bytes can be acquired by encoding the bytes.
        Parameters:
        array - this is the array of bytes to be appended
        off - this is the start offset in the array to read from
        len - this is the number of bytes to write to the buffer
        Throws:
        java.io.IOException
      • update

        protected int update​(byte[] array,
                             int off,
                             int size)
                      throws java.io.IOException
        This is used to process the bytes that have been read from the cursor. This will keep reading bytes from the stream until such time as the zero length chunk has been read from the stream. If the zero length chunk is encountered then the overflow count is returned so it can be used to reset the cursor.
        Specified by:
        update in class UpdateConsumer
        Parameters:
        array - this is a chunk read from the cursor
        off - this is the offset within the array the chunk starts
        size - this is the number of bytes within the array
        Returns:
        this returns the number of bytes overflow that is read
        Throws:
        java.io.IOException
      • parse

        private void parse()
                    throws java.io.IOException
        This method is used to convert the size in hexidecimal to a decimal int. This will use the specified number of bytes from the internal buffer and parse each character read as a hexidecimal character. This stops interpreting the size line when a non-hexidecimal character is encountered.
        Throws:
        java.io.IOException
      • toDecimal

        private int toDecimal​(byte octet)
        This performs a conversion from a character to an integer. If the character given, as a byte, is a hexidecimal char this will convert it into its integer equivelant. So a char of A is converted into 10.
        Parameters:
        octet - this is an ISO 8869-1 hexidecimal character
        Returns:
        returns the hex character into its decinal value