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 MemoryAttribute extends AbstractMemoryHttpData implements Attribute {
30
31 public MemoryAttribute(String name) {
32 super(name, HttpConstants.DEFAULT_CHARSET, 0);
33 }
34
35 public MemoryAttribute(String name, String value) throws IOException {
36 super(name, HttpConstants.DEFAULT_CHARSET, 0);
37 setValue(value);
38 }
39
40 @Override
41 public HttpDataType getHttpDataType() {
42 return HttpDataType.Attribute;
43 }
44
45 @Override
46 public String getValue() {
47 return getByteBuf().toString(charset);
48 }
49
50 @Override
51 public void setValue(String value) throws IOException {
52 if (value == null) {
53 throw new NullPointerException("value");
54 }
55 byte [] bytes = value.getBytes(charset.name());
56 ByteBuf buffer = wrappedBuffer(bytes);
57 if (definedSize > 0) {
58 definedSize = buffer.readableBytes();
59 }
60 setContent(buffer);
61 }
62
63 @Override
64 public void addContent(ByteBuf buffer, boolean last) throws IOException {
65 int localsize = buffer.readableBytes();
66 if (definedSize > 0 && definedSize < size + localsize) {
67 definedSize = size + localsize;
68 }
69 super.addContent(buffer, last);
70 }
71
72 @Override
73 public int hashCode() {
74 return getName().hashCode();
75 }
76
77 @Override
78 public boolean equals(Object o) {
79 if (!(o instanceof Attribute)) {
80 return false;
81 }
82 Attribute attribute = (Attribute) o;
83 return getName().equalsIgnoreCase(attribute.getName());
84 }
85
86 @Override
87 public int compareTo(InterfaceHttpData other) {
88 if (!(other instanceof Attribute)) {
89 throw new ClassCastException("Cannot compare " + getHttpDataType() +
90 " with " + other.getHttpDataType());
91 }
92 return compareTo((Attribute) other);
93 }
94
95 public int compareTo(Attribute o) {
96 return getName().compareToIgnoreCase(o.getName());
97 }
98
99 @Override
100 public String toString() {
101 return getName() + '=' + getValue();
102 }
103
104 @Override
105 public Attribute copy() {
106 MemoryAttribute attr = new MemoryAttribute(getName());
107 attr.setCharset(getCharset());
108 ByteBuf content = content();
109 if (content != null) {
110 try {
111 attr.setContent(content.copy());
112 } catch (IOException e) {
113 throw new ChannelException(e);
114 }
115 }
116 return attr;
117 }
118
119 @Override
120 public Attribute duplicate() {
121 MemoryAttribute attr = new MemoryAttribute(getName());
122 attr.setCharset(getCharset());
123 ByteBuf content = content();
124 if (content != null) {
125 try {
126 attr.setContent(content.duplicate());
127 } catch (IOException e) {
128 throw new ChannelException(e);
129 }
130 }
131 return attr;
132 }
133
134 @Override
135 public Attribute retain() {
136 super.retain();
137 return this;
138 }
139
140 @Override
141 public Attribute retain(int increment) {
142 super.retain(increment);
143 return this;
144 }
145 }