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

BasicHttpEntity

public class BasicHttpEntity extends AbstractHttpEntity
A generic streamed entity being received on a connection.
author
Oleg Kalnichevski
version
$Revision: 496070 $
since
4.0

Fields Summary
private InputStream
content
private boolean
contentObtained
private long
length
Constructors Summary
public BasicHttpEntity()
Creates a new basic entity. The content is initially missing, the content length is set to a negative number.

        super();
        this.length = -1;
    
Methods Summary
public voidconsumeContent()

        if (content != null) {
            content.close(); // reads to the end of the entity
        }
    
public java.io.InputStreamgetContent()
Obtains the content, once only.

return
the content, if this is the first call to this method since {@link #setContent setContent} has been called
throws
IllegalStateException if the content has been obtained before, or has not yet been provided

        if (this.content == null) {
            throw new IllegalStateException("Content has not been provided");
        }
        if (this.contentObtained) {
            throw new IllegalStateException("Content has been consumed");
        }
        this.contentObtained = true;
        return this.content;

    
public longgetContentLength()

        return this.length;
    
public booleanisRepeatable()
Tells that this entity is not repeatable.

return
false

        return false;
    
public booleanisStreaming()

        return !this.contentObtained && this.content != null;
    
public voidsetContent(java.io.InputStream instream)
Specifies the content.

param
instream the stream to return with the next call to {@link #getContent getContent}

        this.content = instream;
        this.contentObtained = false; 
    
public voidsetContentLength(long len)
Specifies the length of the content.

param
len the number of bytes in the content, or a negative number to indicate an unknown length

        this.length = len;
    
public voidwriteTo(java.io.OutputStream outstream)

        if (outstream == null) {
            throw new IllegalArgumentException("Output stream may not be null");
        }
        InputStream instream = getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
            outstream.write(tmp, 0, l);
        }