查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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          checkSize(bytes.length);
62          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
63          if (definedSize > 0) {
64              definedSize = buffer.readableBytes();
65          }
66          setContent(buffer);
67      }
68  
69      @Override
70      public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
71          int localsize = buffer.readableBytes();
72          checkSize(size + localsize);
73          if (definedSize > 0 && definedSize < size + localsize) {
74              definedSize = size + localsize;
75          }
76          super.addContent(buffer, last);
77      }
78      @Override
79      public int hashCode() {
80          return getName().hashCode();
81      }
82  
83      @Override
84      public boolean equals(Object o) {
85          if (!(o instanceof Attribute)) {
86              return false;
87          }
88          Attribute attribute = (Attribute) o;
89          return getName().equalsIgnoreCase(attribute.getName());
90      }
91  
92      public int compareTo(InterfaceHttpData o) {
93          if (!(o instanceof Attribute)) {
94              throw new ClassCastException("Cannot compare " + getHttpDataType() +
95                      " with " + o.getHttpDataType());
96          }
97          return compareTo((Attribute) o);
98      }
99  
100     public int compareTo(Attribute o) {
101         return getName().compareToIgnoreCase(o.getName());
102     }
103 
104     @Override
105     public String toString() {
106         try {
107             return getName() + '=' + getValue();
108         } catch (IOException e) {
109             return getName() + "=IoException";
110         }
111     }
112 
113     @Override
114     protected boolean deleteOnExit() {
115         return deleteOnExitTemporaryFile;
116     }
117 
118     @Override
119     protected String getBaseDirectory() {
120         return baseDirectory;
121     }
122 
123     @Override
124     protected String getDiskFilename() {
125         return getName() + postfix;
126     }
127 
128     @Override
129     protected String getPostfix() {
130         return postfix;
131     }
132 
133     @Override
134     protected String getPrefix() {
135         return prefix;
136     }
137 }