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.handler.codec.http.HttpConstants;
19
20 import java.nio.charset.Charset;
21 import java.util.regex.Pattern;
22
23
24
25
26 public abstract class AbstractHttpData implements HttpData {
27
28 private static final Pattern STRIP_PATTERN = Pattern.compile("(?:^\\s+|\\s+$|\\n)");
29 private static final Pattern REPLACE_PATTERN = Pattern.compile("[\\r\\t]");
30
31 protected final String name;
32 protected long definedSize;
33 protected long size;
34 protected Charset charset = HttpConstants.DEFAULT_CHARSET;
35 protected boolean completed;
36
37 protected AbstractHttpData(String name, Charset charset, long size) {
38 if (name == null) {
39 throw new NullPointerException("name");
40 }
41
42 name = REPLACE_PATTERN.matcher(name).replaceAll(" ");
43 name = STRIP_PATTERN.matcher(name).replaceAll("");
44
45 if (name.length() == 0) {
46 throw new IllegalArgumentException("empty name");
47 }
48
49 this.name = name;
50 if (charset != null) {
51 setCharset(charset);
52 }
53 definedSize = size;
54 }
55
56 public String getName() {
57 return name;
58 }
59
60 public boolean isCompleted() {
61 return completed;
62 }
63
64 public Charset getCharset() {
65 return charset;
66 }
67
68 public void setCharset(Charset charset) {
69 if (charset == null) {
70 throw new NullPointerException("charset");
71 }
72 this.charset = charset;
73 }
74
75 public long length() {
76 return size;
77 }
78 }