1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28 public class MixedAttribute implements Attribute {
29 private Attribute attribute;
30
31 private final long limitSize;
32 protected long maxSize = DefaultHttpDataFactory.MAXSIZE;
33
34 public MixedAttribute(String name, long limitSize) {
35 this.limitSize = limitSize;
36 attribute = new MemoryAttribute(name);
37 }
38
39 public MixedAttribute(String name, String value, long limitSize) {
40 this.limitSize = limitSize;
41 if (value.length() > this.limitSize) {
42 try {
43 attribute = new DiskAttribute(name, value);
44 } catch (IOException e) {
45
46 try {
47 attribute = new MemoryAttribute(name, value);
48 } catch (IOException e1) {
49 throw new IllegalArgumentException(e);
50 }
51 }
52 } else {
53 try {
54 attribute = new MemoryAttribute(name, value);
55 } catch (IOException e) {
56 throw new IllegalArgumentException(e);
57 }
58 }
59 }
60
61 public void setMaxSize(long maxSize) {
62 this.maxSize = maxSize;
63 attribute.setMaxSize(maxSize);
64 }
65
66 public void checkSize(long newSize) throws IOException {
67 if (maxSize >= 0 && newSize > maxSize) {
68 throw new IOException("Size exceed allowed maximum capacity");
69 }
70 }
71
72 public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
73 if (attribute instanceof MemoryAttribute) {
74 checkSize(attribute.length() + buffer.readableBytes());
75 if (attribute.length() + buffer.readableBytes() > limitSize) {
76 DiskAttribute diskAttribute = new DiskAttribute(attribute
77 .getName());
78 diskAttribute.setMaxSize(maxSize);
79 if (((MemoryAttribute) attribute).getChannelBuffer() != null) {
80 diskAttribute.addContent(((MemoryAttribute) attribute)
81 .getChannelBuffer(), false);
82 }
83 attribute = diskAttribute;
84 }
85 }
86 attribute.addContent(buffer, last);
87 }
88
89 public void delete() {
90 attribute.delete();
91 }
92
93 public byte[] get() throws IOException {
94 return attribute.get();
95 }
96
97 public ChannelBuffer getChannelBuffer() throws IOException {
98 return attribute.getChannelBuffer();
99 }
100
101 public Charset getCharset() {
102 return attribute.getCharset();
103 }
104
105 public String getString() throws IOException {
106 return attribute.getString();
107 }
108
109 public String getString(Charset encoding) throws IOException {
110 return attribute.getString(encoding);
111 }
112
113 public boolean isCompleted() {
114 return attribute.isCompleted();
115 }
116
117 public boolean isInMemory() {
118 return attribute.isInMemory();
119 }
120
121 public long length() {
122 return attribute.length();
123 }
124
125 public boolean renameTo(File dest) throws IOException {
126 return attribute.renameTo(dest);
127 }
128
129 public void setCharset(Charset charset) {
130 attribute.setCharset(charset);
131 }
132
133 public void setContent(ChannelBuffer buffer) throws IOException {
134 checkSize(buffer.readableBytes());
135 if (buffer.readableBytes() > limitSize) {
136 if (attribute instanceof MemoryAttribute) {
137
138 attribute = new DiskAttribute(attribute.getName());
139 attribute.setMaxSize(maxSize);
140 }
141 }
142 attribute.setContent(buffer);
143 }
144
145 public void setContent(File file) throws IOException {
146 checkSize(file.length());
147 if (file.length() > limitSize) {
148 if (attribute instanceof MemoryAttribute) {
149
150 attribute = new DiskAttribute(attribute.getName());
151 attribute.setMaxSize(maxSize);
152 }
153 }
154 attribute.setContent(file);
155 }
156
157 public void setContent(InputStream inputStream) throws IOException {
158 if (attribute instanceof MemoryAttribute) {
159
160 attribute = new DiskAttribute(attribute.getName());
161 attribute.setMaxSize(maxSize);
162 }
163 attribute.setContent(inputStream);
164 }
165
166 public HttpDataType getHttpDataType() {
167 return attribute.getHttpDataType();
168 }
169
170 public String getName() {
171 return attribute.getName();
172 }
173
174 public int compareTo(InterfaceHttpData o) {
175 return attribute.compareTo(o);
176 }
177
178 @Override
179 public String toString() {
180 return "Mixed: " + attribute.toString();
181 }
182
183 public String getValue() throws IOException {
184 return attribute.getValue();
185 }
186
187 public void setValue(String value) throws IOException {
188 if (value != null) {
189 checkSize(value.getBytes().length);
190 }
191 attribute.setValue(value);
192 }
193
194 public ChannelBuffer getChunk(int length) throws IOException {
195 return attribute.getChunk(length);
196 }
197
198 public File getFile() throws IOException {
199 return attribute.getFile();
200 }
201
202 }