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   * Disk 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 DiskAttribute extends AbstractDiskHttpData implements Attribute {
32      public static String baseDirectory = null;
33  
34      public static boolean deleteOnExitTemporaryFile = true;
35  
36      public static String prefix = "Attr_";
37  
38      public static String postfix = ".att";
39  
40      /**
41       * Constructor used for huge Attribute
42       * @param name
43       */
44      public DiskAttribute(String name) {
45          super(name, HttpCodecUtil.DEFAULT_CHARSET, 0);
46      }
47      /**
48       *
49       * @param name
50       * @param value
51       * @throws NullPointerException
52       * @throws IllegalArgumentException
53       * @throws IOException
54       */
55      public DiskAttribute(String name, String value)
56              throws NullPointerException, IllegalArgumentException, IOException {
57          super(name, HttpCodecUtil.DEFAULT_CHARSET, 0); // Attribute have no default size
58          setValue(value);
59      }
60  
61      public HttpDataType getHttpDataType() {
62          return HttpDataType.Attribute;
63      }
64  
65      public String getValue() throws IOException {
66          byte [] bytes = get();
67          return new String(bytes, charset);
68      }
69  
70      public void setValue(String value) throws IOException {
71          if (value == null) {
72              throw new NullPointerException("value");
73          }
74          byte [] bytes = value.getBytes(charset);
75          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
76          if (definedSize > 0) {
77              definedSize = buffer.readableBytes();
78          }
79          setContent(buffer);
80      }
81  
82      @Override
83      public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
84          int localsize = buffer.readableBytes();
85          if (definedSize > 0 && definedSize < size + localsize) {
86              definedSize = size + localsize;
87          }
88          super.addContent(buffer, last);
89      }
90      @Override
91      public int hashCode() {
92          return getName().hashCode();
93      }
94  
95      @Override
96      public boolean equals(Object o) {
97          if (!(o instanceof Attribute)) {
98              return false;
99          }
100         Attribute attribute = (Attribute) o;
101         return getName().equalsIgnoreCase(attribute.getName());
102     }
103 
104     public int compareTo(InterfaceHttpData arg0) {
105         if (!(arg0 instanceof Attribute)) {
106             throw new ClassCastException("Cannot compare " + getHttpDataType() +
107                     " with " + arg0.getHttpDataType());
108         }
109         return compareTo((Attribute) arg0);
110     }
111 
112     public int compareTo(Attribute o) {
113         return getName().compareToIgnoreCase(o.getName());
114     }
115 
116     @Override
117     public String toString() {
118         try {
119             return getName() + "=" + getValue();
120         } catch (IOException e) {
121             return getName() + "=IoException";
122         }
123     }
124 
125     protected boolean deleteOnExit() {
126         return deleteOnExitTemporaryFile;
127     }
128 
129     protected String getBaseDirectory() {
130         return baseDirectory;
131     }
132 
133     protected String getDiskFilename() {
134         return getName()+postfix;
135     }
136 
137     protected String getPostfix() {
138         return postfix;
139     }
140 
141     protected String getPrefix() {
142         return prefix;
143     }
144 }