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 org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.util.CharsetUtil;
20
21 import java.nio.charset.Charset;
22
23
24
25
26 final class HttpPostBodyUtil {
27
28 public static int chunkSize = 8096;
29
30
31
32 public static final String CONTENT_DISPOSITION = "Content-Disposition";
33
34 public static final String NAME = "name";
35
36 public static final String FILENAME = "filename";
37
38
39
40
41 public static final String FORM_DATA = "form-data";
42
43
44
45
46 public static final String ATTACHMENT = "attachment";
47
48
49
50
51 public static final String FILE = "file";
52
53
54
55
56 public static final String MULTIPART_MIXED = "multipart/mixed";
57
58
59
60
61 public static final Charset ISO_8859_1 = CharsetUtil.ISO_8859_1;
62
63
64
65
66 public static final Charset US_ASCII = CharsetUtil.US_ASCII;
67
68
69
70
71 public static final String DEFAULT_BINARY_CONTENT_TYPE = "application/octet-stream";
72
73
74
75
76 public static final String DEFAULT_TEXT_CONTENT_TYPE = "text/plain";
77
78
79
80
81
82
83
84
85
86 public enum TransferEncodingMechanism {
87
88
89
90 BIT7("7bit"),
91
92
93
94 BIT8("8bit"),
95
96
97
98 BINARY("binary");
99
100 private final String value;
101
102 TransferEncodingMechanism(String value) {
103 this.value = value;
104 }
105
106 TransferEncodingMechanism() {
107 value = name();
108 }
109
110 public String value() {
111 return value;
112 }
113
114 @Override
115 public String toString() {
116 return value;
117 }
118 }
119
120 private HttpPostBodyUtil() {
121 }
122
123
124
125
126
127 static class SeekAheadNoBackArrayException extends Exception {
128 private static final long serialVersionUID = -630418804938699495L;
129 }
130
131
132
133
134
135 static class SeekAheadOptimize {
136 byte[] bytes;
137 int readerIndex;
138 int pos;
139 int origPos;
140 int limit;
141 ChannelBuffer buffer;
142
143
144
145
146 SeekAheadOptimize(ChannelBuffer buffer) throws SeekAheadNoBackArrayException {
147 if (!buffer.hasArray()) {
148 throw new SeekAheadNoBackArrayException();
149 }
150 this.buffer = buffer;
151 bytes = buffer.array();
152 readerIndex = buffer.readerIndex();
153 origPos = pos = buffer.arrayOffset() + readerIndex;
154 limit = buffer.arrayOffset() + buffer.writerIndex();
155 }
156
157
158
159
160
161
162 void setReadPosition(int minus) {
163 pos -= minus;
164 readerIndex = getReadPosition(pos);
165 buffer.readerIndex(readerIndex);
166 }
167
168
169
170
171
172
173 int getReadPosition(int index) {
174 return index - origPos + readerIndex;
175 }
176
177 void clear() {
178 buffer = null;
179 bytes = null;
180 limit = 0;
181 pos = 0;
182 readerIndex = 0;
183 }
184 }
185
186
187
188
189
190
191
192 static int findNonWhitespace(String sb, int offset) {
193 int result;
194 for (result = offset; result < sb.length(); result ++) {
195 if (!Character.isWhitespace(sb.charAt(result))) {
196 break;
197 }
198 }
199 return result;
200 }
201
202
203
204
205
206
207
208 static int findWhitespace(String sb, int offset) {
209 int result;
210 for (result = offset; result < sb.length(); result ++) {
211 if (Character.isWhitespace(sb.charAt(result))) {
212 break;
213 }
214 }
215 return result;
216 }
217
218
219
220
221
222
223 static int findEndOfString(String sb) {
224 int result;
225 for (result = sb.length(); result > 0; result --) {
226 if (!Character.isWhitespace(sb.charAt(result - 1))) {
227 break;
228 }
229 }
230 return result;
231 }
232
233 }