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