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.IOException;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.jboss.netty.buffer.ChannelBuffers;
22 import org.jboss.netty.handler.codec.http.HttpConstants;
23
24
25
26
27 public class MemoryAttribute extends AbstractMemoryHttpData implements Attribute {
28
29 public MemoryAttribute(String name) {
30 super(name, HttpConstants.DEFAULT_CHARSET, 0);
31 }
32
33
34
35
36
37
38
39
40 public MemoryAttribute(String name, String value) throws IOException {
41 super(name, HttpConstants.DEFAULT_CHARSET, 0);
42 setValue(value);
43 }
44
45 public HttpDataType getHttpDataType() {
46 return HttpDataType.Attribute;
47 }
48
49 public String getValue() {
50 return getChannelBuffer().toString(charset);
51 }
52
53 public void setValue(String value) throws IOException {
54 if (value == null) {
55 throw new NullPointerException("value");
56 }
57 byte [] bytes = value.getBytes(charset.name());
58 ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
59 if (definedSize > 0) {
60 definedSize = buffer.readableBytes();
61 }
62 setContent(buffer);
63 }
64
65 @Override
66 public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
67 int localsize = buffer.readableBytes();
68 if (definedSize > 0 && definedSize < size + localsize) {
69 definedSize = size + localsize;
70 }
71 super.addContent(buffer, last);
72 }
73
74 @Override
75 public int hashCode() {
76 return getName().hashCode();
77 }
78
79 @Override
80 public boolean equals(Object o) {
81 if (!(o instanceof Attribute)) {
82 return false;
83 }
84 Attribute attribute = (Attribute) o;
85 return getName().equalsIgnoreCase(attribute.getName());
86 }
87
88 public int compareTo(InterfaceHttpData arg0) {
89 if (!(arg0 instanceof Attribute)) {
90 throw new ClassCastException("Cannot compare " + getHttpDataType() +
91 " with " + arg0.getHttpDataType());
92 }
93 return compareTo((Attribute) arg0);
94 }
95
96 public int compareTo(Attribute o) {
97 return getName().compareToIgnoreCase(o.getName());
98 }
99
100 @Override
101 public String toString() {
102 return getName() + "=" + getValue();
103 }
104
105 }