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  
17  package io.netty.buffer;
18  
19  import io.netty.util.ResourceLeak;
20  
21  import java.nio.ByteOrder;
22  
23  final class SimpleLeakAwareByteBuf extends WrappedByteBuf {
24  
25      private final ResourceLeak leak;
26  
27      SimpleLeakAwareByteBuf(ByteBuf buf, ResourceLeak leak) {
28          super(buf);
29          this.leak = leak;
30      }
31  
32      @Override
33      public ByteBuf touch() {
34          return this;
35      }
36  
37      @Override
38      public ByteBuf touch(Object hint) {
39          return this;
40      }
41  
42      @Override
43      public boolean release() {
44          boolean deallocated =  super.release();
45          if (deallocated) {
46              leak.close();
47          }
48          return deallocated;
49      }
50  
51      @Override
52      public boolean release(int decrement) {
53          boolean deallocated = super.release(decrement);
54          if (deallocated) {
55              leak.close();
56          }
57          return deallocated;
58      }
59  
60      @Override
61      public ByteBuf order(ByteOrder endianness) {
62          leak.record();
63          if (order() == endianness) {
64              return this;
65          } else {
66              return new SimpleLeakAwareByteBuf(super.order(endianness), leak);
67          }
68      }
69  
70      @Override
71      public ByteBuf slice() {
72          return new SimpleLeakAwareByteBuf(super.slice(), leak);
73      }
74  
75      @Override
76      public ByteBuf slice(int index, int length) {
77          return new SimpleLeakAwareByteBuf(super.slice(index, length), leak);
78      }
79  
80      @Override
81      public ByteBuf duplicate() {
82          return new SimpleLeakAwareByteBuf(super.duplicate(), leak);
83      }
84  
85      @Override
86      public ByteBuf readSlice(int length) {
87          return new SimpleLeakAwareByteBuf(super.readSlice(length), leak);
88      }
89  }