1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.multipart;
17
18 import java.io.IOException;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.jboss.netty.buffer.ChannelBuffers;
22 import org.jboss.netty.handler.codec.http.HttpConstants;
23
24
25
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 String prefix = "Attr_";
33
34 public static String postfix = ".att";
35
36
37
38
39
40 public DiskAttribute(String name) {
41 super(name, HttpConstants.DEFAULT_CHARSET, 0);
42 }
43
44
45
46
47
48
49
50
51 public DiskAttribute(String name, String value) throws IOException {
52 super(name, HttpConstants.DEFAULT_CHARSET, 0);
53 setValue(value);
54 }
55
56 public HttpDataType getHttpDataType() {
57 return HttpDataType.Attribute;
58 }
59
60 public String getValue() throws IOException {
61 byte [] bytes = get();
62 return new String(bytes, charset.name());
63 }
64
65 public void setValue(String value) throws IOException {
66 if (value == null) {
67 throw new NullPointerException("value");
68 }
69 byte [] bytes = value.getBytes(charset.name());
70 ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
71 if (definedSize > 0) {
72 definedSize = buffer.readableBytes();
73 }
74 setContent(buffer);
75 }
76
77 @Override
78 public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
79 int localsize = buffer.readableBytes();
80 if (definedSize > 0 && definedSize < size + localsize) {
81 definedSize = size + localsize;
82 }
83 super.addContent(buffer, last);
84 }
85 @Override
86 public int hashCode() {
87 return getName().hashCode();
88 }
89
90 @Override
91 public boolean equals(Object o) {
92 if (!(o instanceof Attribute)) {
93 return false;
94 }
95 Attribute attribute = (Attribute) o;
96 return getName().equalsIgnoreCase(attribute.getName());
97 }
98
99 public int compareTo(InterfaceHttpData arg0) {
100 if (!(arg0 instanceof Attribute)) {
101 throw new ClassCastException("Cannot compare " + getHttpDataType() +
102 " with " + arg0.getHttpDataType());
103 }
104 return compareTo((Attribute) arg0);
105 }
106
107 public int compareTo(Attribute o) {
108 return getName().compareToIgnoreCase(o.getName());
109 }
110
111 @Override
112 public String toString() {
113 try {
114 return getName() + "=" + getValue();
115 } catch (IOException e) {
116 return getName() + "=IoException";
117 }
118 }
119
120 @Override
121 protected boolean deleteOnExit() {
122 return deleteOnExitTemporaryFile;
123 }
124
125 @Override
126 protected String getBaseDirectory() {
127 return baseDirectory;
128 }
129
130 @Override
131 protected String getDiskFilename() {
132 return getName() + postfix;
133 }
134
135 @Override
136 protected String getPostfix() {
137 return postfix;
138 }
139
140 @Override
141 protected String getPrefix() {
142 return prefix;
143 }
144 }