FileDocCategorySizeDatePackage
ChunkedOutputStream.javaAPI DocAndroid 1.5 API6513Wed May 06 22:41:10 BST 2009org.apache.http.impl.io

ChunkedOutputStream

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).
author
Mohammad Rezaei (Goldman, Sachs & Co.)
author
Oleg Kalnichevski
since
4.0

Fields Summary
private final SessionOutputBuffer
out
private byte[]
cache
private int
cachePosition
private boolean
wroteLastChunk
private boolean
closed
True if the stream is closed.
Constructors Summary
public ChunkedOutputStream(SessionOutputBuffer out, int bufferSize)
Wraps a session output buffer and chunks the output.

param
out the session output buffer to wrap
param
bufferSize minimum chunk size (excluding last chunk)
throws
IOException

    
    // ----------------------------------------------------------- Constructors
                                    
         
              
        super();
        this.cache = new byte[bufferSize];
        this.out = out;
    
public ChunkedOutputStream(SessionOutputBuffer out)
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%

param
out the output buffer to wrap
throws
IOException

        this(out, 2048);
    
Methods Summary
public voidclose()
Finishes writing to the underlying stream, but does NOT close the underlying stream.

throws
IOException

        if (!this.closed) {
            this.closed = true;
            finish();
            this.out.flush();
        }
    
public voidfinish()
Must be called to ensure the internal cache is flushed and the closing chunk is written.

throws
IOException

        if (!this.wroteLastChunk) {
            flushCache();
            writeClosingChunk();
            this.wroteLastChunk = true;
        }
    
public voidflush()
Flushes the content buffer and the underlying stream.

throws
IOException

        flushCache();
        this.out.flush();
    
protected voidflushCache()
Writes the cache out onto the underlying stream

throws
IOException

        if (this.cachePosition > 0) {
            this.out.writeLine(Integer.toHexString(this.cachePosition));
            this.out.write(this.cache, 0, this.cachePosition);
            this.out.writeLine("");
            this.cachePosition = 0;
        }
    
protected voidflushCacheWithAppend(byte[] bufferToAppend, int off, int len)
Writes the cache and bufferToAppend to the underlying stream as one large chunk

param
bufferToAppend
param
off
param
len
throws
IOException

        this.out.writeLine(Integer.toHexString(this.cachePosition + len));
        this.out.write(this.cache, 0, this.cachePosition);
        this.out.write(bufferToAppend, off, len);
        this.out.writeLine("");
        this.cachePosition = 0;
    
public voidwrite(int b)

        if (this.closed) {
            throw new IOException("Attempted write to closed stream.");
        }
        this.cache[this.cachePosition] = (byte) b;
        this.cachePosition++;
        if (this.cachePosition == this.cache.length) flushCache();
    
public voidwrite(byte[] b)
Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.

param
b
throws
IOException

        write(b, 0, b.length);
    
public voidwrite(byte[] src, int off, int len)

        if (this.closed) {
            throw new IOException("Attempted write to closed stream.");
        }
        if (len >= this.cache.length - this.cachePosition) {
            flushCacheWithAppend(src, off, len);
        } else {
            System.arraycopy(src, off, cache, this.cachePosition, len);
            this.cachePosition += len;
        }
    
protected voidwriteClosingChunk()

        // Write the final chunk.
        this.out.writeLine("0");
        this.out.writeLine("");