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 MixedAttribute implements Attribute {
34      private Attribute attribute = null;
35  
36      private long limitSize = 0;
37  
38      public MixedAttribute(String name,
39              long limitSize) throws NullPointerException,
40              IllegalArgumentException {
41          this.limitSize = limitSize;
42          attribute = new MemoryAttribute(name);
43      }
44  
45      public MixedAttribute(String name, String value,
46              long limitSize) throws NullPointerException,
47              IllegalArgumentException {
48          this.limitSize = limitSize;
49          if (value.length() > this.limitSize) {
50              try {
51                  attribute = new DiskAttribute(name, value);
52              } catch (IOException e) {
53                  // revert to Memory mode
54                  try {
55                      attribute = new MemoryAttribute(name, value);
56                  } catch (IOException e1) {
57                      throw new IllegalArgumentException(e);
58                  }
59              }
60          } else {
61              try {
62                  attribute = new MemoryAttribute(name, value);
63              } catch (IOException e) {
64                  throw new IllegalArgumentException(e);
65              }
66          }
67      }
68  
69      @Override
70      public void addContent(ChannelBuffer buffer, boolean last)
71              throws IOException {
72          if (attribute instanceof MemoryAttribute) {
73              if (attribute.length() + buffer.readableBytes() > limitSize) {
74                  DiskAttribute diskAttribute = new DiskAttribute(attribute
75                          .getName());
76                  if (((MemoryAttribute) attribute).getChannelBuffer() != null) {
77                      diskAttribute.addContent(((MemoryAttribute) attribute)
78                          .getChannelBuffer(), false);
79                  }
80                  attribute = diskAttribute;
81              }
82          }
83          attribute.addContent(buffer, last);
84      }
85  
86      @Override
87      public void delete() {
88          attribute.delete();
89      }
90  
91      @Override
92      public byte[] get() throws IOException {
93          return attribute.get();
94      }
95  
96      @Override
97      public ChannelBuffer getChannelBuffer() throws IOException {
98          return attribute.getChannelBuffer();
99      }
100 
101     @Override
102     public Charset getCharset() {
103         return attribute.getCharset();
104     }
105 
106     @Override
107     public String getString() throws IOException {
108         return attribute.getString();
109     }
110 
111     @Override
112     public String getString(Charset encoding) throws IOException {
113         return attribute.getString(encoding);
114     }
115 
116     @Override
117     public boolean isCompleted() {
118         return attribute.isCompleted();
119     }
120 
121     @Override
122     public boolean isInMemory() {
123         return attribute.isInMemory();
124     }
125 
126     @Override
127     public long length() {
128         return attribute.length();
129     }
130 
131     @Override
132     public boolean renameTo(File dest) throws IOException {
133         return attribute.renameTo(dest);
134     }
135 
136     @Override
137     public void setCharset(Charset charset) {
138         attribute.setCharset(charset);
139     }
140 
141     @Override
142     public void setContent(ChannelBuffer buffer) throws IOException {
143         if (buffer.readableBytes() > limitSize) {
144             if (attribute instanceof MemoryAttribute) {
145                 // change to Disk
146                 DiskAttribute diskAttribute = new DiskAttribute(attribute
147                         .getName());
148                 attribute = diskAttribute;
149             }
150         }
151         attribute.setContent(buffer);
152     }
153 
154     @Override
155     public void setContent(File file) throws IOException {
156         if (file.length() > limitSize) {
157             if (attribute instanceof MemoryAttribute) {
158                 // change to Disk
159                 DiskAttribute diskAttribute = new DiskAttribute(attribute
160                         .getName());
161                 attribute = diskAttribute;
162             }
163         }
164         attribute.setContent(file);
165     }
166 
167     @Override
168     public void setContent(InputStream inputStream) throws IOException {
169         if (attribute instanceof MemoryAttribute) {
170             // change to Disk even if we don't know the size
171             DiskAttribute diskAttribute = new DiskAttribute(attribute
172                     .getName());
173             attribute = diskAttribute;
174         }
175         attribute.setContent(inputStream);
176     }
177 
178     @Override
179     public HttpDataType getHttpDataType() {
180         return attribute.getHttpDataType();
181     }
182 
183     @Override
184     public String getName() {
185         return attribute.getName();
186     }
187 
188     @Override
189     public int compareTo(InterfaceHttpData o) {
190         return attribute.compareTo(o);
191     }
192 
193     @Override
194     public String toString() {
195         return "Mixed: " + attribute.toString();
196     }
197 
198     @Override
199     public String getValue() throws IOException {
200         return attribute.getValue();
201     }
202 
203     @Override
204     public void setValue(String value) throws IOException {
205         attribute.setValue(value);
206     }
207 
208     @Override
209     public ChannelBuffer getChunk(int length) throws IOException {
210         return attribute.getChunk(length);
211     }
212 
213     @Override
214     public File getFile() throws IOException {
215         return attribute.getFile();
216     }
217 
218 }