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, Charset charset) {
37          super(name, charset, 0);
38      }
39  
40      public MemoryAttribute(String name, String value) throws IOException {
41          this(name, value, HttpConstants.DEFAULT_CHARSET); // Attribute have no default size
42      }
43  
44      public MemoryAttribute(String name, String value, Charset charset) throws IOException {
45          super(name, charset, 0); // Attribute have no default size
46          setValue(value);
47      }
48  
49      @Override
50      public HttpDataType getHttpDataType() {
51          return HttpDataType.Attribute;
52      }
53  
54      @Override
55      public String getValue() {
56          return getByteBuf().toString(getCharset());
57      }
58  
59      @Override
60      public void setValue(String value) throws IOException {
61          if (value == null) {
62              throw new NullPointerException("value");
63          }
64          byte [] bytes = value.getBytes(getCharset());
65          checkSize(bytes.length);
66          ByteBuf buffer = wrappedBuffer(bytes);
67          if (definedSize > 0) {
68              definedSize = buffer.readableBytes();
69          }
70          setContent(buffer);
71      }
72  
73      @Override
74      public void addContent(ByteBuf buffer, boolean last) throws IOException {
75          int localsize = buffer.readableBytes();
76          checkSize(size + localsize);
77          if (definedSize > 0 && definedSize < size + localsize) {
78              definedSize = size + localsize;
79          }
80          super.addContent(buffer, last);
81      }
82  
83      @Override
84      public int hashCode() {
85          return getName().hashCode();
86      }
87  
88      @Override
89      public boolean equals(Object o) {
90          if (!(o instanceof Attribute)) {
91              return false;
92          }
93          Attribute attribute = (Attribute) o;
94          return getName().equalsIgnoreCase(attribute.getName());
95      }
96  
97      @Override
98      public int compareTo(InterfaceHttpData other) {
99          if (!(other instanceof Attribute)) {
100             throw new ClassCastException("Cannot compare " + getHttpDataType() +
101                     " with " + other.getHttpDataType());
102         }
103         return compareTo((Attribute) other);
104     }
105 
106     public int compareTo(Attribute o) {
107         return getName().compareToIgnoreCase(o.getName());
108     }
109 
110     @Override
111     public String toString() {
112         return getName() + '=' + getValue();
113     }
114 
115     @Override
116     public Attribute copy() {
117         MemoryAttribute attr = new MemoryAttribute(getName());
118         attr.setCharset(getCharset());
119         ByteBuf content = content();
120         if (content != null) {
121             try {
122                 attr.setContent(content.copy());
123             } catch (IOException e) {
124                 throw new ChannelException(e);
125             }
126         }
127         return attr;
128     }
129 
130     @Override
131     public Attribute duplicate() {
132         MemoryAttribute attr = new MemoryAttribute(getName());
133         attr.setCharset(getCharset());
134         ByteBuf content = content();
135         if (content != null) {
136             try {
137                 attr.setContent(content.duplicate());
138             } catch (IOException e) {
139                 throw new ChannelException(e);
140             }
141         }
142         return attr;
143     }
144 
145     @Override
146     public Attribute retain() {
147         super.retain();
148         return this;
149     }
150 
151     @Override
152     public Attribute retain(int increment) {
153         super.retain(increment);
154         return this;
155     }
156 
157     @Override
158     public Attribute touch() {
159         super.touch();
160         return this;
161     }
162 
163     @Override
164     public Attribute touch(Object hint) {
165         super.touch(hint);
166         return this;
167     }
168 }