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    *   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.buffer;
17  
18  import io.netty.util.internal.ObjectUtil;
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          this.data = ObjectUtil.checkNotNull(data, "data");
31      }
32  
33      @Override
34      public ByteBuf content() {
35          return ByteBufUtil.ensureAccessible(data);
36      }
37  
38      /**
39       * {@inheritDoc}
40       * <p>
41       * This method calls {@code replace(content().copy())} by default.
42       */
43      @Override
44      public ByteBufHolder copy() {
45          return replace(data.copy());
46      }
47  
48      /**
49       * {@inheritDoc}
50       * <p>
51       * This method calls {@code replace(content().duplicate())} by default.
52       */
53      @Override
54      public ByteBufHolder duplicate() {
55          return replace(data.duplicate());
56      }
57  
58      /**
59       * {@inheritDoc}
60       * <p>
61       * This method calls {@code replace(content().retainedDuplicate())} by default.
62       */
63      @Override
64      public ByteBufHolder retainedDuplicate() {
65          return replace(data.retainedDuplicate());
66      }
67  
68      /**
69       * {@inheritDoc}
70       * <p>
71       * Override this method to return a new instance of this object whose content is set to the specified
72       * {@code content}. The default implementation of {@link #copy()}, {@link #duplicate()} and
73       * {@link #retainedDuplicate()} invokes this method to create a copy.
74       */
75      @Override
76      public ByteBufHolder replace(ByteBuf content) {
77          return new DefaultByteBufHolder(content);
78      }
79  
80      @Override
81      public int refCnt() {
82          return data.refCnt();
83      }
84  
85      @Override
86      public ByteBufHolder retain() {
87          data.retain();
88          return this;
89      }
90  
91      @Override
92      public ByteBufHolder retain(int increment) {
93          data.retain(increment);
94          return this;
95      }
96  
97      @Override
98      public ByteBufHolder touch() {
99          data.touch();
100         return this;
101     }
102 
103     @Override
104     public ByteBufHolder touch(Object hint) {
105         data.touch(hint);
106         return this;
107     }
108 
109     @Override
110     public boolean release() {
111         return data.release();
112     }
113 
114     @Override
115     public boolean release(int decrement) {
116         return data.release(decrement);
117     }
118 
119     /**
120      * Return {@link ByteBuf#toString()} without checking the reference count first. This is useful to implement
121      * {@link #toString()}.
122      */
123     protected final String contentToString() {
124         return data.toString();
125     }
126 
127     @Override
128     public String toString() {
129         return StringUtil.simpleClassName(this) + '(' + contentToString() + ')';
130     }
131 
132     /**
133      * This implementation of the {@code equals} operation is restricted to
134      * work only with instances of the same class. The reason for that is that
135      * Netty library already has a number of classes that extend {@link DefaultByteBufHolder} and
136      * override {@code equals} method with an additional comparison logic and we
137      * need the symmetric property of the {@code equals} operation to be preserved.
138      *
139      * @param   o   the reference object with which to compare.
140      * @return  {@code true} if this object is the same as the obj
141      *          argument; {@code false} otherwise.
142      */
143     @Override
144     public boolean equals(Object o) {
145         if (this == o) {
146             return true;
147         }
148         if (o != null && getClass() == o.getClass()) {
149             return data.equals(((DefaultByteBufHolder) o).data);
150         }
151         return false;
152     }
153 
154     @Override
155     public int hashCode() {
156         return data.hashCode();
157     }
158 }