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.buffer.Unpooled;
20 import io.netty.util.AbstractReferenceCounted;
21
22 import java.nio.charset.Charset;
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30 final class InternalAttribute extends AbstractReferenceCounted implements InterfaceHttpData {
31 private final List<ByteBuf> value = new ArrayList<ByteBuf>();
32 private final Charset charset;
33 private int size;
34
35 InternalAttribute(Charset charset) {
36 this.charset = charset;
37 }
38
39 @Override
40 public HttpDataType getHttpDataType() {
41 return HttpDataType.InternalAttribute;
42 }
43
44 public void addValue(String value) {
45 if (value == null) {
46 throw new NullPointerException("value");
47 }
48 ByteBuf buf = Unpooled.copiedBuffer(value, charset);
49 this.value.add(buf);
50 size += buf.readableBytes();
51 }
52
53 public void addValue(String value, int rank) {
54 if (value == null) {
55 throw new NullPointerException("value");
56 }
57 ByteBuf buf = Unpooled.copiedBuffer(value, charset);
58 this.value.add(rank, buf);
59 size += buf.readableBytes();
60 }
61
62 public void setValue(String value, int rank) {
63 if (value == null) {
64 throw new NullPointerException("value");
65 }
66 ByteBuf buf = Unpooled.copiedBuffer(value, charset);
67 ByteBuf old = this.value.set(rank, buf);
68 if (old != null) {
69 size -= old.readableBytes();
70 old.release();
71 }
72 size += buf.readableBytes();
73 }
74
75 @Override
76 public int hashCode() {
77 return getName().hashCode();
78 }
79
80 @Override
81 public boolean equals(Object o) {
82 if (!(o instanceof InternalAttribute)) {
83 return false;
84 }
85 InternalAttribute attribute = (InternalAttribute) o;
86 return getName().equalsIgnoreCase(attribute.getName());
87 }
88
89 @Override
90 public int compareTo(InterfaceHttpData o) {
91 if (!(o instanceof InternalAttribute)) {
92 throw new ClassCastException("Cannot compare " + getHttpDataType() +
93 " with " + o.getHttpDataType());
94 }
95 return compareTo((InternalAttribute) o);
96 }
97
98 public int compareTo(InternalAttribute o) {
99 return getName().compareToIgnoreCase(o.getName());
100 }
101
102 @Override
103 public String toString() {
104 StringBuilder result = new StringBuilder();
105 for (ByteBuf elt : value) {
106 result.append(elt.toString(charset));
107 }
108 return result.toString();
109 }
110
111 public int size() {
112 return size;
113 }
114
115 public ByteBuf toByteBuf() {
116 return Unpooled.compositeBuffer().addComponents(value).writerIndex(size()).readerIndex(0);
117 }
118
119 @Override
120 public String getName() {
121 return "InternalAttribute";
122 }
123
124 @Override
125 protected void deallocate() {
126
127 }
128 }