View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * This Attribute is only for Encoder use to insert special command between object if needed
28   * (like Multipart Mixed mode)
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         // Do nothing
127     }
128 }