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 java.nio.charset.Charset;
19  
20  import org.jboss.netty.handler.codec.http.HttpConstants;
21  
22  /**
23   * Abstract HttpData implementation
24   */
25  public abstract class AbstractHttpData implements HttpData {
26  
27      protected final String name;
28      protected long definedSize;
29      protected long size;
30      protected Charset charset = HttpConstants.DEFAULT_CHARSET;
31      protected boolean completed;
32  
33      public AbstractHttpData(String name, Charset charset, long size) {
34          if (name == null) {
35              throw new NullPointerException("name");
36          }
37          name = name.trim();
38          if (name.length() == 0) {
39              throw new IllegalArgumentException("empty name");
40          }
41  
42          for (int i = 0; i < name.length(); i ++) {
43              char c = name.charAt(i);
44              if (c > 127) {
45                  throw new IllegalArgumentException(
46                          "name contains non-ascii character: " + name);
47              }
48  
49              // Check prohibited characters.
50              switch (c) {
51              case '=':
52              case ',':
53              case ';':
54              case ' ':
55              case '\t':
56              case '\r':
57              case '\n':
58              case '\f':
59              case 0x0b: // Vertical tab
60                  throw new IllegalArgumentException(
61                          "name contains one of the following prohibited characters: " +
62                          "=,; \\t\\r\\n\\v\\f: " + name);
63              }
64          }
65          this.name = name;
66          if (charset != null) {
67              setCharset(charset);
68          }
69          definedSize = size;
70      }
71  
72      public String getName() {
73          return name;
74      }
75  
76      public boolean isCompleted() {
77          return completed;
78      }
79  
80      public Charset getCharset() {
81          return charset;
82      }
83  
84      public void setCharset(Charset charset) {
85          if (charset == null) {
86              throw new NullPointerException("charset");
87          }
88          this.charset = charset;
89      }
90  
91      public long length() {
92          return size;
93      }
94  }