1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28
29
30
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
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
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
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
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 }