View Javadoc

1   /*
2   * Copyright 2009 Red Hat, Inc.
3   *
4   * Red Hat licenses this file to you under the Apache License, version 2.0
5   * (the "License"); you may not use this file except in compliance with the
6   * 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.http2;
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  * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
28  * @author Andy Taylor (andy.taylor@jboss.org)
29  * @author <a href="http://gleamynode.net/">Trustin Lee</a>
30  * @author <a href="http://openr66.free.fr/">Frederic Bregier</a>
31  *
32  */
33  public class MixedFileUpload implements FileUpload {
34      private FileUpload fileUpload = null;
35  
36      private long limitSize = 0;
37  
38      private long definedSize = 0;
39  
40      public MixedFileUpload(String name, String filename, String contentType,
41              String contentTransferEncoding, Charset charset, long size,
42              long limitSize) throws NullPointerException,
43              IllegalArgumentException {
44          this.limitSize = limitSize;
45          if (size > this.limitSize) {
46              fileUpload = new DiskFileUpload(name, filename, contentType,
47                      contentTransferEncoding, charset, size);
48          } else {
49              fileUpload = new MemoryFileUpload(name, filename, contentType,
50                      contentTransferEncoding, charset, size);
51          }
52          definedSize = size;
53      }
54  
55      @Override
56      public void addContent(ChannelBuffer buffer, boolean last)
57              throws IOException {
58          if (fileUpload instanceof MemoryFileUpload) {
59              if (fileUpload.length() + buffer.readableBytes() > limitSize) {
60                  DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
61                          .getName(), fileUpload.getFilename(), fileUpload
62                          .getContentType(), fileUpload
63                          .getContentTransferEncoding(), fileUpload.getCharset(),
64                          definedSize);
65                  if (((MemoryFileUpload) fileUpload).getChannelBuffer() != null){
66                      diskFileUpload.addContent(((MemoryFileUpload) fileUpload)
67                          .getChannelBuffer(), false);
68                  }
69                  fileUpload = diskFileUpload;
70              }
71          }
72          fileUpload.addContent(buffer, last);
73      }
74  
75      @Override
76      public void delete() {
77          fileUpload.delete();
78      }
79  
80      @Override
81      public byte[] get() throws IOException {
82          return fileUpload.get();
83      }
84  
85      @Override
86      public ChannelBuffer getChannelBuffer() throws IOException {
87          return fileUpload.getChannelBuffer();
88      }
89  
90      @Override
91      public Charset getCharset() {
92          return fileUpload.getCharset();
93      }
94  
95      @Override
96      public String getContentType() {
97          return fileUpload.getContentType();
98      }
99  
100     @Override
101     public String getContentTransferEncoding() {
102         return fileUpload.getContentTransferEncoding();
103     }
104 
105     @Override
106     public String getFilename() {
107         return fileUpload.getFilename();
108     }
109 
110     @Override
111     public String getString() throws IOException {
112         return fileUpload.getString();
113     }
114 
115     @Override
116     public String getString(Charset encoding) throws IOException {
117         return fileUpload.getString(encoding);
118     }
119 
120     @Override
121     public boolean isCompleted() {
122         return fileUpload.isCompleted();
123     }
124 
125     @Override
126     public boolean isInMemory() {
127         return fileUpload.isInMemory();
128     }
129 
130     @Override
131     public long length() {
132         return fileUpload.length();
133     }
134 
135     @Override
136     public boolean renameTo(File dest) throws IOException {
137         return fileUpload.renameTo(dest);
138     }
139 
140     @Override
141     public void setCharset(Charset charset) {
142         fileUpload.setCharset(charset);
143     }
144 
145     @Override
146     public void setContent(ChannelBuffer buffer) throws IOException {
147         if (buffer.readableBytes() > limitSize) {
148             if (fileUpload instanceof MemoryFileUpload) {
149                 // change to Disk
150                 DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
151                         .getName(), fileUpload.getFilename(), fileUpload
152                         .getContentType(), fileUpload
153                         .getContentTransferEncoding(), fileUpload.getCharset(),
154                         definedSize);
155                 fileUpload = diskFileUpload;
156             }
157         }
158         fileUpload.setContent(buffer);
159     }
160 
161     @Override
162     public void setContent(File file) throws IOException {
163         if (file.length() > limitSize) {
164             if (fileUpload instanceof MemoryFileUpload) {
165                 // change to Disk
166                 DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
167                         .getName(), fileUpload.getFilename(), fileUpload
168                         .getContentType(), fileUpload
169                         .getContentTransferEncoding(), fileUpload.getCharset(),
170                         definedSize);
171                 fileUpload = diskFileUpload;
172             }
173         }
174         fileUpload.setContent(file);
175     }
176 
177     @Override
178     public void setContent(InputStream inputStream) throws IOException {
179         if (fileUpload instanceof MemoryFileUpload) {
180             // change to Disk
181             DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
182                     .getName(), fileUpload.getFilename(), fileUpload
183                     .getContentType(), fileUpload
184                     .getContentTransferEncoding(), fileUpload.getCharset(),
185                     definedSize);
186             fileUpload = diskFileUpload;
187         }
188         fileUpload.setContent(inputStream);
189     }
190 
191     @Override
192     public void setContentType(String contentType) {
193         fileUpload.setContentType(contentType);
194     }
195 
196     @Override
197     public void setContentTransferEncoding(String contentTransferEncoding) {
198         fileUpload.setContentTransferEncoding(contentTransferEncoding);
199     }
200 
201     @Override
202     public void setFilename(String filename) {
203         fileUpload.setFilename(filename);
204     }
205 
206     @Override
207     public HttpDataType getHttpDataType() {
208         return fileUpload.getHttpDataType();
209     }
210 
211     @Override
212     public String getName() {
213         return fileUpload.getName();
214     }
215 
216     @Override
217     public int compareTo(InterfaceHttpData o) {
218         return fileUpload.compareTo(o);
219     }
220 
221     @Override
222     public String toString() {
223         return "Mixed: " + fileUpload.toString();
224     }
225 
226     @Override
227     public ChannelBuffer getChunk(int length) throws IOException {
228         return fileUpload.getChunk(length);
229     }
230 
231     @Override
232     public File getFile() throws IOException {
233         return fileUpload.getFile();
234     }
235 
236 }