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 java.io.ByteArrayInputStream;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Serializable;
26  
27  import org.apache.commons.fileupload.FileItem;
28  import org.apache.commons.fileupload.disk.DiskFileItem;
29  
30  
31  /**
32   * @author Sylvain Vieujot (latest modification by $Author: lu4242 $)
33   * @version $Revision: 782291 $ $Date: 2009-06-06 13:03:52 -0500 (Sat, 06 Jun 2009) $
34   */
35  public class UploadedFileDefaultFileImpl extends UploadedFileDefaultImplBase
36  {
37    private static final long serialVersionUID = -6401426361519246443L;
38    private transient DiskFileItem fileItem = null;
39    private StorageStrategy storageStrategy;
40  
41      public UploadedFileDefaultFileImpl(final FileItem fileItem) throws IOException
42      {
43          super(fileItem.getName(), fileItem.getContentType());
44          this.fileItem = (DiskFileItem) fileItem;
45          storageStrategy = new DefaultDiskStorageStrategy();
46      }
47  
48      private class DefaultDiskStorageStrategy 
49          extends DiskStorageStrategy implements Serializable
50      {
51          private static final long serialVersionUID = 5191237379179109587L;
52          
53          public DefaultDiskStorageStrategy()
54          {
55          }
56  
57          public File getTempFile()
58          {
59              if (UploadedFileDefaultFileImpl.this.fileItem != null)
60              {
61                  return UploadedFileDefaultFileImpl.this.fileItem.getStoreLocation();
62              }
63              else
64              {
65                  return null;
66              }
67          }
68  
69          public void deleteFileContents()
70          {
71              // UploadedFileDefaultFileImpl.this.fileItem becomes null 
72              // when the parent class is serialized and deserialized.
73              // In this case, the instance contained by the original
74              // object is garbage collected, so we don't have to 
75              // worry about it.
76              if (UploadedFileDefaultFileImpl.this.fileItem != null)
77              {
78                  UploadedFileDefaultFileImpl.this.fileItem.delete();
79              }
80          }
81      }
82  
83      /**
84       * Answer the uploaded file contents.
85       *
86       * @return file contents
87       */
88      public byte[] getBytes() throws IOException
89      {
90          byte[] bytes = new byte[(int)getSize()];
91          if (fileItem != null) fileItem.getInputStream().read(bytes);
92          return bytes;
93      }
94  
95  
96      /**
97       * Answer the uploaded file contents input stream
98       *
99       * @return InputStream
100      * @throws IOException
101      */
102     public InputStream getInputStream() throws IOException
103     {
104         return fileItem != null
105                ? fileItem.getInputStream()
106                : new ByteArrayInputStream(new byte[0]);
107     }
108 
109 
110     /**
111      * Answer the size of this file.
112      * @return long
113      */
114     public long getSize()
115     {
116         return fileItem != null ? fileItem.getSize() : 0;
117     }
118 
119 
120     public StorageStrategy getStorageStrategy() {
121       return storageStrategy;
122     }
123 }