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