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 io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelException;
20  import io.netty.handler.codec.http.HttpConstants;
21  import io.netty.util.internal.ObjectUtil;
22  
23  import java.io.IOException;
24  import java.nio.charset.Charset;
25  
26  import static io.netty.buffer.Unpooled.*;
27  
28  /**
29   * Memory implementation of Attributes
30   */
31  public class MemoryAttribute extends AbstractMemoryHttpData implements Attribute {
32  
33      public MemoryAttribute(String name) {
34          this(name, HttpConstants.DEFAULT_CHARSET);
35      }
36  
37      public MemoryAttribute(String name, long definedSize) {
38          this(name, definedSize, HttpConstants.DEFAULT_CHARSET);
39      }
40  
41      public MemoryAttribute(String name, Charset charset) {
42          super(name, charset, 0);
43      }
44  
45      public MemoryAttribute(String name, long definedSize, Charset charset) {
46          super(name, charset, definedSize);
47      }
48  
49      public MemoryAttribute(String name, String value) throws IOException {
50          this(name, value, HttpConstants.DEFAULT_CHARSET); // Attribute have no default size
51      }
52  
53      public MemoryAttribute(String name, String value, Charset charset) throws IOException {
54          super(name, charset, 0); // Attribute have no default size
55          setValue(value);
56      }
57  
58      @Override
59      public HttpDataType getHttpDataType() {
60          return HttpDataType.Attribute;
61      }
62  
63      @Override
64      public String getValue() {
65          return getByteBuf().toString(getCharset());
66      }
67  
68      @Override
69      public void setValue(String value) throws IOException {
70          ObjectUtil.checkNotNull(value, "value");
71          byte [] bytes = value.getBytes(getCharset());
72          checkSize(bytes.length);
73          ByteBuf buffer = wrappedBuffer(bytes);
74          if (definedSize > 0) {
75              definedSize = buffer.readableBytes();
76          }
77          setContent(buffer);
78      }
79  
80      @Override
81      public void addContent(ByteBuf buffer, boolean last) throws IOException {
82          int localsize = buffer.readableBytes();
83          try {
84              checkSize(size + localsize);
85          } catch (IOException e) {
86              buffer.release();
87              throw e;
88          }
89          if (definedSize > 0 && definedSize < size + localsize) {
90              definedSize = size + localsize;
91          }
92          super.addContent(buffer, last);
93      }
94  
95      @Override
96      public int hashCode() {
97          return getName().hashCode();
98      }
99  
100     @Override
101     public boolean equals(Object o) {
102         if (!(o instanceof Attribute)) {
103             return false;
104         }
105         Attribute attribute = (Attribute) o;
106         return getName().equalsIgnoreCase(attribute.getName());
107     }
108 
109     @Override
110     public int compareTo(InterfaceHttpData other) {
111         if (!(other instanceof Attribute)) {
112             throw new ClassCastException("Cannot compare " + getHttpDataType() +
113                     " with " + other.getHttpDataType());
114         }
115         return compareTo((Attribute) other);
116     }
117 
118     public int compareTo(Attribute o) {
119         return getName().compareToIgnoreCase(o.getName());
120     }
121 
122     @Override
123     public String toString() {
124         return getName() + '=' + getValue();
125     }
126 
127     @Override
128     public Attribute copy() {
129         final ByteBuf content = content();
130         return replace(content != null ? content.copy() : null);
131     }
132 
133     @Override
134     public Attribute duplicate() {
135         final ByteBuf content = content();
136         return replace(content != null ? content.duplicate() : null);
137     }
138 
139     @Override
140     public Attribute retainedDuplicate() {
141         ByteBuf content = content();
142         if (content != null) {
143             content = content.retainedDuplicate();
144             boolean success = false;
145             try {
146                 Attribute duplicate = replace(content);
147                 success = true;
148                 return duplicate;
149             } finally {
150                 if (!success) {
151                     content.release();
152                 }
153             }
154         } else {
155             return replace(null);
156         }
157     }
158 
159     @Override
160     public Attribute replace(ByteBuf content) {
161         MemoryAttribute attr = new MemoryAttribute(getName());
162         attr.setCharset(getCharset());
163         if (content != null) {
164             try {
165                 attr.setContent(content);
166             } catch (IOException e) {
167                 throw new ChannelException(e);
168             }
169         }
170         attr.setCompleted(isCompleted());
171         return attr;
172     }
173 
174     @Override
175     public Attribute retain() {
176         super.retain();
177         return this;
178     }
179 
180     @Override
181     public Attribute retain(int increment) {
182         super.retain(increment);
183         return this;
184     }
185 
186     @Override
187     public Attribute touch() {
188         super.touch();
189         return this;
190     }
191 
192     @Override
193     public Attribute touch(Object hint) {
194         super.touch(hint);
195         return this;
196     }
197 }