org.apache.hc.core5.http.impl.io

## Class ChunkedOutputStream

- java.lang.Object
- - java.io.OutputStream
  - - org.apache.hc.core5.http.impl.io.ChunkedOutputStream

- All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

      public class ChunkedOutputStream
      extends OutputStream

  Implements chunked transfer coding. The content is sent in small chunks. Entities transferred using this output stream can be of unlimited length. Writes are buffered to an internal buffer (2048 default size).

  Note that this class NEVER closes the underlying stream, even when `close()` gets called. Instead, the stream will be marked as closed and no further output will be permitted.

  Since:
    4.0

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ChunkedOutputStream(SessionOutputBuffer buffer, OutputStream outputStream, byte[] chunkCache, Supplier<List<? extends Header>> trailerSupplier)` Default constructor.
    `ChunkedOutputStream(SessionOutputBuffer buffer, OutputStream outputStream, int chunkSizeHint)` Constructor with no trailers.
    `ChunkedOutputStream(SessionOutputBuffer buffer, OutputStream outputStream, int chunkSizeHint, Supplier<List<? extends Header>> trailerSupplier)` Constructor taking an integer chunk size hint.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Finishes writing to the underlying stream, but does NOT close the underlying stream.
    `void` | `finish()` Must be called to ensure the internal cache is flushed and the closing chunk is written.
    `void` | `flush()` Flushes the content buffer and the underlying stream.
    `void` | `write(byte[] b)` Writes the array.
    `void` | `write(byte[] src, int off, int len)` Writes the array.
    `void` | `write(int b)`

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

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

- - ### Constructor Detail

    - #### ChunkedOutputStream

          public ChunkedOutputStream(SessionOutputBuffer buffer,
                             OutputStream outputStream,
                             byte[] chunkCache,
                             Supplier<List<? extends Header>> trailerSupplier)

      Default constructor.
      Parameters:
        `buffer` - Session output buffer
        `outputStream` - Output stream
        `chunkCache` - Buffer used to aggregate smaller writes into chunks.
        `trailerSupplier` - Trailer supplier. May be `null`
      Since:
        5.1

    - #### ChunkedOutputStream

          public ChunkedOutputStream(SessionOutputBuffer buffer,
                             OutputStream outputStream,
                             int chunkSizeHint,
                             Supplier<List<? extends Header>> trailerSupplier)

      Constructor taking an integer chunk size hint.
      Parameters:
        `buffer` - Session output buffer
        `outputStream` - Output stream
        `chunkSizeHint` - minimal chunk size hint
        `trailerSupplier` - Trailer supplier. May be `null`
      Since:
        5.0

    - #### ChunkedOutputStream

          public ChunkedOutputStream(SessionOutputBuffer buffer,
                             OutputStream outputStream,
                             int chunkSizeHint)

      Constructor with no trailers.
      Parameters:
        `buffer` - Session output buffer
        `outputStream` - Output stream
        `chunkSizeHint` - minimal chunk size hint

  - ### Method Detail

    - #### finish

          public void finish()
                      throws IOException

      Must be called to ensure the internal cache is flushed and the closing chunk is written.
      Throws:
        `IOException` - in case of an I/O error

    - #### write

          public void write(int b)
                     throws IOException

      Specified by:
        `write` in class `OutputStream`
      Throws:
        `IOException`

    - #### write

          public void write(byte[] b)
                     throws IOException

      Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.
      Overrides:
        `write` in class `OutputStream`
      Throws:
        `IOException`

    - #### write

          public void write(byte[] src,
                   int off,
                   int len)
                     throws IOException

      Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.
      Overrides:
        `write` in class `OutputStream`
      Throws:
        `IOException`

    - #### flush

          public void flush()
                     throws IOException

      Flushes the content buffer and the underlying stream.
      Specified by:
        `flush` in interface `Flushable`
      Overrides:
        `flush` in class `OutputStream`
      Throws:
        `IOException`

    - #### close

          public void close()
                     throws IOException

      Finishes writing to the underlying stream, but does NOT close the underlying stream.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Overrides:
        `close` in class `OutputStream`
      Throws:
        `IOException`

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