2 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v 1.19 2004/04/18 23:51:37 jsdever Exp $
4 * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
6 * ====================================================================
8 * Licensed to the Apache Software Foundation (ASF) under one or more
9 * contributor license agreements. See the NOTICE file distributed with
10 * this work for additional information regarding copyright ownership.
11 * The ASF licenses this file to You under the Apache License, Version 2.0
12 * (the "License"); you may not use this file except in compliance with
13 * the License. You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ====================================================================
24 * This software consists of voluntary contributions made by many
25 * individuals on behalf of the Apache Software Foundation. For more
26 * information on the Apache Software Foundation, please see
27 * <http://www.apache.org/>.
31 package com.android.internal.http.multipart;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import org.apache.http.util.EncodingUtils;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
43 * This class implements a part of a Multipart post object that
46 * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
47 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
48 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
49 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
50 * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
51 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
52 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
57 public class FilePart extends PartBase {
59 /** Default content encoding of file attachments. */
60 public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
62 /** Default charset of file attachments. */
63 public static final String DEFAULT_CHARSET = "ISO-8859-1";
65 /** Default transfer encoding of file attachments. */
66 public static final String DEFAULT_TRANSFER_ENCODING = "binary";
68 /** Log object for this class. */
69 private static final Log LOG = LogFactory.getLog(FilePart.class);
71 /** Attachment's file name */
72 protected static final String FILE_NAME = "; filename=";
74 /** Attachment's file name as a byte array */
75 private static final byte[] FILE_NAME_BYTES =
76 EncodingUtils.getAsciiBytes(FILE_NAME);
78 /** Source of the file part. */
79 private PartSource source;
82 * FilePart Constructor.
84 * @param name the name for this part
85 * @param partSource the source for this part
86 * @param contentType the content type for this part, if <code>null</code> the
87 * {@link #DEFAULT_CONTENT_TYPE default} is used
88 * @param charset the charset encoding for this part, if <code>null</code> the
89 * {@link #DEFAULT_CHARSET default} is used
91 public FilePart(String name, PartSource partSource, String contentType, String charset) {
95 contentType == null ? DEFAULT_CONTENT_TYPE : contentType,
96 charset == null ? "ISO-8859-1" : charset,
97 DEFAULT_TRANSFER_ENCODING
100 if (partSource == null) {
101 throw new IllegalArgumentException("Source may not be null");
103 this.source = partSource;
107 * FilePart Constructor.
109 * @param name the name for this part
110 * @param partSource the source for this part
112 public FilePart(String name, PartSource partSource) {
113 this(name, partSource, null, null);
117 * FilePart Constructor.
119 * @param name the name of the file part
120 * @param file the file to post
122 * @throws FileNotFoundException if the <i>file</i> is not a normal
123 * file or if it is not readable.
125 public FilePart(String name, File file)
126 throws FileNotFoundException {
127 this(name, new FilePartSource(file), null, null);
131 * FilePart Constructor.
133 * @param name the name of the file part
134 * @param file the file to post
135 * @param contentType the content type for this part, if <code>null</code> the
136 * {@link #DEFAULT_CONTENT_TYPE default} is used
137 * @param charset the charset encoding for this part, if <code>null</code> the
138 * {@link #DEFAULT_CHARSET default} is used
140 * @throws FileNotFoundException if the <i>file</i> is not a normal
141 * file or if it is not readable.
143 public FilePart(String name, File file, String contentType, String charset)
144 throws FileNotFoundException {
145 this(name, new FilePartSource(file), contentType, charset);
149 * FilePart Constructor.
151 * @param name the name of the file part
152 * @param fileName the file name
153 * @param file the file to post
155 * @throws FileNotFoundException if the <i>file</i> is not a normal
156 * file or if it is not readable.
158 public FilePart(String name, String fileName, File file)
159 throws FileNotFoundException {
160 this(name, new FilePartSource(fileName, file), null, null);
164 * FilePart Constructor.
166 * @param name the name of the file part
167 * @param fileName the file name
168 * @param file the file to post
169 * @param contentType the content type for this part, if <code>null</code> the
170 * {@link #DEFAULT_CONTENT_TYPE default} is used
171 * @param charset the charset encoding for this part, if <code>null</code> the
172 * {@link #DEFAULT_CHARSET default} is used
174 * @throws FileNotFoundException if the <i>file</i> is not a normal
175 * file or if it is not readable.
177 public FilePart(String name, String fileName, File file, String contentType, String charset)
178 throws FileNotFoundException {
179 this(name, new FilePartSource(fileName, file), contentType, charset);
183 * Write the disposition header to the output stream
184 * @param out The output stream
185 * @throws IOException If an IO problem occurs
186 * @see Part#sendDispositionHeader(OutputStream)
189 protected void sendDispositionHeader(OutputStream out)
191 LOG.trace("enter sendDispositionHeader(OutputStream out)");
192 super.sendDispositionHeader(out);
193 String filename = this.source.getFileName();
194 if (filename != null) {
195 out.write(FILE_NAME_BYTES);
196 out.write(QUOTE_BYTES);
197 out.write(EncodingUtils.getAsciiBytes(filename));
198 out.write(QUOTE_BYTES);
203 * Write the data in "source" to the specified stream.
204 * @param out The output stream.
205 * @throws IOException if an IO problem occurs.
206 * @see Part#sendData(OutputStream)
209 protected void sendData(OutputStream out) throws IOException {
210 LOG.trace("enter sendData(OutputStream out)");
211 if (lengthOfData() == 0) {
213 // this file contains no data, so there is nothing to send.
214 // we don't want to create a zero length buffer as this will
215 // cause an infinite loop when reading.
216 LOG.debug("No data to send.");
220 byte[] tmp = new byte[4096];
221 InputStream instream = source.createInputStream();
224 while ((len = instream.read(tmp)) >= 0) {
225 out.write(tmp, 0, len);
228 // we're done with the stream, close it
234 * Returns the source of the file part.
236 * @return The source.
238 protected PartSource getSource() {
239 LOG.trace("enter getSource()");
244 * Return the length of the data.
245 * @return The length.
246 * @see Part#lengthOfData()
249 protected long lengthOfData() {
250 LOG.trace("enter lengthOfData()");
251 return source.getLength();