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    *   https://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 static io.netty.util.internal.ObjectUtil.checkNonEmpty;
19  
20  import io.netty.buffer.ByteBuf;
21  import io.netty.channel.ChannelException;
22  import io.netty.handler.codec.http.HttpConstants;
23  import io.netty.util.AbstractReferenceCounted;
24  import io.netty.util.internal.ObjectUtil;
25  
26  import java.io.IOException;
27  import java.nio.charset.Charset;
28  import java.util.regex.Pattern;
29  
30  /**
31   * Abstract HttpData implementation
32   */
33  public abstract class AbstractHttpData extends AbstractReferenceCounted implements HttpData {
34  
35      private static final Pattern STRIP_PATTERN = Pattern.compile("(?:^\\s+|\\s+$|\\n)");
36      private static final Pattern REPLACE_PATTERN = Pattern.compile("[\\r\\t]");
37  
38      private final String name;
39      protected long definedSize;
40      protected long size;
41      private Charset charset = HttpConstants.DEFAULT_CHARSET;
42      private boolean completed;
43      private long maxSize = DefaultHttpDataFactory.MAXSIZE;
44  
45      protected AbstractHttpData(String name, Charset charset, long size) {
46          ObjectUtil.checkNotNull(name, "name");
47  
48          name = REPLACE_PATTERN.matcher(name).replaceAll(" ");
49          name = STRIP_PATTERN.matcher(name).replaceAll("");
50  
51          this.name = checkNonEmpty(name, "name");
52          if (charset != null) {
53              setCharset(charset);
54          }
55          definedSize = size;
56      }
57  
58      @Override
59      public long getMaxSize() {
60          return maxSize;
61      }
62  
63      @Override
64      public void setMaxSize(long maxSize) {
65          this.maxSize = maxSize;
66      }
67  
68      @Override
69      public void checkSize(long newSize) throws IOException {
70          if (maxSize >= 0 && newSize > maxSize) {
71              throw new IOException("Size exceed allowed maximum capacity");
72          }
73      }
74  
75      @Override
76      public String getName() {
77          return name;
78      }
79  
80      @Override
81      public boolean isCompleted() {
82          return completed;
83      }
84  
85      protected void setCompleted() {
86          completed = true;
87      }
88  
89      @Override
90      public Charset getCharset() {
91          return charset;
92      }
93  
94      @Override
95      public void setCharset(Charset charset) {
96          this.charset = ObjectUtil.checkNotNull(charset, "charset");
97      }
98  
99      @Override
100     public long length() {
101         return size;
102     }
103 
104     @Override
105     public long definedLength() {
106         return definedSize;
107     }
108 
109     @Override
110     public ByteBuf content() {
111         try {
112             return getByteBuf();
113         } catch (IOException e) {
114             throw new ChannelException(e);
115         }
116     }
117 
118     @Override
119     protected void deallocate() {
120         delete();
121     }
122 
123     @Override
124     public HttpData retain() {
125         super.retain();
126         return this;
127     }
128 
129     @Override
130     public HttpData retain(int increment) {
131         super.retain(increment);
132         return this;
133     }
134 
135     @Override
136     public abstract HttpData touch();
137 
138     @Override
139     public abstract HttpData touch(Object hint);
140 }