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 ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
52 if (definedSize > 0) {
53 definedSize = buffer.readableBytes();
54 }
55 setContent(buffer);
56 }
57
58 @Override
59 public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
60 int localsize = buffer.readableBytes();
61 if (definedSize > 0 && definedSize < size + localsize) {
62 definedSize = size + localsize;
63 }
64 super.addContent(buffer, last);
65 }
66
67 @Override
68 public int hashCode() {
69 return getName().hashCode();
70 }
71
72 @Override
73 public boolean equals(Object o) {
74 if (!(o instanceof Attribute)) {
75 return false;
76 }
77 Attribute attribute = (Attribute) o;
78 return getName().equalsIgnoreCase(attribute.getName());
79 }
80
81 public int compareTo(InterfaceHttpData other) {
82 if (!(other instanceof Attribute)) {
83 throw new ClassCastException("Cannot compare " + getHttpDataType() +
84 " with " + other.getHttpDataType());
85 }
86 return compareTo((Attribute) other);
87 }
88
89 public int compareTo(Attribute o) {
90 return getName().compareToIgnoreCase(o.getName());
91 }
92
93 @Override
94 public String toString() {
95 return getName() + '=' + getValue();
96 }
97 }