Class ContentConsumer

  • All Implemented Interfaces:
    BodyConsumer, ByteConsumer

    class ContentConsumer
    extends UpdateConsumer
    The ContentConsumer object represents a consumer for a multipart body part. This will read the contents of the cursor until such time as it reads the terminal boundary token, which is used to frame the content. Once the boundary token has been read this will add itself as a part to a part list. This part list can then be used with the HTTP request to examine and use the part.
    See Also:
    PartConsumer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Allocator allocator
      This is used to allocate the internal buffer when required.
      private byte[] boundary
      Represents the message boundary that terminates the part body.
      private Buffer buffer
      This is the internal buffer used to house the part body.
      private int seek
      This is used to determine how many boundary tokens are read.
      private Segment segment
      Represents the HTTP headers that were provided for the part.
      private PartSeries series
      This is the part list that this part is to be added to.
      private int start
      This is used to determine if the start token had been read.
      private static byte[] START
      This represents the start of the boundary token for the body.
    • 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.
      protected void commit​(ByteCursor cursor)
      This is used to push the start and boundary back on to the cursor.
      Body getBody()
      This is used to acquire the body for this HTTP entity.
      Part getPart()
      This is used to acquire the part for this HTTP entity.
      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

      • START

        private static final byte[] START
        This represents the start of the boundary token for the body.
      • series

        private PartSeries series
        This is the part list that this part is to be added to.
      • allocator

        private Allocator allocator
        This is used to allocate the internal buffer when required.
      • segment

        private Segment segment
        Represents the HTTP headers that were provided for the part.
      • buffer

        private Buffer buffer
        This is the internal buffer used to house the part body.
      • boundary

        private byte[] boundary
        Represents the message boundary that terminates the part body.
      • start

        private int start
        This is used to determine if the start token had been read.
      • seek

        private int seek
        This is used to determine how many boundary tokens are read.
    • Constructor Detail

      • ContentConsumer

        public ContentConsumer​(Allocator allocator,
                               Segment segment,
                               PartSeries series,
                               byte[] boundary)
        Constructor for the ContentConsumer object. This is used to create a consumer that reads the body of a part in a multipart request body. The terminal token must be provided so that the end of the part body can be determined.
        Parameters:
        allocator - this is used to allocate the internal buffer
        segment - this represents the headers for the part body
        series - this is the part list that this body belongs in
        boundary - this is the message boundary for the body part
    • Method Detail

      • getBody

        public Body getBody()
        This is used to acquire the body for this HTTP entity. 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 Part objects. Each part can then be read as an individual message.
        Returns:
        the body provided by the HTTP request message
      • getPart

        public Part getPart()
        This is used to acquire the part for this HTTP entity. This will return a part which can be used to read the content of the message, the part created contains the contents of the body and the headers associated with it.
        Returns:
        the part provided by the HTTP request message
      • 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
      • commit

        protected void commit​(ByteCursor cursor)
                       throws java.io.IOException
        This is used to push the start and boundary back on to the cursor. Pushing the boundary back on to the cursor is required to ensure that the next consumer will have valid data to read from it. Simply resetting the boundary is not enough as this can cause an infinite loop if the connection is bad.
        Overrides:
        commit in class UpdateConsumer
        Parameters:
        cursor - this is the cursor used by this consumer
        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 search for the boundary token within the body of the message part, when it is found this will returns the number of bytes that represent the overflow.
        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