KickJava   Java API By Example, From Geeks To Geeks.

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


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.5 $ $Date: 2004/10/13 11:50:57 $
28  * $Log: UploadedFileDefaultMemoryImpl.java,v $
29  * Revision 1.5 2004/10/13 11:50:57 matze
30  * renamed packages to org.apache
31  *
32  * Revision 1.4 2004/09/11 02:48:23 svieujot
33  * Remove empty constructor
34  *
35  * Revision 1.3 2004/09/03 12:43:57 manolito
36  * File item transient in file based UploadedFile and no more empty constructors
37  *
38  * Revision 1.2 2004/07/14 06:10:18 svieujot
39  * Remove debug messages
40  *
41  * Revision 1.1 2004/07/14 06:02:48 svieujot
42  * FileUpload : split file based and memory based implementation.
43  * Use the storage="memory|file" attribute.
44  * Default is memory because file based implementation fails to serialize.
45  */

46 public class UploadedFileDefaultMemoryImpl extends UploadedFileDefaultImplBase
47 {
48
49     private byte[] bytes;
50
51
52     public UploadedFileDefaultMemoryImpl(FileItem fileItem) throws IOException JavaDoc
53     {
54         super(fileItem.getName(), fileItem.getContentType());
55         int sizeInBytes = (int)fileItem.getSize();
56         bytes = new byte[sizeInBytes];
57         fileItem.getInputStream().read(bytes);
58
59         /*
60         TODO/manolito: what was the reason for this if?
61         if (bytes.length != 0) {
62             _name = fileItem.getName();
63             _contentType = fileItem.getContentType();
64         }
65         */

66     }
67
68
69     /**
70      * Answer the uploaded file contents.
71      *
72      * @return file contents
73      */

74     public byte[] getBytes()
75     {
76         return bytes;
77     }
78
79
80     /**
81      * Answer the uploaded file contents input stream
82      *
83      * @return
84      * @throws IOException
85      */

86     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
87     {
88         return new ByteArrayInputStream JavaDoc( bytes );
89     }
90
91
92     /**
93      * Answer the size of this file.
94      * @return
95      */

96     public long getSize() {
97         if( bytes == null )
98             return 0;
99         return bytes.length;
100     }
101 }
102
Popular Tags