查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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 org.jboss.netty.handler.codec.http.multipart;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.buffer.ChannelBuffers;
20  import org.jboss.netty.util.CharsetUtil;
21  
22  import java.io.UnsupportedEncodingException;
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  public class InternalAttribute implements InterfaceHttpData {
32      protected final List<String> value = new ArrayList<String>();
33      private final Charset charset;
34  
35      public InternalAttribute(Charset charset) {
36          this.charset = charset;
37      }
38  
39      public HttpDataType getHttpDataType() {
40          return HttpDataType.InternalAttribute;
41      }
42  
43      public void addValue(String value) {
44          if (value == null) {
45              throw new NullPointerException("value");
46          }
47          this.value.add(value);
48      }
49  
50      public void addValue(String value, int rank) {
51          if (value == null) {
52              throw new NullPointerException("value");
53          }
54          this.value.add(rank, value);
55      }
56  
57      public void setValue(String value, int rank) {
58          if (value == null) {
59              throw new NullPointerException("value");
60          }
61          this.value.set(rank, value);
62      }
63  
64      @Override
65      public int hashCode() {
66          return getName().hashCode();
67      }
68  
69      @Override
70      public boolean equals(Object o) {
71          if (!(o instanceof Attribute)) {
72              return false;
73          }
74          Attribute attribute = (Attribute) o;
75          return getName().equalsIgnoreCase(attribute.getName());
76      }
77  
78      public int compareTo(InterfaceHttpData o) {
79          if (!(o instanceof InternalAttribute)) {
80              throw new ClassCastException("Cannot compare " + getHttpDataType() +
81                      " with " + o.getHttpDataType());
82          }
83          return compareTo((InternalAttribute) o);
84      }
85  
86      public int compareTo(InternalAttribute o) {
87          return getName().compareToIgnoreCase(o.getName());
88      }
89  
90      public int size() {
91          int size = 0;
92          for (String elt : value) {
93              try {
94                  size += elt.getBytes(charset.name()).length;
95              } catch (UnsupportedEncodingException e) {
96                  throw new RuntimeException(e);
97              }
98          }
99          return size;
100     }
101     @Override
102     public String toString() {
103         StringBuilder result = new StringBuilder();
104         for (String elt : value) {
105             result.append(elt);
106         }
107         return result.toString();
108     }
109 
110     public ChannelBuffer toChannelBuffer() {
111         ChannelBuffer[] buffers = new ChannelBuffer[value.size()];
112         for (int i = 0; i < buffers.length; i++) {
113             buffers[i] = ChannelBuffers.copiedBuffer(value.get(i), charset);
114         }
115         return ChannelBuffers.wrappedBuffer(buffers);
116     }
117 
118     public String getName() {
119         return "InternalAttribute";
120     }
121 }