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          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
52          if (definedSize > 0) {
53              definedSize = buffer.readableBytes();
54          }
55          setContent(buffer);
56      }
57  
58      @Override
59      public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
60          int localsize = buffer.readableBytes();
61          if (definedSize > 0 && definedSize < size + localsize) {
62              definedSize = size + localsize;
63          }
64          super.addContent(buffer, last);
65      }
66  
67      @Override
68      public int hashCode() {
69          return getName().hashCode();
70      }
71  
72      @Override
73      public boolean equals(Object o) {
74          if (!(o instanceof Attribute)) {
75              return false;
76          }
77          Attribute attribute = (Attribute) o;
78          return getName().equalsIgnoreCase(attribute.getName());
79      }
80  
81      public int compareTo(InterfaceHttpData other) {
82          if (!(other instanceof Attribute)) {
83              throw new ClassCastException("Cannot compare " + getHttpDataType() +
84                      " with " + other.getHttpDataType());
85          }
86          return compareTo((Attribute) other);
87      }
88  
89      public int compareTo(Attribute o) {
90          return getName().compareToIgnoreCase(o.getName());
91      }
92  
93      @Override
94      public String toString() {
95          return getName() + '=' + getValue();
96      }
97  }