Class ChunkedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class ChunkedOutputStream
    extends java.io.OutputStream
    Implements HTTP chunking support. Writes are buffered to an internal buffer (2048 default size). Chunks are guaranteed to be at least as large as the buffer size (except for the last chunk).
    • Constructor Summary

      Constructors 
      Constructor Description
      ChunkedOutputStream​(java.io.OutputStream stream)
      Wraps a stream and chunks the output.
      ChunkedOutputStream​(java.io.OutputStream stream, int bufferSize)
      Wraps a stream and chunks the output.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      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 underlying stream, but leaves the internal buffer alone.
      void write​(byte[] b)
      Writes the array.
      void write​(byte[] src, int off, int len)  
      void write​(int b)
      Write the specified byte to our output stream.
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ChunkedOutputStream

        public ChunkedOutputStream​(java.io.OutputStream stream,
                                   int bufferSize)
                            throws java.io.IOException
        Wraps a stream and chunks the output.
        Parameters:
        stream - to wrap
        bufferSize - minimum chunk size (excluding last chunk)
        Throws:
        java.io.IOException
        Since:
        3.0
      • ChunkedOutputStream

        public ChunkedOutputStream​(java.io.OutputStream stream)
                            throws java.io.IOException
        Wraps a stream and chunks the output. The default buffer size of 2048 was chosen because the chunk overhead is less than 0.5%
        Parameters:
        stream -
        Throws:
        java.io.IOException
    • Method Detail

      • finish

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

        public void write​(int b)
                   throws java.io.IOException
        Write the specified byte to our output stream. Note: Avoid this method as it will cause an inefficient single byte chunk. Use write (byte[], int, int) instead.
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        b - The byte to be written
        Throws:
        java.io.IOException - if an input/output error occurs
      • write

        public void write​(byte[] b)
                   throws java.io.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 java.io.OutputStream
        Parameters:
        b -
        Throws:
        java.io.IOException
        Since:
        3.0
      • write

        public void write​(byte[] src,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Flushes the underlying stream, but leaves the internal buffer alone.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Finishes writing to the underlying stream, but does NOT close the underlying stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException