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

FilePart

public class FilePart extends PartBase
This class implements a part of a Multipart post object that consists of a file.
author
Matthew Albright
author
Jeff Dever
author
Adrian Sutton
author
Michael Becke
author
Mark Diggory
author
Mike Bowler
author
Oleg Kalnichevski
since
2.0

Fields Summary
public static final String
DEFAULT_CONTENT_TYPE
Default content encoding of file attachments.
public static final String
DEFAULT_CHARSET
Default charset of file attachments.
public static final String
DEFAULT_TRANSFER_ENCODING
Default transfer encoding of file attachments.
private static final Log
LOG
Log object for this class.
protected static final String
FILE_NAME
Attachment's file name
private static final byte[]
FILE_NAME_BYTES
Attachment's file name as a byte array
private PartSource
source
Source of the file part.
Constructors Summary
public FilePart(String name, PartSource partSource, String contentType, String charset)
FilePart Constructor.

param
name the name for this part
param
partSource the source for this part
param
contentType the content type for this part, if null the {@link #DEFAULT_CONTENT_TYPE default} is used
param
charset the charset encoding for this part, if null the {@link #DEFAULT_CHARSET default} is used


                                                           
             
        
        super(
            name, 
            contentType == null ? DEFAULT_CONTENT_TYPE : contentType, 
            charset == null ? "ISO-8859-1" : charset, 
            DEFAULT_TRANSFER_ENCODING
        );

        if (partSource == null) {
            throw new IllegalArgumentException("Source may not be null");
        }
        this.source = partSource;
    
public FilePart(String name, PartSource partSource)
FilePart Constructor.

param
name the name for this part
param
partSource the source for this part

        this(name, partSource, null, null);
    
public FilePart(String name, File file)
FilePart Constructor.

param
name the name of the file part
param
file the file to post
throws
FileNotFoundException if the file is not a normal file or if it is not readable.

        this(name, new FilePartSource(file), null, null);
    
public FilePart(String name, File file, String contentType, String charset)
FilePart Constructor.

param
name the name of the file part
param
file the file to post
param
contentType the content type for this part, if null the {@link #DEFAULT_CONTENT_TYPE default} is used
param
charset the charset encoding for this part, if null the {@link #DEFAULT_CHARSET default} is used
throws
FileNotFoundException if the file is not a normal file or if it is not readable.

        this(name, new FilePartSource(file), contentType, charset);
    
public FilePart(String name, String fileName, File file)
FilePart Constructor.

param
name the name of the file part
param
fileName the file name
param
file the file to post
throws
FileNotFoundException if the file is not a normal file or if it is not readable.

        this(name, new FilePartSource(fileName, file), null, null);
    
public FilePart(String name, String fileName, File file, String contentType, String charset)
FilePart Constructor.

param
name the name of the file part
param
fileName the file name
param
file the file to post
param
contentType the content type for this part, if null the {@link #DEFAULT_CONTENT_TYPE default} is used
param
charset the charset encoding for this part, if null the {@link #DEFAULT_CHARSET default} is used
throws
FileNotFoundException if the file is not a normal file or if it is not readable.

        this(name, new FilePartSource(fileName, file), contentType, charset);
    
Methods Summary
protected PartSourcegetSource()
Returns the source of the file part.

return
The source.

        LOG.trace("enter getSource()");
        return this.source;
    
protected longlengthOfData()
Return the length of the data.

return
The length.
see
Part#lengthOfData()

        LOG.trace("enter lengthOfData()");
        return source.getLength();
    
protected voidsendData(java.io.OutputStream out)
Write the data in "source" to the specified stream.

param
out The output stream.
throws
IOException if an IO problem occurs.
see
Part#sendData(OutputStream)

        LOG.trace("enter sendData(OutputStream out)");
        if (lengthOfData() == 0) {
            
            // this file contains no data, so there is nothing to send.
            // we don't want to create a zero length buffer as this will
            // cause an infinite loop when reading.
            LOG.debug("No data to send.");
            return;
        }
        
        byte[] tmp = new byte[4096];
        InputStream instream = source.createInputStream();
        try {
            int len;
            while ((len = instream.read(tmp)) >= 0) {
                out.write(tmp, 0, len);
            }
        } finally {
            // we're done with the stream, close it
            instream.close();
        }
    
protected voidsendDispositionHeader(java.io.OutputStream out)
Write the disposition header to the output stream

param
out The output stream
throws
IOException If an IO problem occurs
see
Part#sendDispositionHeader(OutputStream)

        LOG.trace("enter sendDispositionHeader(OutputStream out)");
        super.sendDispositionHeader(out);
        String filename = this.source.getFileName();
        if (filename != null) {
            out.write(FILE_NAME_BYTES);
            out.write(QUOTE_BYTES);
            out.write(EncodingUtils.getAsciiBytes(filename));
            out.write(QUOTE_BYTES);
        }