FileDocCategorySizeDatePackage
Part.javaAPI DocAndroid 1.5 API14887Wed May 06 22:41:56 BST 2009com.android.internal.http.multipart

Part

public abstract class Part extends Object
Abstract class for one Part of a multipart post object.
author
Matthew Albright
author
Jeff Dever
author
Adrian Sutton
author
Mike Bowler
author
Oleg Kalnichevski
since
2.0

Fields Summary
private static final Log
LOG
Log object for this class.
protected static final String
BOUNDARY
The boundary
protected static final byte[]
BOUNDARY_BYTES
The boundary as a byte array.
private static final byte[]
DEFAULT_BOUNDARY_BYTES
The default boundary to be used if {@link #setPartBoundary(byte[])} has not been called.
protected static final String
CRLF
Carriage return/linefeed
protected static final byte[]
CRLF_BYTES
Carriage return/linefeed as a byte array
protected static final String
QUOTE
Content dispostion characters
protected static final byte[]
QUOTE_BYTES
Content dispostion as a byte array
protected static final String
EXTRA
Extra characters
protected static final byte[]
EXTRA_BYTES
Extra characters as a byte array
protected static final String
CONTENT_DISPOSITION
Content dispostion characters
protected static final byte[]
CONTENT_DISPOSITION_BYTES
Content dispostion as a byte array
protected static final String
CONTENT_TYPE
Content type header
protected static final byte[]
CONTENT_TYPE_BYTES
Content type header as a byte array
protected static final String
CHARSET
Content charset
protected static final byte[]
CHARSET_BYTES
Content charset as a byte array
protected static final String
CONTENT_TRANSFER_ENCODING
Content type header
protected static final byte[]
CONTENT_TRANSFER_ENCODING_BYTES
Content type header as a byte array
private byte[]
boundaryBytes
The ASCII bytes to use as the multipart boundary.
Constructors Summary
Methods Summary
public static java.lang.StringgetBoundary()
Return the boundary string.

return
the boundary string
deprecated
uses a constant string. Rather use {@link #getPartBoundary}


                          
        
        return BOUNDARY;
    
public abstract java.lang.StringgetCharSet()
Return the character encoding of this part.

return
the character encoding, or null to exclude the character encoding header

public abstract java.lang.StringgetContentType()
Returns the content type of this part.

return
the content type, or null to exclude the content type header

public static longgetLengthOfParts(com.android.internal.http.multipart.Part[] parts)
Return the total sum of all parts and that of the last boundary

param
parts The parts.
return
The total length
throws
IOException If an I/O error occurs while writing the parts.

        return getLengthOfParts(parts, DEFAULT_BOUNDARY_BYTES);
    
public static longgetLengthOfParts(com.android.internal.http.multipart.Part[] parts, byte[] partBoundary)
Gets the length of the multipart message including the given parts.

param
parts The parts.
param
partBoundary The ASCII bytes to use as the part boundary.
return
The total length
throws
IOException If an I/O error occurs while writing the parts.
since
3.0

        LOG.trace("getLengthOfParts(Parts[])");
        if (parts == null) {
            throw new IllegalArgumentException("Parts may not be null"); 
        }
        long total = 0;
        for (int i = 0; i < parts.length; i++) {
            // set the part boundary before we calculate the part's length
            parts[i].setPartBoundary(partBoundary);
            long l = parts[i].length();
            if (l < 0) {
                return -1;
            }
            total += l;
        }
        total += EXTRA_BYTES.length;
        total += partBoundary.length;
        total += EXTRA_BYTES.length;
        total += CRLF_BYTES.length;
        return total;
    
public abstract java.lang.StringgetName()
Return the name of this part.

return
The name.

protected byte[]getPartBoundary()
Gets the part boundary to be used.

return
the part boundary as an array of bytes.
since
3.0

        if (boundaryBytes == null) {
            // custom boundary bytes have not been set, use the default.
            return DEFAULT_BOUNDARY_BYTES;
        } else {
            return boundaryBytes;            
        }
    
public abstract java.lang.StringgetTransferEncoding()
Return the transfer encoding of this part.

return
the transfer encoding, or null to exclude the transfer encoding header

public booleanisRepeatable()
Tests if this part can be sent more than once.

return
true if {@link #sendData(OutputStream)} can be successfully called more than once.
since
3.0

        return true;
    
public longlength()
Return the full length of all the data. If you override this method make sure to override #send(OutputStream) as well

return
long The length.
throws
IOException If an IO problem occurs

        LOG.trace("enter length()");
        if (lengthOfData() < 0) {
            return -1;
        }
        ByteArrayOutputStream overhead = new ByteArrayOutputStream();
        sendStart(overhead);
        sendDispositionHeader(overhead);
        sendContentTypeHeader(overhead);
        sendTransferEncodingHeader(overhead);
        sendEndOfHeader(overhead);
        sendEnd(overhead);
        return overhead.size() + lengthOfData();
    
protected abstract longlengthOfData()
Return the length of the main content

return
long The length.
throws
IOException If an IO problem occurs

public voidsend(java.io.OutputStream out)
Write all the data to the output stream. If you override this method make sure to override #length() as well

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter send(OutputStream out)");
        sendStart(out);
        sendDispositionHeader(out);
        sendContentTypeHeader(out);
        sendTransferEncodingHeader(out);
        sendEndOfHeader(out);
        sendData(out);
        sendEnd(out);
    
protected voidsendContentTypeHeader(java.io.OutputStream out)
Write the content type header to the specified output stream

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter sendContentTypeHeader(OutputStream out)");
        String contentType = getContentType();
        if (contentType != null) {
            out.write(CRLF_BYTES);
            out.write(CONTENT_TYPE_BYTES);
            out.write(EncodingUtils.getAsciiBytes(contentType));
            String charSet = getCharSet();
            if (charSet != null) {
                out.write(CHARSET_BYTES);
                out.write(EncodingUtils.getAsciiBytes(charSet));
            }
        }
    
protected abstract voidsendData(java.io.OutputStream out)
Write the data to the specified output stream

param
out The output stream
throws
IOException If an IO problem occurs.

protected voidsendDispositionHeader(java.io.OutputStream out)
Write the content disposition header to the specified output stream

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter sendDispositionHeader(OutputStream out)");
        out.write(CONTENT_DISPOSITION_BYTES);
        out.write(QUOTE_BYTES);
        out.write(EncodingUtils.getAsciiBytes(getName()));
        out.write(QUOTE_BYTES);
    
protected voidsendEnd(java.io.OutputStream out)
Write the end data to the output stream.

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter sendEnd(OutputStream out)");
        out.write(CRLF_BYTES);
    
protected voidsendEndOfHeader(java.io.OutputStream out)
Write the end of the header to the output stream

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter sendEndOfHeader(OutputStream out)");
        out.write(CRLF_BYTES);
        out.write(CRLF_BYTES);
    
public static voidsendParts(java.io.OutputStream out, com.android.internal.http.multipart.Part[] parts)
Write all parts and the last boundary to the specified output stream.

param
out The stream to write to.
param
parts The parts to write.
throws
IOException If an I/O error occurs while writing the parts.

        sendParts(out, parts, DEFAULT_BOUNDARY_BYTES);
    
public static voidsendParts(java.io.OutputStream out, com.android.internal.http.multipart.Part[] parts, byte[] partBoundary)
Write all parts and the last boundary to the specified output stream.

param
out The stream to write to.
param
parts The parts to write.
param
partBoundary The ASCII bytes to use as the part boundary.
throws
IOException If an I/O error occurs while writing the parts.
since
3.0

        
        if (parts == null) {
            throw new IllegalArgumentException("Parts may not be null"); 
        }
        if (partBoundary == null || partBoundary.length == 0) {
            throw new IllegalArgumentException("partBoundary may not be empty");
        }
        for (int i = 0; i < parts.length; i++) {
            // set the part boundary before the part is sent
            parts[i].setPartBoundary(partBoundary);
            parts[i].send(out);
        }
        out.write(EXTRA_BYTES);
        out.write(partBoundary);
        out.write(EXTRA_BYTES);
        out.write(CRLF_BYTES);
    
protected voidsendStart(java.io.OutputStream out)
Write the start to the specified output stream

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter sendStart(OutputStream out)");
        out.write(EXTRA_BYTES);
        out.write(getPartBoundary());
        out.write(CRLF_BYTES);
    
protected voidsendTransferEncodingHeader(java.io.OutputStream out)
Write the content transfer encoding header to the specified output stream

param
out The output stream
throws
IOException If an IO problem occurs.

        LOG.trace("enter sendTransferEncodingHeader(OutputStream out)");
        String transferEncoding = getTransferEncoding();
        if (transferEncoding != null) {
            out.write(CRLF_BYTES);
            out.write(CONTENT_TRANSFER_ENCODING_BYTES);
            out.write(EncodingUtils.getAsciiBytes(transferEncoding));
        }
    
voidsetPartBoundary(byte[] boundaryBytes)
Sets the part boundary. Only meant to be used by {@link Part#sendParts(OutputStream, Part[], byte[])} and {@link Part#getLengthOfParts(Part[], byte[])}

param
boundaryBytes An array of ASCII bytes.
since
3.0

        this.boundaryBytes = boundaryBytes;
    
public java.lang.StringtoString()
Return a string representation of this object.

return
A string representation of this object.
see
java.lang.Object#toString()

        return this.getName();