001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.methods.multipart;
032:
033: import java.io.ByteArrayInputStream;
034: import java.io.File;
035: import java.io.FileInputStream;
036: import java.io.FileNotFoundException;
037: import java.io.IOException;
038: import java.io.InputStream;
039:
040: /**
041: * A PartSource that reads from a File.
042: *
043: * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
044: * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
045: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
046: *
047: * @since 2.0
048: */
049: public class FilePartSource implements PartSource {
050:
051: /** File part file. */
052: private File file = null;
053:
054: /** File part file name. */
055: private String fileName = null;
056:
057: /**
058: * Constructor for FilePartSource.
059: *
060: * @param file the FilePart source File.
061: *
062: * @throws FileNotFoundException if the file does not exist or
063: * cannot be read
064: */
065: public FilePartSource(File file) throws FileNotFoundException {
066: this .file = file;
067: if (file != null) {
068: if (!file.isFile()) {
069: throw new FileNotFoundException(
070: "File is not a normal file.");
071: }
072: if (!file.canRead()) {
073: throw new FileNotFoundException("File is not readable.");
074: }
075: this .fileName = file.getName();
076: }
077: }
078:
079: /**
080: * Constructor for FilePartSource.
081: *
082: * @param fileName the file name of the FilePart
083: * @param file the source File for the FilePart
084: *
085: * @throws FileNotFoundException if the file does not exist or
086: * cannot be read
087: */
088: public FilePartSource(String fileName, File file)
089: throws FileNotFoundException {
090: this (file);
091: if (fileName != null) {
092: this .fileName = fileName;
093: }
094: }
095:
096: /**
097: * Return the length of the file
098: * @return the length of the file.
099: * @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 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 createInputStream() throws IOException {
125: if (this .file != null) {
126: return new FileInputStream(this .file);
127: } else {
128: return new ByteArrayInputStream(new byte[] {});
129: }
130: }
131:
132: }
|