org.apache.http.impl.io
Class ChunkedOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended byorg.apache.http.impl.io.ChunkedOutputStream

public class ChunkedOutputStream
extends java.io.OutputStream

Implements chunked transfer coding. See RFC 2616, section 3.6.1. 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).

Since:
4.0
Author:
Mohammad Rezaei (Goldman, Sachs & Co.), Oleg Kalnichevski

Constructor Summary
ChunkedOutputStream(HttpDataTransmitter datatransmitter)
          Wraps a data transmitter and chunks the output.
ChunkedOutputStream(HttpDataTransmitter out, int bufferSize)
          Wraps a stream and chunks the output.
 
Method Summary
 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.
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)
           
 void write(int b)
           
protected  void writeClosingChunk()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChunkedOutputStream

public ChunkedOutputStream(HttpDataTransmitter out,
                           int bufferSize)
                    throws java.io.IOException
Wraps a stream and chunks the output.

Parameters:
out - the transmitter to wrap
bufferSize - minimum chunk size (excluding last chunk)
Throws:
java.io.IOException

ChunkedOutputStream

public ChunkedOutputStream(HttpDataTransmitter datatransmitter)
                    throws java.io.IOException
Wraps a data transmitter and chunks the output. The default buffer size of 2048 was chosen because the chunk overhead is less than 0.5%

Parameters:
datatransmitter - the transmitter to wrap
Throws:
java.io.IOException
Method Detail

flushCache

protected void flushCache()
                   throws java.io.IOException
Writes the cache out onto the underlying stream

Throws:
java.io.IOException

flushCacheWithAppend

protected void flushCacheWithAppend(byte[] bufferToAppend,
                                    int off,
                                    int len)
                             throws java.io.IOException
Writes the cache and bufferToAppend to the underlying stream as one large chunk

Parameters:
bufferToAppend -
off -
len -
Throws:
java.io.IOException

writeClosingChunk

protected void writeClosingChunk()
                          throws java.io.IOException
Throws:
java.io.IOException

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

write

public void write(int b)
           throws java.io.IOException
Throws:
java.io.IOException

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.

Parameters:
b -
Throws:
java.io.IOException

write

public void write(byte[] src,
                  int off,
                  int len)
           throws java.io.IOException
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flushes the underlying stream, but leaves the internal buffer alone.

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.

Throws:
java.io.IOException


Copyright © 2005-2007 Apache Software Foundation. All Rights Reserved.