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

## Class LengthDelimitedDecoder

- java.lang.Object
- - org.apache.hc.core5.http.impl.nio.AbstractContentDecoder
  - - org.apache.hc.core5.http.impl.nio.LengthDelimitedDecoder

- All Implemented Interfaces:
    ContentDecoder, FileContentDecoder

      public class LengthDelimitedDecoder
      extends AbstractContentDecoder
      implements FileContentDecoder

  Content decoder that cuts off after a defined number of bytes. This class is used to receive 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 the underlying I/O session's channel to a `FileChannel`, whenever possible avoiding intermediate buffering in the session buffer.

  Since:
    4.0

- - ### Field Summary

    - ### Fields inherited from class org.apache.hc.core5.http.impl.nio.AbstractContentDecoder

      `completed`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `LengthDelimitedDecoder(ReadableByteChannel channel, SessionInputBuffer buffer, BasicHttpTransportMetrics metrics, long contentLength)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `read(ByteBuffer dst)` Reads a portion of content from the underlying channel
    `String` | `toString()`
    `long` | `transfer(FileChannel dst, long position, long count)` Transfers a portion of entity content from the underlying network channel into the given file channel.

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

      `buffer, channel, fillBufferFromChannel, getTrailers, isCompleted, metrics, readFromChannel, readFromChannel, setCompleted, setCompleted`

    - ### 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.ContentDecoder

      `getTrailers, isCompleted`

- - ### Constructor Detail

    - #### LengthDelimitedDecoder

          public LengthDelimitedDecoder(ReadableByteChannel channel,
                                        SessionInputBuffer buffer,
                                        BasicHttpTransportMetrics metrics,
                                        long contentLength)

  - ### Method Detail

    - #### read

          public int read(ByteBuffer dst)
                   throws IOException

      Description copied from interface: `ContentDecoder`
      Reads a portion of content from the underlying channel
      Specified by:
        `read` in interface `ContentDecoder`
      Parameters:
        `dst` - The buffer into which entity content is to be transferred
      Returns:
        The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
      Throws:
        `IOException` - if I/O error occurs while reading content

    - #### transfer

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

      Description copied from interface: `FileContentDecoder`
      Transfers a portion of entity content from the underlying network channel into the given file channel.

      Warning: Many implementations cannot write beyond the length of the file. If the position exceeds the channel's size, some implementations may throw an IOException.

      Specified by:
        `transfer` in interface `FileContentDecoder`
      Parameters:
        `dst` - the target FileChannel to transfer data into.
        `position` - The position within the file at which the transfer is to begin; must be non-negative. Must be less than or equal to the size of the file
        `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–2021 The Apache Software Foundation. All rights reserved.