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 io.netty.handler.codec.http.multipart;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelException;
20  import io.netty.handler.codec.http.HttpConstants;
21  import io.netty.util.AbstractReferenceCounted;
22  
23  import java.io.IOException;
24  import java.nio.charset.Charset;
25  import java.util.regex.Pattern;
26  
27  /**
28   * Abstract HttpData implementation
29   */
30  public abstract class AbstractHttpData extends AbstractReferenceCounted implements HttpData {
31  
32      private static final Pattern STRIP_PATTERN = Pattern.compile("(?:^\\s+|\\s+$|\\n)");
33      private static final Pattern REPLACE_PATTERN = Pattern.compile("[\\r\\t]");
34  
35      protected final String name;
36      protected long definedSize;
37      protected long size;
38      protected Charset charset = HttpConstants.DEFAULT_CHARSET;
39      protected boolean completed;
40  
41      protected AbstractHttpData(String name, Charset charset, long size) {
42          if (name == null) {
43              throw new NullPointerException("name");
44          }
45  
46          name = REPLACE_PATTERN.matcher(name).replaceAll(" ");
47          name = STRIP_PATTERN.matcher(name).replaceAll("");
48  
49          if (name.isEmpty()) {
50              throw new IllegalArgumentException("empty name");
51          }
52  
53          this.name = name;
54          if (charset != null) {
55              setCharset(charset);
56          }
57          definedSize = size;
58      }
59  
60      @Override
61      public String getName() {
62          return name;
63      }
64  
65      @Override
66      public boolean isCompleted() {
67          return completed;
68      }
69  
70      @Override
71      public Charset getCharset() {
72          return charset;
73      }
74  
75      @Override
76      public void setCharset(Charset charset) {
77          if (charset == null) {
78              throw new NullPointerException("charset");
79          }
80          this.charset = charset;
81      }
82  
83      @Override
84      public long length() {
85          return size;
86      }
87  
88      @Override
89      public ByteBuf content() {
90          try {
91              return getByteBuf();
92          } catch (IOException e) {
93              throw new ChannelException(e);
94          }
95      }
96  
97      @Override
98      protected void deallocate() {
99          delete();
100     }
101 
102     @Override
103     public HttpData retain() {
104         super.retain();
105         return this;
106     }
107 
108     @Override
109     public HttpData retain(int increment) {
110         super.retain(increment);
111         return this;
112     }
113 }