查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.io.IOException;
21  import java.nio.charset.Charset;
22  import java.util.regex.Pattern;
23  
24  /**
25   * Abstract HttpData implementation
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  }