Class ChunkedOutputStream

java.lang.Object
java.io.OutputStream
org.apache.http.impl.io.ChunkedOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class ChunkedOutputStream
extends OutputStream
Implements chunked transfer coding. See RFC 2616, section 3.6.1. Writes are buffered to an internal buffer (2048 default size).
Since:
4.0
Author:
Mohammad Rezaei (Goldman, Sachs & Co.), Oleg Kalnichevski
  • Constructor Summary

    Constructors
    Constructor Description
    ChunkedOutputStream​(SessionOutputBuffer out)
    Wraps a session output buffer and chunks the output.
    ChunkedOutputStream​(SessionOutputBuffer out, int bufferSize)
    Wraps a session output buffer and chunks the output.
  • Method Summary

    Modifier and Type Method 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.
    protected void flushCache()
    Writes the cache out onto the underlying stream
    protected void flushCacheWithAppend​(byte[] bufferToAppend, int off, int len)
    Writes the cache and bufferToAppend to the underlying stream as one large chunk
    void write​(byte[] b)
    Writes the array.
    void write​(byte[] src, int off, int len)
    Writes count bytes from the byte array buffer starting at position offset to this stream.
    void write​(int b)
    Writes a single byte to this stream.
    protected void writeClosingChunk()  

    Methods inherited from class java.lang.Object

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

    • ChunkedOutputStream

      public ChunkedOutputStream​(SessionOutputBuffer out, int bufferSize) throws IOException
      Wraps a session output buffer and chunks the output.
      Parameters:
      out - the session output buffer to wrap
      bufferSize - minimum chunk size (excluding last chunk)
      Throws:
      IOException
    • ChunkedOutputStream

      public ChunkedOutputStream​(SessionOutputBuffer out) throws IOException
      Wraps a session output buffer and chunks the output. The default buffer size of 2048 was chosen because the chunk overhead is less than 0.5%
      Parameters:
      out - the output buffer to wrap
      Throws:
      IOException
  • Method Details

    • flushCache

      protected void flushCache() throws IOException
      Writes the cache out onto the underlying stream
      Throws:
      IOException
    • flushCacheWithAppend

      protected void flushCacheWithAppend​(byte[] bufferToAppend, int off, int len) throws IOException
      Writes the cache and bufferToAppend to the underlying stream as one large chunk
      Parameters:
      bufferToAppend -
      off -
      len -
      Throws:
      IOException
    • writeClosingChunk

      protected void writeClosingChunk() throws IOException
      Throws:
      IOException
    • finish

      public void finish() throws IOException
      Must be called to ensure the internal cache is flushed and the closing chunk is written.
      Throws:
      IOException
    • write

      public void write​(int b) throws IOException
      Description copied from class: OutputStream
      Writes a single byte to this stream. Only the least significant byte of the integer oneByte is written to the stream.
      Specified by:
      write in class OutputStream
      Parameters:
      b - the byte to be written.
      Throws:
      IOException - if an error occurs while writing to this stream.
    • 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
      Parameters:
      b -
      Throws:
      IOException
    • write

      public void write​(byte[] src, int off, int len) throws IOException
      Description copied from class: OutputStream
      Writes count bytes from the byte array buffer starting at position offset to this stream.
      Overrides:
      write in class OutputStream
      Parameters:
      src - the buffer to be written.
      off - the start position in buffer from where to get bytes.
      len - the number of bytes from buffer to write to this stream.
      Throws:
      IOException - if an error occurs while writing to this stream.
    • 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 AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException