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      @Override
45      public ByteBufHolder copy() {
46          return new DefaultByteBufHolder(data.copy());
47      }
48  
49      @Override
50      public ByteBufHolder duplicate() {
51          return new DefaultByteBufHolder(data.duplicate());
52      }
53  
54      @Override
55      public int refCnt() {
56          return data.refCnt();
57      }
58  
59      @Override
60      public ByteBufHolder retain() {
61          data.retain();
62          return this;
63      }
64  
65      @Override
66      public ByteBufHolder retain(int increment) {
67          data.retain(increment);
68          return this;
69      }
70  
71      @Override
72      public boolean release() {
73          return data.release();
74      }
75  
76      @Override
77      public boolean release(int decrement) {
78          return data.release(decrement);
79      }
80  
81      /**
82       * Return {@link ByteBuf#toString()} without checking the reference count first. This is useful to implement
83       * {@link #toString()}.
84       */
85      protected final String contentToString() {
86          return data.toString();
87      }
88  
89      @Override
90      public String toString() {
91          return StringUtil.simpleClassName(this) + '(' + contentToString() + ')';
92      }
93  
94      @Override
95      public boolean equals(Object o) {
96          if (this == o) {
97              return true;
98          }
99          if (o instanceof ByteBufHolder) {
100             return data.equals(((ByteBufHolder) o).content());
101         }
102         return false;
103     }
104 
105     @Override
106     public int hashCode() {
107         return data.hashCode();
108     }
109 }