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