View Javadoc
1   /*
2    * Copyright 2013 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.buffer;
17  
18  import io.netty.util.IllegalReferenceCountException;
19  import io.netty.util.internal.StringUtil;
20  
21  /**
22   * Default implementation of a {@link ByteBufHolder} that holds it's data in a {@link ByteBuf}.
23   *
24   */
25  public class DefaultByteBufHolder implements ByteBufHolder {
26  
27      private final ByteBuf data;
28  
29      public DefaultByteBufHolder(ByteBuf data) {
30          if (data == null) {
31              throw new NullPointerException("data");
32          }
33          this.data = data;
34      }
35  
36      @Override
37      public ByteBuf content() {
38          if (data.refCnt() <= 0) {
39              throw new IllegalReferenceCountException(data.refCnt());
40          }
41          return data;
42      }
43  
44      /**
45       * {@inheritDoc}
46       * <p>
47       * This method calls {@code replace(content().copy())} by default.
48       */
49      @Override
50      public ByteBufHolder copy() {
51          return replace(data.copy());
52      }
53  
54      /**
55       * {@inheritDoc}
56       * <p>
57       * This method calls {@code replace(content().duplicate())} by default.
58       */
59      @Override
60      public ByteBufHolder duplicate() {
61          return replace(data.duplicate());
62      }
63  
64      /**
65       * {@inheritDoc}
66       * <p>
67       * This method calls {@code replace(content().retainedDuplicate())} by default.
68       */
69      @Override
70      public ByteBufHolder retainedDuplicate() {
71          return replace(data.retainedDuplicate());
72      }
73  
74      /**
75       * {@inheritDoc}
76       * <p>
77       * Override this method to return a new instance of this object whose content is set to the specified
78       * {@code content}. The default implementation of {@link #copy()}, {@link #duplicate()} and
79       * {@link #retainedDuplicate()} invokes this method to create a copy.
80       */
81      @Override
82      public ByteBufHolder replace(ByteBuf content) {
83          return new DefaultByteBufHolder(content);
84      }
85  
86      @Override
87      public int refCnt() {
88          return data.refCnt();
89      }
90  
91      @Override
92      public ByteBufHolder retain() {
93          data.retain();
94          return this;
95      }
96  
97      @Override
98      public ByteBufHolder retain(int increment) {
99          data.retain(increment);
100         return this;
101     }
102 
103     @Override
104     public ByteBufHolder touch() {
105         data.touch();
106         return this;
107     }
108 
109     @Override
110     public ByteBufHolder touch(Object hint) {
111         data.touch(hint);
112         return this;
113     }
114 
115     @Override
116     public boolean release() {
117         return data.release();
118     }
119 
120     @Override
121     public boolean release(int decrement) {
122         return data.release(decrement);
123     }
124 
125     /**
126      * Return {@link ByteBuf#toString()} without checking the reference count first. This is useful to implement
127      * {@link #toString()}.
128      */
129     protected final String contentToString() {
130         return data.toString();
131     }
132 
133     @Override
134     public String toString() {
135         return StringUtil.simpleClassName(this) + '(' + contentToString() + ')';
136     }
137 
138     @Override
139     public boolean equals(Object o) {
140         if (this == o) {
141             return true;
142         }
143         if (o instanceof ByteBufHolder) {
144             return data.equals(((ByteBufHolder) o).content());
145         }
146         return false;
147     }
148 
149     @Override
150     public int hashCode() {
151         return data.hashCode();
152     }
153 }