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

ContentLengthOutputStream

public class ContentLengthOutputStream extends OutputStream
A stream wrapper that closes itself after a defined number of bytes.
author
Oleg Kalnichevski
version
$Revision: 560343 $
since
4.0

Fields Summary
private final SessionOutputBuffer
out
Wrapped session outbut buffer.
private final long
contentLength
The maximum number of bytes that can be written the stream. Subsequent write operations will be ignored.
private long
total
Total bytes written
private boolean
closed
True if the stream is closed.
Constructors Summary
public ContentLengthOutputStream(SessionOutputBuffer out, long contentLength)
Creates a new length limited stream

param
out The data transmitter to wrap
param
contentLength The maximum number of bytes that can be written to the stream. Subsequent write operations will be ignored.
since
4.0


                                             
          
        super();
        if (out == null) {
            throw new IllegalArgumentException("Session output buffer may not be null");
        }
        if (contentLength < 0) {
            throw new IllegalArgumentException("Content length may not be negative");
        }
        this.out = out;
        this.contentLength = contentLength;
    
Methods Summary
public voidclose()

Does not close the underlying socket output.

throws
IOException If an I/O problem occurs.

        if (!this.closed) {
            this.closed = true;
            this.out.flush();
        }
    
public voidflush()

        this.out.flush();
    
public voidwrite(byte[] b, int off, int len)

        if (this.closed) {
            throw new IOException("Attempted write to closed stream.");
        }
        if (this.total < this.contentLength) {
            long max = this.contentLength - this.total;
            if (len > max) {
                len = (int) max;
            }
            this.out.write(b, off, len);
            this.total += len;
        }
    
public voidwrite(byte[] b)

        write(b, 0, b.length);
    
public voidwrite(int b)

        if (this.closed) {
            throw new IOException("Attempted write to closed stream.");
        }
        if (this.total < this.contentLength) {
            this.out.write(b);
            this.total++;
        }