1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.multipart;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.handler.codec.http.HttpConstants;
20
21 import java.io.IOException;
22 import java.nio.charset.Charset;
23
24
25
26
27 public class MixedAttribute extends AbstractMixedHttpData<Attribute> implements Attribute {
28 public MixedAttribute(String name, long limitSize) {
29 this(name, limitSize, HttpConstants.DEFAULT_CHARSET);
30 }
31
32 public MixedAttribute(String name, long definedSize, long limitSize) {
33 this(name, definedSize, limitSize, HttpConstants.DEFAULT_CHARSET);
34 }
35
36 public MixedAttribute(String name, long limitSize, Charset charset) {
37 this(name, limitSize, charset, DiskAttribute.baseDirectory, DiskAttribute.deleteOnExitTemporaryFile);
38 }
39
40 public MixedAttribute(String name, long limitSize, Charset charset, String baseDir, boolean deleteOnExit) {
41 this(name, 0, limitSize, charset, baseDir, deleteOnExit);
42 }
43
44 public MixedAttribute(String name, long definedSize, long limitSize, Charset charset) {
45 this(name, definedSize, limitSize, charset,
46 DiskAttribute.baseDirectory, DiskAttribute.deleteOnExitTemporaryFile);
47 }
48
49 public MixedAttribute(String name, long definedSize, long limitSize, Charset charset,
50 String baseDir, boolean deleteOnExit) {
51 super(limitSize, baseDir, deleteOnExit,
52 new MemoryAttribute(name, definedSize, charset));
53 }
54
55 public MixedAttribute(String name, String value, long limitSize) {
56 this(name, value, limitSize, HttpConstants.DEFAULT_CHARSET,
57 DiskAttribute.baseDirectory, DiskFileUpload.deleteOnExitTemporaryFile);
58 }
59
60 public MixedAttribute(String name, String value, long limitSize, Charset charset) {
61 this(name, value, limitSize, charset,
62 DiskAttribute.baseDirectory, DiskFileUpload.deleteOnExitTemporaryFile);
63 }
64
65 private static Attribute makeInitialAttributeFromValue(String name, String value, long limitSize, Charset charset,
66 String baseDir, boolean deleteOnExit) {
67 if (value.length() > limitSize) {
68 try {
69 return new DiskAttribute(name, value, charset, baseDir, deleteOnExit);
70 } catch (IOException e) {
71
72 try {
73 return new MemoryAttribute(name, value, charset);
74 } catch (IOException ignore) {
75 throw new IllegalArgumentException(e);
76 }
77 }
78 } else {
79 try {
80 return new MemoryAttribute(name, value, charset);
81 } catch (IOException e) {
82 throw new IllegalArgumentException(e);
83 }
84 }
85 }
86
87 public MixedAttribute(String name, String value, long limitSize, Charset charset,
88 String baseDir, boolean deleteOnExit) {
89 super(limitSize, baseDir, deleteOnExit,
90 makeInitialAttributeFromValue(name, value, limitSize, charset, baseDir, deleteOnExit));
91 }
92
93 @Override
94 public String getValue() throws IOException {
95 return wrapped.getValue();
96 }
97
98 @Override
99 public void setValue(String value) throws IOException {
100 wrapped.setValue(value);
101 }
102
103 @Override
104 Attribute makeDiskData() {
105 DiskAttribute diskAttribute = new DiskAttribute(getName(), definedLength(), baseDir, deleteOnExit);
106 diskAttribute.setMaxSize(getMaxSize());
107 return diskAttribute;
108 }
109
110 @Override
111 public Attribute copy() {
112
113 return super.copy();
114 }
115
116 @Override
117 public Attribute duplicate() {
118
119 return super.duplicate();
120 }
121
122 @Override
123 public Attribute replace(ByteBuf content) {
124
125 return super.replace(content);
126 }
127
128 @Override
129 public Attribute retain() {
130
131 return super.retain();
132 }
133
134 @Override
135 public Attribute retain(int increment) {
136
137 return super.retain(increment);
138 }
139
140 @Override
141 public Attribute retainedDuplicate() {
142
143 return super.retainedDuplicate();
144 }
145
146 @Override
147 public Attribute touch() {
148
149 return super.touch();
150 }
151
152 @Override
153 public Attribute touch(Object hint) {
154
155 return super.touch(hint);
156 }
157 }