View Javadoc

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 io.netty.handler.codec.http.multipart;
17  
18  import io.netty.buffer.ByteBuf;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.charset.Charset;
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  
30      private FileUpload fileUpload;
31  
32      private final long limitSize;
33  
34      private final long definedSize;
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      @Override
51      public void addContent(ByteBuf buffer, boolean last)
52              throws IOException {
53          if (fileUpload instanceof MemoryFileUpload) {
54              if (fileUpload.length() + buffer.readableBytes() > limitSize) {
55                  DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
56                          .getName(), fileUpload.getFilename(), fileUpload
57                          .getContentType(), fileUpload
58                          .getContentTransferEncoding(), fileUpload.getCharset(),
59                          definedSize);
60  
61                  ByteBuf data = fileUpload.getByteBuf();
62                  if (data != null && data.isReadable()) {
63                      diskFileUpload.addContent(data.retain(), false);
64                  }
65                  // release old upload
66                  fileUpload.release();
67  
68                  fileUpload = diskFileUpload;
69              }
70          }
71          fileUpload.addContent(buffer, last);
72      }
73  
74      @Override
75      public void delete() {
76          fileUpload.delete();
77      }
78  
79      @Override
80      public byte[] get() throws IOException {
81          return fileUpload.get();
82      }
83  
84      @Override
85      public ByteBuf getByteBuf() throws IOException {
86          return fileUpload.getByteBuf();
87      }
88  
89      @Override
90      public Charset getCharset() {
91          return fileUpload.getCharset();
92      }
93  
94      @Override
95      public String getContentType() {
96          return fileUpload.getContentType();
97      }
98  
99      @Override
100     public String getContentTransferEncoding() {
101         return fileUpload.getContentTransferEncoding();
102     }
103 
104     @Override
105     public String getFilename() {
106         return fileUpload.getFilename();
107     }
108 
109     @Override
110     public String getString() throws IOException {
111         return fileUpload.getString();
112     }
113 
114     @Override
115     public String getString(Charset encoding) throws IOException {
116         return fileUpload.getString(encoding);
117     }
118 
119     @Override
120     public boolean isCompleted() {
121         return fileUpload.isCompleted();
122     }
123 
124     @Override
125     public boolean isInMemory() {
126         return fileUpload.isInMemory();
127     }
128 
129     @Override
130     public long length() {
131         return fileUpload.length();
132     }
133 
134     @Override
135     public boolean renameTo(File dest) throws IOException {
136         return fileUpload.renameTo(dest);
137     }
138 
139     @Override
140     public void setCharset(Charset charset) {
141         fileUpload.setCharset(charset);
142     }
143 
144     @Override
145     public void setContent(ByteBuf buffer) throws IOException {
146         if (buffer.readableBytes() > limitSize) {
147             if (fileUpload instanceof MemoryFileUpload) {
148                 FileUpload memoryUpload = fileUpload;
149                 // change to Disk
150                 fileUpload = new DiskFileUpload(memoryUpload
151                         .getName(), memoryUpload.getFilename(), memoryUpload
152                         .getContentType(), memoryUpload
153                         .getContentTransferEncoding(), memoryUpload.getCharset(),
154                         definedSize);
155 
156                 // release old upload
157                 memoryUpload.release();
158             }
159         }
160         fileUpload.setContent(buffer);
161     }
162 
163     @Override
164     public void setContent(File file) throws IOException {
165         if (file.length() > limitSize) {
166             if (fileUpload instanceof MemoryFileUpload) {
167                 FileUpload memoryUpload = fileUpload;
168 
169                 // change to Disk
170                 fileUpload = new DiskFileUpload(memoryUpload
171                         .getName(), memoryUpload.getFilename(), memoryUpload
172                         .getContentType(), memoryUpload
173                         .getContentTransferEncoding(), memoryUpload.getCharset(),
174                         definedSize);
175 
176                 // release old upload
177                 memoryUpload.release();
178             }
179         }
180         fileUpload.setContent(file);
181     }
182 
183     @Override
184     public void setContent(InputStream inputStream) throws IOException {
185         if (fileUpload instanceof MemoryFileUpload) {
186             FileUpload memoryUpload = fileUpload;
187 
188             // change to Disk
189             fileUpload = new DiskFileUpload(fileUpload
190                     .getName(), fileUpload.getFilename(), fileUpload
191                     .getContentType(), fileUpload
192                     .getContentTransferEncoding(), fileUpload.getCharset(),
193                     definedSize);
194 
195             // release old upload
196             memoryUpload.release();
197         }
198         fileUpload.setContent(inputStream);
199     }
200 
201     @Override
202     public void setContentType(String contentType) {
203         fileUpload.setContentType(contentType);
204     }
205 
206     @Override
207     public void setContentTransferEncoding(String contentTransferEncoding) {
208         fileUpload.setContentTransferEncoding(contentTransferEncoding);
209     }
210 
211     @Override
212     public void setFilename(String filename) {
213         fileUpload.setFilename(filename);
214     }
215 
216     @Override
217     public HttpDataType getHttpDataType() {
218         return fileUpload.getHttpDataType();
219     }
220 
221     @Override
222     public String getName() {
223         return fileUpload.getName();
224     }
225 
226     @Override
227     public int compareTo(InterfaceHttpData o) {
228         return fileUpload.compareTo(o);
229     }
230 
231     @Override
232     public String toString() {
233         return "Mixed: " + fileUpload.toString();
234     }
235 
236     @Override
237     public ByteBuf getChunk(int length) throws IOException {
238         return fileUpload.getChunk(length);
239     }
240 
241     @Override
242     public File getFile() throws IOException {
243         return fileUpload.getFile();
244     }
245 
246     @Override
247     public FileUpload copy() {
248         return fileUpload.copy();
249     }
250 
251     @Override
252     public FileUpload duplicate() {
253         return fileUpload.duplicate();
254     }
255 
256     @Override
257     public ByteBuf content() {
258         return fileUpload.content();
259     }
260 
261     @Override
262     public int refCnt() {
263         return fileUpload.refCnt();
264     }
265 
266     @Override
267     public FileUpload retain() {
268         fileUpload.retain();
269         return this;
270     }
271 
272     @Override
273     public FileUpload retain(int increment) {
274         fileUpload.retain(increment);
275         return this;
276     }
277 
278     @Override
279     public boolean release() {
280         return fileUpload.release();
281     }
282 
283     @Override
284     public boolean release(int decrement) {
285         return fileUpload.release(decrement);
286     }
287 }