Class ChunkedEncoder

java.lang.Object
org.simpleframework.http.core.ChunkedEncoder
All Implemented Interfaces:
BodyEncoder

class ChunkedEncoder extends Object implements BodyEncoder
The ChunkedEncoder object is used to encode data in the chunked encoding format. A chunked producer is required when the length of the emitted content is unknown. It enables the HTTP pipeline to remain open as it is a self delimiting format. This is preferred over the CloseEncoder for HTTP/1.1 as it maintains the pipeline and thus the cost of creating it.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private byte[]
    This is the hexadecimal alphabet used to translate the size.
    private BodyObserver
    This is the observer used to notify the selector of events.
    private byte[]
    This is the size line which is used to generate the size.
    private ByteWriter
    This is the underlying writer used to deliver the encoded data.
    private byte[]
    This is the zero length chunk sent when this is completed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the ChunkedEncoder object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This is used to signal to the producer that all content has been written and the user no longer needs to write.
    void
    encode(byte[] array)
    This method is used to encode the provided array of bytes in a HTTP/1.1 complaint format and sent it to the client.
    void
    encode(byte[] array, int off, int len)
    This method is used to encode the provided array of bytes in a HTTP/1.1 complaint format and sent it to the client.
    void
    This method is used to encode the provided buffer of bytes in a HTTP/1.1 compliant format and sent it to the client.
    void
    encode(ByteBuffer buffer, int off, int len)
    This method is used to encode the provided buffer of bytes in a HTTP/1.1 compliant format and sent it to the client.
    private void
    This method is used to write the zero length chunk.
    void
    This method is used to flush the contents of the buffer to the client.

    Methods inherited from class java.lang.Object

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

    • size

      private byte[] size
      This is the size line which is used to generate the size.
    • index

      private byte[] index
      This is the hexadecimal alphabet used to translate the size.
    • zero

      private byte[] zero
      This is the zero length chunk sent when this is completed.
    • observer

      private BodyObserver observer
      This is the observer used to notify the selector of events.
    • writer

      private ByteWriter writer
      This is the underlying writer used to deliver the encoded data.
  • Constructor Details

    • ChunkedEncoder

      public ChunkedEncoder(BodyObserver observer, ByteWriter writer)
      Constructor for the ChunkedEncoder object. This is used to create a producer that can sent data in the chunked encoding format. Once the data is encoded in the format it is handed to the provided ByteWriter object which will then deliver it to the client using the underlying transport.
      Parameters:
      observer - this is the observer used to signal I/O events
      writer - this is the writer used to deliver the content
  • Method Details

    • encode

      public void encode(byte[] array) throws IOException
      This method is used to encode the provided array of bytes in a HTTP/1.1 complaint format and sent it to the client. Once the data has been encoded it is handed to the transport layer within the server, which may choose to buffer the data if the content is too small to send efficiently or if the socket is not write ready.
      Specified by:
      encode in interface BodyEncoder
      Parameters:
      array - this is the array of bytes to send to the client
      Throws:
      IOException
    • encode

      public void encode(byte[] array, int off, int len) throws IOException
      This method is used to encode the provided array of bytes in a HTTP/1.1 complaint format and sent it to the client. Once the data has been encoded it is handed to the transport layer within the server, which may choose to buffer the data if the content is too small to send efficiently or if the socket is not write ready.
      Specified by:
      encode in interface BodyEncoder
      Parameters:
      array - this is the array of bytes to send to the client
      off - this is the offset within the array to send from
      len - this is the number of bytes that are to be sent
      Throws:
      IOException
    • encode

      public void encode(ByteBuffer buffer) throws IOException
      This method is used to encode the provided buffer of bytes in a HTTP/1.1 compliant format and sent it to the client. Once the data has been encoded it is handed to the transport layer within the server, which may choose to buffer the data if the content is too small to send efficiently or if the socket is not write ready.
      Specified by:
      encode in interface BodyEncoder
      Parameters:
      buffer - this is the buffer of bytes to send to the client
      Throws:
      IOException
    • encode

      public void encode(ByteBuffer buffer, int off, int len) throws IOException
      This method is used to encode the provided buffer of bytes in a HTTP/1.1 compliant format and sent it to the client. Once the data has been encoded it is handed to the transport layer within the server, which may choose to buffer the data if the content is too small to send efficiently or if the socket is not write ready.
      Specified by:
      encode in interface BodyEncoder
      Parameters:
      buffer - this is the buffer of bytes to send to the client
      off - this is the offset within the buffer to send from
      len - this is the number of bytes that are to be sent
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      This method is used to flush the contents of the buffer to the client. This method will block until such time as all of the data has been sent to the client. If at any point there is an error sending the content an exception is thrown.
      Specified by:
      flush in interface BodyEncoder
      Throws:
      IOException
    • finish

      private void finish() throws IOException
      This method is used to write the zero length chunk. Writing the zero length chunk tells the client that the response has been fully sent, and the next sequence of bytes from the HTTP pipeline is the start of the next response. This will signal to the server kernel that the next request is read to read.
      Throws:
      IOException
    • close

      public void close() throws IOException
      This is used to signal to the producer that all content has been written and the user no longer needs to write. This will either close the underlying transport or it will notify the monitor that the response has completed and the next request can begin. This ensures the content is flushed to the client.
      Specified by:
      close in interface BodyEncoder
      Throws:
      IOException