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 org.jboss.netty.handler.codec.http.multipart;
17  
18  import java.io.IOException;
19  
20  import org.jboss.netty.buffer.ChannelBuffer;
21  import org.jboss.netty.buffer.ChannelBuffers;
22  import org.jboss.netty.handler.codec.http.HttpConstants;
23  
24  /**
25   * Memory implementation of Attributes
26   */
27  public class MemoryAttribute extends AbstractMemoryHttpData implements Attribute {
28  
29      public MemoryAttribute(String name) {
30          super(name, HttpConstants.DEFAULT_CHARSET, 0);
31      }
32      /**
33       *
34       * @param name
35       * @param value
36       * @throws NullPointerException
37       * @throws IllegalArgumentException
38       * @throws IOException
39       */
40      public MemoryAttribute(String name, String value) throws IOException {
41          super(name, HttpConstants.DEFAULT_CHARSET, 0); // Attribute have no default size
42          setValue(value);
43      }
44  
45      public HttpDataType getHttpDataType() {
46          return HttpDataType.Attribute;
47      }
48  
49      public String getValue() {
50          return getChannelBuffer().toString(charset);
51      }
52  
53      public void setValue(String value) throws IOException {
54          if (value == null) {
55              throw new NullPointerException("value");
56          }
57          byte [] bytes = value.getBytes(charset.name());
58          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
59          if (definedSize > 0) {
60              definedSize = buffer.readableBytes();
61          }
62          setContent(buffer);
63      }
64  
65      @Override
66      public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
67          int localsize = buffer.readableBytes();
68          if (definedSize > 0 && definedSize < size + localsize) {
69              definedSize = size + localsize;
70          }
71          super.addContent(buffer, last);
72      }
73  
74      @Override
75      public int hashCode() {
76          return getName().hashCode();
77      }
78  
79      @Override
80      public boolean equals(Object o) {
81          if (!(o instanceof Attribute)) {
82              return false;
83          }
84          Attribute attribute = (Attribute) o;
85          return getName().equalsIgnoreCase(attribute.getName());
86      }
87  
88      public int compareTo(InterfaceHttpData arg0) {
89          if (!(arg0 instanceof Attribute)) {
90              throw new ClassCastException("Cannot compare " + getHttpDataType() +
91                      " with " + arg0.getHttpDataType());
92          }
93          return compareTo((Attribute) arg0);
94      }
95  
96      public int compareTo(Attribute o) {
97          return getName().compareToIgnoreCase(o.getName());
98      }
99  
100     @Override
101     public String toString() {
102         return getName() + "=" + getValue();
103     }
104 
105 }