FileDocCategorySizeDatePackage
InputStreamEntity.javaAPI DocAndroid 1.5 API3895Wed May 06 22:41:10 BST 2009org.apache.http.entity

InputStreamEntity

public class InputStreamEntity extends AbstractHttpEntity
A streamed entity obtaining content from an {@link InputStream InputStream}.
author
Oleg Kalnichevski
version
$Revision: 617591 $
since
4.0

Fields Summary
private static final int
BUFFER_SIZE
private final InputStream
content
private final long
length
private boolean
consumed
Constructors Summary
public InputStreamEntity(InputStream instream, long length)


          
        super();        
        if (instream == null) {
            throw new IllegalArgumentException("Source input stream may not be null");
        }
        this.content = instream;
        this.length = length;
    
Methods Summary
public voidconsumeContent()

        this.consumed = true;
        // If the input stream is from a connection, closing it will read to
        // the end of the content. Otherwise, we don't care what it does.
        this.content.close();
    
public java.io.InputStreamgetContent()

        return this.content;
    
public longgetContentLength()

        return this.length;
    
public booleanisRepeatable()

        return false;
    
public booleanisStreaming()

        return !this.consumed;
    
public voidwriteTo(java.io.OutputStream outstream)

        if (outstream == null) {
            throw new IllegalArgumentException("Output stream may not be null");
        }
        InputStream instream = this.content;
        byte[] buffer = new byte[BUFFER_SIZE];
        int l;
        if (this.length < 0) {
            // consume until EOF
            while ((l = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, l);
            }
        } else {
            // consume no more than length
            long remaining = this.length;
            while (remaining > 0) {
                l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
                if (l == -1) {
                    break;
                }
                outstream.write(buffer, 0, l);
                remaining -= l;
            }
        }
        this.consumed = true;