• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePartSource.java,v 1.10 2004/04/18 23:51:37 jsdever Exp $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
5  *
6  * ====================================================================
7  *
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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  * ====================================================================
23  *
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/>.
28  *
29  */
30 
31 package com.android.internal.http.multipart;
32 
33 import java.io.ByteArrayInputStream;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.io.InputStream;
39 
40 /**
41  * A PartSource that reads from a File.
42  *
43  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
44  * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
45  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
46  *
47  * @since 2.0
48  */
49 public class FilePartSource implements PartSource {
50 
51     /** File part file. */
52     private File file = null;
53 
54     /** File part file name. */
55     private String fileName = null;
56 
57     /**
58      * Constructor for FilePartSource.
59      *
60      * @param file the FilePart source File.
61      *
62      * @throws FileNotFoundException if the file does not exist or
63      * cannot be read
64      */
FilePartSource(File file)65     public FilePartSource(File file) throws FileNotFoundException {
66         this.file = file;
67         if (file != null) {
68             if (!file.isFile()) {
69                 throw new FileNotFoundException("File is not a normal file.");
70             }
71             if (!file.canRead()) {
72                 throw new FileNotFoundException("File is not readable.");
73             }
74             this.fileName = file.getName();
75         }
76     }
77 
78     /**
79      * Constructor for FilePartSource.
80      *
81      * @param fileName the file name of the FilePart
82      * @param file the source File for the FilePart
83      *
84      * @throws FileNotFoundException if the file does not exist or
85      * cannot be read
86      */
FilePartSource(String fileName, File file)87     public FilePartSource(String fileName, File file)
88       throws FileNotFoundException {
89         this(file);
90         if (fileName != null) {
91             this.fileName = fileName;
92         }
93     }
94 
95     /**
96      * Return the length of the file
97      * @return the length of the file.
98      * @see PartSource#getLength()
99      */
getLength()100     public long getLength() {
101         if (this.file != null) {
102             return this.file.length();
103         } else {
104             return 0;
105         }
106     }
107 
108     /**
109      * Return the current filename
110      * @return the filename.
111      * @see PartSource#getFileName()
112      */
getFileName()113     public String getFileName() {
114         return (fileName == null) ? "noname" : fileName;
115     }
116 
117     /**
118      * Return a new {@link FileInputStream} for the current filename.
119      * @return the new input stream.
120      * @throws IOException If an IO problem occurs.
121      * @see PartSource#createInputStream()
122      */
createInputStream()123     public InputStream createInputStream() throws IOException {
124         if (this.file != null) {
125             return new FileInputStream(this.file);
126         } else {
127             return new ByteArrayInputStream(new byte[] {});
128         }
129     }
130 
131 }
132