KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > servlet > multipart > PartOnDisk


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.servlet.multipart;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * This class represents a file part parsed from a http post stream.
26  *
27  * @author <a HREF="mailto:j.tervoorde@home.nl">Jeroen ter Voorde</a>
28  * @version CVS $Id: PartOnDisk.java 280876 2005-09-14 15:44:29Z sylvain $
29  */

30 public class PartOnDisk extends Part {
31
32     /** Field file */
33     private File JavaDoc file = null;
34     private int size;
35
36     /**
37      * Constructor PartOnDisk
38      *
39      * @param headers
40      * @param file
41      */

42     protected PartOnDisk(Map JavaDoc headers, File JavaDoc file) {
43         super(headers);
44         this.file = file;
45         
46         // Ensure the file will be deleted when we exit the JVM
47
this.file.deleteOnExit();
48         
49         this.size = (int) file.length();
50     }
51
52     /**
53      * Returns the file name
54      */

55     public String JavaDoc getFileName() {
56         return file.getName();
57     }
58
59     /**
60      * Returns the file size in bytes
61      */

62     public int getSize() {
63         return this.size;
64     }
65
66     /**
67      * Returns the file
68      */

69     public File JavaDoc getFile() {
70         return file;
71     }
72
73     /**
74      * Returns a (ByteArray)InputStream containing the file data
75      *
76      * @throws Exception
77      */

78     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
79         if (this.file != null) {
80             return new FileInputStream JavaDoc(file);
81         } else {
82             throw new IllegalStateException JavaDoc("This part has already been disposed.");
83         }
84     }
85
86     /**
87      * Returns the filename
88      */

89     public String JavaDoc toString() {
90         return file.getPath();
91     }
92     
93     /**
94      * Delete the underlying file.
95      */

96     public void dispose() {
97         if (this.file != null) {
98             this.file.delete();
99             this.file = null;
100         }
101     }
102     
103     /**
104      * Ensures the underlying file has been deleted
105      */

106     public void finalize() throws Throwable JavaDoc {
107         // Ensure the file has been deleted
108
dispose();
109         
110         super.finalize();
111     }
112 }
113
Popular Tags