KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > fileupload > UploadedFileDefaultFileImpl


1 /*
2  * Copyright 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.myfaces.custom.fileupload;
17
18 import org.apache.commons.fileupload.FileItem;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24
25 /**
26  * @author Sylvain Vieujot (latest modification by $Author: matze $)
27  * @version $Revision: 1.4 $ $Date: 2004/10/13 11:50:57 $
28  * $Log: UploadedFileDefaultFileImpl.java,v $
29  * Revision 1.4 2004/10/13 11:50:57 matze
30  * renamed packages to org.apache
31  *
32  * Revision 1.3 2004/09/03 12:43:57 manolito
33  * File item transient in file based UploadedFile and no more empty constructors
34  *
35  * Revision 1.2 2004/07/14 06:10:18 svieujot
36  * Remove debug messages
37  *
38  * Revision 1.1 2004/07/14 06:02:48 svieujot
39  * FileUpload : split file based and memory based implementation.
40  * Use the storage="memory|file" attribute.
41  * Default is memory because file based implementation fails to serialize.
42  */

43 public class UploadedFileDefaultFileImpl extends UploadedFileDefaultImplBase
44 {
45     private transient FileItem fileItem = null;
46
47     /*
48     TODO/manolito: Do we need an empty constructor?!
49     public UploadedFileDefaultFileImpl()
50     {
51     }
52     */

53
54
55     public UploadedFileDefaultFileImpl(FileItem fileItem) throws IOException JavaDoc
56     {
57         super(fileItem.getName(), fileItem.getContentType());
58         this.fileItem = fileItem;
59     }
60
61
62     /**
63      * Answer the uploaded file contents.
64      *
65      * @return file contents
66      */

67     public byte[] getBytes() throws IOException JavaDoc
68     {
69         byte[] bytes = new byte[(int)getSize()];
70         if (fileItem != null) fileItem.getInputStream().read(bytes);
71         return bytes;
72     }
73
74
75     /**
76      * Answer the uploaded file contents input stream
77      *
78      * @return
79      * @throws IOException
80      */

81     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
82     {
83         return fileItem != null
84                ? fileItem.getInputStream()
85                : new ByteArrayInputStream JavaDoc(new byte[0]);
86     }
87
88
89     /**
90      * Answer the size of this file.
91      * @return
92      */

93     public long getSize()
94     {
95         return fileItem != null ? fileItem.getSize() : 0;
96     }
97 }
98
Popular Tags