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 ByteBufHolder touch() {
73          data.touch();
74          return this;
75      }
76  
77      @Override
78      public ByteBufHolder touch(Object hint) {
79          data.touch(hint);
80          return this;
81      }
82  
83      @Override
84      public boolean release() {
85          return data.release();
86      }
87  
88      @Override
89      public boolean release(int decrement) {
90          return data.release(decrement);
91      }
92  
93      @Override
94      public String toString() {
95          return StringUtil.simpleClassName(this) + '(' + content().toString() + ')';
96      }
97  }