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