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.channel.ChannelException;
20 import io.netty.handler.codec.http.HttpConstants;
21
22 import java.io.IOException;
23
24 import static io.netty.buffer.Unpooled.*;
25
26
27
28
29 public class DiskAttribute extends AbstractDiskHttpData implements Attribute {
30 public static String baseDirectory;
31
32 public static boolean deleteOnExitTemporaryFile = true;
33
34 public static final String prefix = "Attr_";
35
36 public static final String postfix = ".att";
37
38
39
40
41 public DiskAttribute(String name) {
42 super(name, HttpConstants.DEFAULT_CHARSET, 0);
43 }
44
45 public DiskAttribute(String name, String value) throws IOException {
46 super(name, HttpConstants.DEFAULT_CHARSET, 0);
47 setValue(value);
48 }
49
50 @Override
51 public HttpDataType getHttpDataType() {
52 return HttpDataType.Attribute;
53 }
54
55 @Override
56 public String getValue() throws IOException {
57 byte [] bytes = get();
58 return new String(bytes, charset.name());
59 }
60
61 @Override
62 public void setValue(String value) throws IOException {
63 if (value == null) {
64 throw new NullPointerException("value");
65 }
66 byte [] bytes = value.getBytes(charset.name());
67 ByteBuf buffer = wrappedBuffer(bytes);
68 if (definedSize > 0) {
69 definedSize = buffer.readableBytes();
70 }
71 setContent(buffer);
72 }
73
74 @Override
75 public void addContent(ByteBuf buffer, boolean last) throws IOException {
76 int localsize = buffer.readableBytes();
77 if (definedSize > 0 && definedSize < size + localsize) {
78 definedSize = size + localsize;
79 }
80 super.addContent(buffer, last);
81 }
82 @Override
83 public int hashCode() {
84 return getName().hashCode();
85 }
86
87 @Override
88 public boolean equals(Object o) {
89 if (!(o instanceof Attribute)) {
90 return false;
91 }
92 Attribute attribute = (Attribute) o;
93 return getName().equalsIgnoreCase(attribute.getName());
94 }
95
96 @Override
97 public int compareTo(InterfaceHttpData o) {
98 if (!(o instanceof Attribute)) {
99 throw new ClassCastException("Cannot compare " + getHttpDataType() +
100 " with " + o.getHttpDataType());
101 }
102 return compareTo((Attribute) o);
103 }
104
105 public int compareTo(Attribute o) {
106 return getName().compareToIgnoreCase(o.getName());
107 }
108
109 @Override
110 public String toString() {
111 try {
112 return getName() + '=' + getValue();
113 } catch (IOException e) {
114 return getName() + "=IoException";
115 }
116 }
117
118 @Override
119 protected boolean deleteOnExit() {
120 return deleteOnExitTemporaryFile;
121 }
122
123 @Override
124 protected String getBaseDirectory() {
125 return baseDirectory;
126 }
127
128 @Override
129 protected String getDiskFilename() {
130 return getName() + postfix;
131 }
132
133 @Override
134 protected String getPostfix() {
135 return postfix;
136 }
137
138 @Override
139 protected String getPrefix() {
140 return prefix;
141 }
142
143 @Override
144 public Attribute copy() {
145 DiskAttribute attr = new DiskAttribute(getName());
146 attr.setCharset(getCharset());
147 ByteBuf content = content();
148 if (content != null) {
149 try {
150 attr.setContent(content.copy());
151 } catch (IOException e) {
152 throw new ChannelException(e);
153 }
154 }
155 return attr;
156 }
157
158 @Override
159 public Attribute duplicate() {
160 DiskAttribute attr = new DiskAttribute(getName());
161 attr.setCharset(getCharset());
162 ByteBuf content = content();
163 if (content != null) {
164 try {
165 attr.setContent(content.duplicate());
166 } catch (IOException e) {
167 throw new ChannelException(e);
168 }
169 }
170 return attr;
171 }
172
173 @Override
174 public Attribute retain(int increment) {
175 super.retain(increment);
176 return this;
177 }
178
179 @Override
180 public Attribute retain() {
181 super.retain();
182 return this;
183 }
184 }