View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.http2;
17  
18  import java.io.IOException;
19  
20  import org.jboss.netty.buffer.ChannelBuffer;
21  import org.jboss.netty.buffer.ChannelBuffers;
22  
23  /**
24   * Memory implementation of Attributes
25   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
26   * @author Andy Taylor (andy.taylor@jboss.org)
27   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
28   * @author <a href="http://openr66.free.fr/">Frederic Bregier</a>
29   *
30   */
31  public class MemoryAttribute extends AbstractMemoryHttpData implements Attribute {
32  
33      public MemoryAttribute(String name) {
34          super(name, HttpCodecUtil.DEFAULT_CHARSET, 0);
35      }
36      /**
37       *
38       * @param name
39       * @param value
40       * @throws NullPointerException
41       * @throws IllegalArgumentException
42       * @throws IOException
43       */
44      public MemoryAttribute(String name, String value)
45              throws NullPointerException, IllegalArgumentException, IOException {
46          super(name, HttpCodecUtil.DEFAULT_CHARSET, 0); // Attribute have no default size
47          setValue(value);
48      }
49  
50      public HttpDataType getHttpDataType() {
51          return HttpDataType.Attribute;
52      }
53  
54      public String getValue() {
55          return getChannelBuffer().toString(charset);
56      }
57  
58      public void setValue(String value) throws IOException {
59          if (value == null) {
60              throw new NullPointerException("value");
61          }
62          byte [] bytes = value.getBytes(charset);
63          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
64          if (definedSize > 0) {
65              definedSize = buffer.readableBytes();
66          }
67          setContent(buffer);
68      }
69  
70      @Override
71      public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
72          int localsize = buffer.readableBytes();
73          if (definedSize > 0 && definedSize < size + localsize) {
74              definedSize = size + localsize;
75          }
76          super.addContent(buffer, last);
77      }
78  
79      @Override
80      public int hashCode() {
81          return getName().hashCode();
82      }
83  
84      @Override
85      public boolean equals(Object o) {
86          if (!(o instanceof Attribute)) {
87              return false;
88          }
89          Attribute attribute = (Attribute) o;
90          return getName().equalsIgnoreCase(attribute.getName());
91      }
92  
93      public int compareTo(InterfaceHttpData arg0) {
94          if (!(arg0 instanceof Attribute)) {
95              throw new ClassCastException("Cannot compare " + getHttpDataType() +
96                      " with " + arg0.getHttpDataType());
97          }
98          return compareTo((Attribute) arg0);
99      }
100 
101     public int compareTo(Attribute o) {
102         return getName().compareToIgnoreCase(o.getName());
103     }
104 
105     @Override
106     public String toString() {
107         return getName() + "=" + getValue();
108     }
109 
110 }