View Javadoc

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.handler.codec.http.HttpConstants;
19  
20  import java.nio.charset.Charset;
21  import java.util.regex.Pattern;
22  
23  /**
24   * Abstract HttpData implementation
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  }