001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.servlet.multipart;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.util.Map;
024:
025: /**
026: * This class represents a file part parsed from a http post stream.
027: *
028: * @author <a href="mailto:j.tervoorde@home.nl">Jeroen ter Voorde</a>
029: * @version CVS $Id: PartOnDisk.java 433543 2006-08-22 06:22:54Z crossley $
030: */
031: public class PartOnDisk extends Part {
032:
033: /** Field file */
034: private File file = null;
035: private int size;
036:
037: /**
038: * Constructor PartOnDisk
039: *
040: * @param headers
041: * @param file
042: */
043: public PartOnDisk(Map headers, File file) {
044: super (headers);
045: this .file = file;
046:
047: // Ensure the file will be deleted when we exit the JVM
048: this .file.deleteOnExit();
049:
050: this .size = (int) file.length();
051: }
052:
053: /**
054: * Returns the file name
055: */
056: public String getFileName() {
057: return file.getName();
058: }
059:
060: /**
061: * Returns the file size in bytes
062: */
063: public int getSize() {
064: return this .size;
065: }
066:
067: /**
068: * Returns the file
069: */
070: public File getFile() {
071: return file;
072: }
073:
074: /**
075: * Returns a (ByteArray)InputStream containing the file data
076: *
077: * @throws IOException
078: */
079: public InputStream getInputStream() throws IOException {
080: if (this .file != null) {
081: return new FileInputStream(file);
082: } else {
083: throw new IllegalStateException(
084: "This part has already been disposed.");
085: }
086: }
087:
088: /**
089: * Returns the filename
090: */
091: public String toString() {
092: return file.getPath();
093: }
094:
095: /**
096: * Delete the underlying file.
097: */
098: public void dispose() {
099: if (this .file != null) {
100: this .file.delete();
101: this .file = null;
102: }
103: }
104:
105: /**
106: * Ensures the underlying file has been deleted
107: */
108: public void finalize() throws Throwable {
109: // Ensure the file has been deleted
110: dispose();
111: super.finalize();
112: }
113: }
|