org.apache.hc.core5.http.impl.nio

## Class LengthDelimitedEncoder

- java.lang.Object
- - org.apache.hc.core5.http.impl.nio.AbstractContentEncoder
  - - org.apache.hc.core5.http.impl.nio.LengthDelimitedEncoder

- All Implemented Interfaces:
    ContentEncoder, FileContentEncoder

      public class LengthDelimitedEncoder
      extends AbstractContentEncoder
      implements FileContentEncoder

  Content encoder that cuts off after a defined number of bytes. This class is used to send content of HTTP messages where the end of the content entity is determined by the value of the `Content-Length header`. Entities transferred using this stream can be maximum `Long.MAX_VALUE` long.

  This decoder is optimized to transfer data directly from a `FileChannel` to the underlying I/O session's channel whenever possible avoiding intermediate buffering in the session buffer.

  Since:
    4.0

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `LengthDelimitedEncoder(WritableByteChannel channel, SessionOutputBuffer buffer, BasicHttpTransportMetrics metrics, long contentLength)`
    `LengthDelimitedEncoder(WritableByteChannel channel, SessionOutputBuffer buffer, BasicHttpTransportMetrics metrics, long contentLength, int chunkSizeHint)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `toString()`
    `long` | `transfer(FileChannel src, long position, long count)` Transfers a portion of entity content from the given file channel to the underlying network channel.
    `int` | `write(ByteBuffer src)` Writes a portion of entity content to the underlying channel.

    - ### Methods inherited from class org.apache.hc.core5.http.impl.nio.AbstractContentEncoder

      `assertNotCompleted, buffer, channel, complete, complete, flushToChannel, isCompleted, metrics, writeToBuffer, writeToChannel, writeToChannel`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.apache.hc.core5.http.nio.ContentEncoder

      `complete, isCompleted`

- - ### Constructor Detail

    - #### LengthDelimitedEncoder

          public LengthDelimitedEncoder(WritableByteChannel channel,
                                SessionOutputBuffer buffer,
                                BasicHttpTransportMetrics metrics,
                                long contentLength,
                                int chunkSizeHint)

      Parameters:
        `channel` - underlying channel.
        `buffer` - session buffer.
        `metrics` - transport metrics.
        `contentLength` - content length.
        `chunkSizeHint` - fragment size hint defining an minimal size of a fragment that should be written out directly to the channel bypassing the session buffer. Value `0` disables fragment buffering.
      Since:
        4.3

    - #### LengthDelimitedEncoder

          public LengthDelimitedEncoder(WritableByteChannel channel,
                                SessionOutputBuffer buffer,
                                BasicHttpTransportMetrics metrics,
                                long contentLength)

  - ### Method Detail

    - #### write

          public int write(ByteBuffer src)
                    throws IOException

      Description copied from interface: `ContentEncoder`
      Writes a portion of entity content to the underlying channel.
      Specified by:
        `write` in interface `ContentEncoder`
      Parameters:
        `src` - The buffer from which content is to be retrieved
      Returns:
        The number of bytes read, possibly zero
      Throws:
        `IOException` - if I/O error occurs while writing content

    - #### transfer

          public long transfer(FileChannel src,
                      long position,
                      long count)
                        throws IOException

      Description copied from interface: `FileContentEncoder`
      Transfers a portion of entity content from the given file channel to the underlying network channel.
      Specified by:
        `transfer` in interface `FileContentEncoder`
      Parameters:
        `src` - the source FileChannel to transfer data from.
        `position` - The position within the file at which the transfer is to begin; must be non-negative
        `count` - The maximum number of bytes to be transferred; must be non-negative
      Returns:
        The number of bytes, possibly zero, that were actually transferred
      Throws:
        `IOException` - if some I/O error occurs.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

Copyright © 2005–2022 The Apache Software Foundation. All rights reserved.