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    *   https://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  import io.netty.util.internal.ObjectUtil;
22  
23  import java.nio.charset.Charset;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * This Attribute is only for Encoder use to insert special command between object if needed
29   * (like Multipart Mixed mode)
30   */
31  final class InternalAttribute extends AbstractReferenceCounted implements InterfaceHttpData {
32      private final List<ByteBuf> value = new ArrayList<ByteBuf>();
33      private final Charset charset;
34      private int size;
35  
36      InternalAttribute(Charset charset) {
37          this.charset = charset;
38      }
39  
40      @Override
41      public HttpDataType getHttpDataType() {
42          return HttpDataType.InternalAttribute;
43      }
44  
45      public void addValue(String value) {
46          ObjectUtil.checkNotNull(value, "value");
47          ByteBuf buf = Unpooled.copiedBuffer(value, charset);
48          this.value.add(buf);
49          size += buf.readableBytes();
50      }
51  
52      public void addValue(String value, int rank) {
53          ObjectUtil.checkNotNull(value, "value");
54          ByteBuf buf = Unpooled.copiedBuffer(value, charset);
55          this.value.add(rank, buf);
56          size += buf.readableBytes();
57      }
58  
59      public void setValue(String value, int rank) {
60          ObjectUtil.checkNotNull(value, "value");
61          ByteBuf buf = Unpooled.copiedBuffer(value, charset);
62          ByteBuf old = this.value.set(rank, buf);
63          if (old != null) {
64              size -= old.readableBytes();
65              old.release();
66          }
67          size += buf.readableBytes();
68      }
69  
70      @Override
71      public int hashCode() {
72          return getName().hashCode();
73      }
74  
75      @Override
76      public boolean equals(Object o) {
77          if (!(o instanceof InternalAttribute)) {
78              return false;
79          }
80          InternalAttribute attribute = (InternalAttribute) o;
81          return getName().equalsIgnoreCase(attribute.getName());
82      }
83  
84      @Override
85      public int compareTo(InterfaceHttpData o) {
86          if (!(o instanceof InternalAttribute)) {
87              throw new ClassCastException("Cannot compare " + getHttpDataType() +
88                      " with " + o.getHttpDataType());
89          }
90          return compareTo((InternalAttribute) o);
91      }
92  
93      public int compareTo(InternalAttribute o) {
94          return getName().compareToIgnoreCase(o.getName());
95      }
96  
97      @Override
98      public String toString() {
99          StringBuilder result = new StringBuilder();
100         for (ByteBuf elt : value) {
101             result.append(elt.toString(charset));
102         }
103         return result.toString();
104     }
105 
106     public int size() {
107         return size;
108     }
109 
110     public ByteBuf toByteBuf() {
111         return Unpooled.compositeBuffer().addComponents(value).writerIndex(size()).readerIndex(0);
112     }
113 
114     @Override
115     public String getName() {
116         return "InternalAttribute";
117     }
118 
119     @Override
120     protected void deallocate() {
121         // Do nothing
122     }
123 
124     @Override
125     public InterfaceHttpData retain() {
126         for (ByteBuf buf: value) {
127             buf.retain();
128         }
129         return this;
130     }
131 
132     @Override
133     public InterfaceHttpData retain(int increment) {
134         for (ByteBuf buf: value) {
135             buf.retain(increment);
136         }
137         return this;
138     }
139 
140     @Override
141     public InterfaceHttpData touch() {
142         for (ByteBuf buf: value) {
143             buf.touch();
144         }
145         return this;
146     }
147 
148     @Override
149     public InterfaceHttpData touch(Object hint) {
150         for (ByteBuf buf: value) {
151             buf.touch(hint);
152         }
153         return this;
154     }
155 }