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