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