KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > methods > multipart > FilePartSource


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePartSource.java,v 1.8.2.1 2004/02/22 18:21:15 olegk Exp $
3  * $Revision: 1.8.2.1 $
4  * $Date: 2004/02/22 18:21:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31  
32 package org.apache.commons.httpclient.methods.multipart;
33
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40
41 /**
42  * A PartSource that reads from a File.
43  *
44  * @author <a HREF="mailto:becke@u.washington.edu">Michael Becke</a>
45  * @author <a HREF="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
46  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
47  *
48  * @since 2.0
49  */

50 public class FilePartSource implements PartSource {
51
52     /** File part file. */
53     private File JavaDoc file = null;
54
55     /** File part file name. */
56     private String JavaDoc fileName = null;
57     
58     /**
59      * Constructor for FilePartSource.
60      *
61      * @param file the FilePart source File.
62      *
63      * @throws FileNotFoundException if the file does not exist or
64      * cannot be read
65      */

66     public FilePartSource(File JavaDoc file) throws FileNotFoundException JavaDoc {
67         this.file = file;
68         if (file != null) {
69             if (!file.isFile()) {
70                 throw new FileNotFoundException JavaDoc("File is not a normal file.");
71             }
72             if (!file.canRead()) {
73                 throw new FileNotFoundException JavaDoc("File is not readable.");
74             }
75             this.fileName = file.getName();
76         }
77     }
78
79     /**
80      * Constructor for FilePartSource.
81      *
82      * @param fileName the file name of the FilePart
83      * @param file the source File for the FilePart
84      *
85      * @throws FileNotFoundException if the file does not exist or
86      * cannot be read
87      */

88     public FilePartSource(String JavaDoc fileName, File JavaDoc file)
89       throws FileNotFoundException JavaDoc {
90         this(file);
91         if (fileName != null) {
92             this.fileName = fileName;
93         }
94     }
95     
96     /**
97      * Return the length of the file
98      * @return the length of the file.
99      * @see PartSource#getLength()
100      */

101     public long getLength() {
102         if (this.file != null) {
103             return this.file.length();
104         } else {
105             return 0;
106         }
107     }
108
109     /**
110      * Return the current filename
111      * @return the filename.
112      * @see PartSource#getFileName()
113      */

114     public String JavaDoc getFileName() {
115         return (fileName == null) ? "noname" : fileName;
116     }
117
118     /**
119      * Return a new {@link FileInputStream} for the current filename.
120      * @return the new input stream.
121      * @throws IOException If an IO problem occurs.
122      * @see PartSource#createInputStream()
123      */

124     public InputStream JavaDoc createInputStream() throws IOException JavaDoc {
125         if (this.file != null) {
126             return new FileInputStream JavaDoc(this.file);
127         } else {
128             return new ByteArrayInputStream JavaDoc(new byte[] {});
129         }
130     }
131
132 }
133
Popular Tags