查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.http.multipart;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.nio.charset.Charset;
22  
23  import org.jboss.netty.buffer.ChannelBuffer;
24  
25  /**
26   * Mixed implementation using both in Memory and in File with a limit of size
27   */
28  public class MixedFileUpload implements FileUpload {
29      private FileUpload fileUpload;
30  
31      private final long limitSize;
32  
33      private final long definedSize;
34      protected long maxSize = DefaultHttpDataFactory.MAXSIZE;
35  
36      public MixedFileUpload(String name, String filename, String contentType,
37              String contentTransferEncoding, Charset charset, long size,
38              long limitSize) {
39          this.limitSize = limitSize;
40          if (size > this.limitSize) {
41              fileUpload = new DiskFileUpload(name, filename, contentType,
42                      contentTransferEncoding, charset, size);
43          } else {
44              fileUpload = new MemoryFileUpload(name, filename, contentType,
45                      contentTransferEncoding, charset, size);
46          }
47          definedSize = size;
48      }
49  
50      public void setMaxSize(long maxSize) {
51          this.maxSize = maxSize;
52          fileUpload.setMaxSize(maxSize);
53      }
54  
55      public void checkSize(long newSize) throws IOException {
56          if (maxSize >= 0 && newSize > maxSize) {
57              throw new IOException("Size exceed allowed maximum capacity");
58          }
59      }
60  
61      public void addContent(ChannelBuffer buffer, boolean last)
62              throws IOException {
63          if (fileUpload instanceof MemoryFileUpload) {
64              checkSize(fileUpload.length() + buffer.readableBytes());
65              if (fileUpload.length() + buffer.readableBytes() > limitSize) {
66                  DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
67                          .getName(), fileUpload.getFilename(), fileUpload
68                          .getContentType(), fileUpload
69                          .getContentTransferEncoding(), fileUpload.getCharset(),
70                          definedSize);
71                  diskFileUpload.setMaxSize(maxSize);
72                  if (((MemoryFileUpload) fileUpload).getChannelBuffer() != null) {
73                      diskFileUpload.addContent(((MemoryFileUpload) fileUpload)
74                          .getChannelBuffer(), false);
75                  }
76                  fileUpload = diskFileUpload;
77              }
78          }
79          fileUpload.addContent(buffer, last);
80      }
81  
82      public void delete() {
83          fileUpload.delete();
84      }
85  
86      public byte[] get() throws IOException {
87          return fileUpload.get();
88      }
89  
90      public ChannelBuffer getChannelBuffer() throws IOException {
91          return fileUpload.getChannelBuffer();
92      }
93  
94      public Charset getCharset() {
95          return fileUpload.getCharset();
96      }
97  
98      public String getContentType() {
99          return fileUpload.getContentType();
100     }
101 
102     public String getContentTransferEncoding() {
103         return fileUpload.getContentTransferEncoding();
104     }
105 
106     public String getFilename() {
107         return fileUpload.getFilename();
108     }
109 
110     public String getString() throws IOException {
111         return fileUpload.getString();
112     }
113 
114     public String getString(Charset encoding) throws IOException {
115         return fileUpload.getString(encoding);
116     }
117 
118     public boolean isCompleted() {
119         return fileUpload.isCompleted();
120     }
121 
122     public boolean isInMemory() {
123         return fileUpload.isInMemory();
124     }
125 
126     public long length() {
127         return fileUpload.length();
128     }
129 
130     public boolean renameTo(File dest) throws IOException {
131         return fileUpload.renameTo(dest);
132     }
133 
134     public void setCharset(Charset charset) {
135         fileUpload.setCharset(charset);
136     }
137 
138     public void setContent(ChannelBuffer buffer) throws IOException {
139         checkSize(buffer.readableBytes());
140         if (buffer.readableBytes() > limitSize) {
141             if (fileUpload instanceof MemoryFileUpload) {
142                 // change to Disk
143                 fileUpload = new DiskFileUpload(fileUpload
144                         .getName(), fileUpload.getFilename(), fileUpload
145                         .getContentType(), fileUpload
146                         .getContentTransferEncoding(), fileUpload.getCharset(),
147                         definedSize);
148                 fileUpload.setMaxSize(maxSize);
149             }
150         }
151         fileUpload.setContent(buffer);
152     }
153 
154     public void setContent(File file) throws IOException {
155         checkSize(file.length());
156         if (file.length() > limitSize) {
157             if (fileUpload instanceof MemoryFileUpload) {
158                 // change to Disk
159                 fileUpload = new DiskFileUpload(fileUpload
160                         .getName(), fileUpload.getFilename(), fileUpload
161                         .getContentType(), fileUpload
162                         .getContentTransferEncoding(), fileUpload.getCharset(),
163                         definedSize);
164                 fileUpload.setMaxSize(maxSize);
165             }
166         }
167         fileUpload.setContent(file);
168     }
169 
170     public void setContent(InputStream inputStream) throws IOException {
171         if (fileUpload instanceof MemoryFileUpload) {
172             // change to Disk
173             fileUpload = new DiskFileUpload(fileUpload
174                     .getName(), fileUpload.getFilename(), fileUpload
175                     .getContentType(), fileUpload
176                     .getContentTransferEncoding(), fileUpload.getCharset(),
177                     definedSize);
178             fileUpload.setMaxSize(maxSize);
179         }
180         fileUpload.setContent(inputStream);
181     }
182 
183     public void setContentType(String contentType) {
184         fileUpload.setContentType(contentType);
185     }
186 
187     public void setContentTransferEncoding(String contentTransferEncoding) {
188         fileUpload.setContentTransferEncoding(contentTransferEncoding);
189     }
190 
191     public void setFilename(String filename) {
192         fileUpload.setFilename(filename);
193     }
194 
195     public HttpDataType getHttpDataType() {
196         return fileUpload.getHttpDataType();
197     }
198 
199     public String getName() {
200         return fileUpload.getName();
201     }
202 
203     public int compareTo(InterfaceHttpData o) {
204         return fileUpload.compareTo(o);
205     }
206 
207     @Override
208     public String toString() {
209         return "Mixed: " + fileUpload.toString();
210     }
211 
212     public ChannelBuffer getChunk(int length) throws IOException {
213         return fileUpload.getChunk(length);
214     }
215 
216     public File getFile() throws IOException {
217         return fileUpload.getFile();
218     }
219 
220 }