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