查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.ResourceLeakDetector;
20  import io.netty.util.ResourceLeakTracker;
21  import io.netty.util.internal.ObjectUtil;
22  
23  import java.nio.ByteOrder;
24  
25  class SimpleLeakAwareByteBuf extends WrappedByteBuf {
26  
27      /**
28       * This object's is associated with the {@link ResourceLeakTracker}. When {@link ResourceLeakTracker#close(Object)}
29       * is called this object will be used as the argument. It is also assumed that this object is used when
30       * {@link ResourceLeakDetector#track(Object)} is called to create {@link #leak}.
31       */
32      private final ByteBuf trackedByteBuf;
33      final ResourceLeakTracker<ByteBuf> leak;
34  
35      SimpleLeakAwareByteBuf(ByteBuf wrapped, ByteBuf trackedByteBuf, ResourceLeakTracker<ByteBuf> leak) {
36          super(wrapped);
37          this.trackedByteBuf = ObjectUtil.checkNotNull(trackedByteBuf, "trackedByteBuf");
38          this.leak = ObjectUtil.checkNotNull(leak, "leak");
39      }
40  
41      SimpleLeakAwareByteBuf(ByteBuf wrapped, ResourceLeakTracker<ByteBuf> leak) {
42          this(wrapped, wrapped, leak);
43      }
44  
45      @Override
46      public ByteBuf slice() {
47          return newSharedLeakAwareByteBuf(super.slice());
48      }
49  
50      @Override
51      public ByteBuf slice(int index, int length) {
52          return newSharedLeakAwareByteBuf(super.slice(index, length));
53      }
54  
55      @Override
56      public ByteBuf duplicate() {
57          return newSharedLeakAwareByteBuf(super.duplicate());
58      }
59  
60      @Override
61      public ByteBuf readSlice(int length) {
62          return newSharedLeakAwareByteBuf(super.readSlice(length));
63      }
64  
65      @Override
66      public boolean release() {
67          if (super.release()) {
68              closeLeak();
69              return true;
70          }
71          return false;
72      }
73  
74      @Override
75      public boolean release(int decrement) {
76          if (super.release(decrement)) {
77              closeLeak();
78              return true;
79          }
80          return false;
81      }
82  
83      private void closeLeak() {
84          // Close the ResourceLeakTracker with the tracked ByteBuf as argument. This must be the same that was used when
85          // calling DefaultResourceLeak.track(...).
86          boolean closed = leak.close(trackedByteBuf);
87          assert closed;
88      }
89  
90      @Override
91      public ByteBuf order(ByteOrder endianness) {
92          if (order() == endianness) {
93              return this;
94          } else {
95              return newSharedLeakAwareByteBuf(super.order(endianness));
96          }
97      }
98  
99      private SimpleLeakAwareByteBuf newSharedLeakAwareByteBuf(
100             ByteBuf wrapped) {
101         return newLeakAwareByteBuf(wrapped, trackedByteBuf, leak);
102     }
103 
104     protected SimpleLeakAwareByteBuf newLeakAwareByteBuf(
105             ByteBuf buf, ByteBuf trackedByteBuf, ResourceLeakTracker<ByteBuf> leakTracker) {
106         return new SimpleLeakAwareByteBuf(buf, trackedByteBuf, leakTracker);
107     }
108 }