查看本类的 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    *   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          setCompleted(true);
87      }
88  
89      protected void setCompleted(boolean completed) {
90          this.completed = completed;
91      }
92  
93      @Override
94      public Charset getCharset() {
95          return charset;
96      }
97  
98      @Override
99      public void setCharset(Charset charset) {
100         this.charset = ObjectUtil.checkNotNull(charset, "charset");
101     }
102 
103     @Override
104     public long length() {
105         return size;
106     }
107 
108     @Override
109     public long definedLength() {
110         return definedSize;
111     }
112 
113     @Override
114     public ByteBuf content() {
115         try {
116             return getByteBuf();
117         } catch (IOException e) {
118             throw new ChannelException(e);
119         }
120     }
121 
122     @Override
123     protected void deallocate() {
124         delete();
125     }
126 
127     @Override
128     public HttpData retain() {
129         super.retain();
130         return this;
131     }
132 
133     @Override
134     public HttpData retain(int increment) {
135         super.retain(increment);
136         return this;
137     }
138 
139     @Override
140     public abstract HttpData touch();
141 
142     @Override
143     public abstract HttpData touch(Object hint);
144 }