View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.fileupload;
20  
21  import org.apache.commons.fileupload.FileItem;
22  import org.apache.commons.fileupload.disk.DiskFileItem;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.Serializable;
28  
29  
30  /**
31   * @author Sylvain Vieujot (latest modification by $Author: lu4242 $)
32   * @version $Revision: 782291 $ $Date: 2009-06-06 13:03:52 -0500 (Sat, 06 Jun 2009) $
33   */
34  public class UploadedFileDefaultMemoryImpl extends UploadedFileDefaultImplBase
35  {
36      private static final long serialVersionUID = -6006333070975059090L;
37      private byte[] bytes;
38      private StorageStrategy storageStrategy;
39      private transient FileItem fileItem = null;
40  
41      public UploadedFileDefaultMemoryImpl(final FileItem fileItem) throws IOException
42      {
43          super(fileItem.getName(), fileItem.getContentType());
44          int sizeInBytes = (int)fileItem.getSize();
45          bytes = new byte[sizeInBytes];
46          this.fileItem = fileItem;
47          fileItem.getInputStream().read(bytes);
48          this.storageStrategy = new DefaultMemoryStorageStrategy();
49      }
50      
51      private class DefaultMemoryStorageStrategy 
52          extends StorageStrategy implements Serializable
53      {
54          private static final long serialVersionUID = 3610866246514636068L;
55  
56          public void deleteFileContents()
57          {
58            // UploadedFileDefaultMemoryImpl.this.fileItem becomes null 
59            // when the parent class is serialized and deserialized.
60            // In this case, the instance contained by the original
61            // object is garbage collected, so we don't have to 
62            // worry about it.
63            if (UploadedFileDefaultMemoryImpl.this.fileItem != null)
64            {
65                UploadedFileDefaultMemoryImpl.this.fileItem.delete();
66            }
67            bytes = null;
68          }
69      }
70  
71      /**
72       * Answer the uploaded file contents.
73       *
74       * @return file contents
75       */
76      public byte[] getBytes()
77      {
78          return bytes;
79      }
80  
81  
82      /**
83       * Answer the uploaded file contents input stream
84       *
85       * @return InputStream
86       * @throws IOException
87       */
88      public InputStream getInputStream() throws IOException
89      {
90          return new ByteArrayInputStream( bytes );
91      }
92  
93      /**
94       * Answer the size of this file.
95       * @return long
96       */
97      public long getSize() {
98          if( bytes == null )
99              return 0;
100         return bytes.length;
101     }
102 
103     public StorageStrategy getStorageStrategy() {
104       return storageStrategy;
105     }    
106 }