查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2016 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  
19  import io.netty.util.ResourceLeakTracker;
20  import io.netty.util.internal.ObjectUtil;
21  
22  import java.nio.ByteOrder;
23  
24  class SimpleLeakAwareCompositeByteBuf extends WrappedCompositeByteBuf {
25  
26      final ResourceLeakTracker<ByteBuf> leak;
27  
28      SimpleLeakAwareCompositeByteBuf(CompositeByteBuf wrapped, ResourceLeakTracker<ByteBuf> leak) {
29          super(wrapped);
30          this.leak = ObjectUtil.checkNotNull(leak, "leak");
31      }
32  
33      @Override
34      public boolean release() {
35          // Call unwrap() before just in case that super.release() will change the ByteBuf instance that is returned
36          // by unwrap().
37          ByteBuf unwrapped = unwrap();
38          if (super.release()) {
39              closeLeak(unwrapped);
40              return true;
41          }
42          return false;
43      }
44  
45      @Override
46      public boolean release(int decrement) {
47          // Call unwrap() before just in case that super.release() will change the ByteBuf instance that is returned
48          // by unwrap().
49          ByteBuf unwrapped = unwrap();
50          if (super.release(decrement)) {
51              closeLeak(unwrapped);
52              return true;
53          }
54          return false;
55      }
56  
57      private void closeLeak(ByteBuf trackedByteBuf) {
58          // Close the ResourceLeakTracker with the tracked ByteBuf as argument. This must be the same that was used when
59          // calling DefaultResourceLeak.track(...).
60          boolean closed = leak.close(trackedByteBuf);
61          assert closed;
62      }
63  
64      @Override
65      public ByteBuf order(ByteOrder endianness) {
66          if (order() == endianness) {
67              return this;
68          } else {
69              return newLeakAwareByteBuf(super.order(endianness));
70          }
71      }
72  
73      @Override
74      public ByteBuf slice() {
75          return newLeakAwareByteBuf(super.slice());
76      }
77  
78      @Override
79      public ByteBuf retainedSlice() {
80          return newLeakAwareByteBuf(super.retainedSlice());
81      }
82  
83      @Override
84      public ByteBuf slice(int index, int length) {
85          return newLeakAwareByteBuf(super.slice(index, length));
86      }
87  
88      @Override
89      public ByteBuf retainedSlice(int index, int length) {
90          return newLeakAwareByteBuf(super.retainedSlice(index, length));
91      }
92  
93      @Override
94      public ByteBuf duplicate() {
95          return newLeakAwareByteBuf(super.duplicate());
96      }
97  
98      @Override
99      public ByteBuf retainedDuplicate() {
100         return newLeakAwareByteBuf(super.retainedDuplicate());
101     }
102 
103     @Override
104     public ByteBuf readSlice(int length) {
105         return newLeakAwareByteBuf(super.readSlice(length));
106     }
107 
108     @Override
109     public ByteBuf readRetainedSlice(int length) {
110         return newLeakAwareByteBuf(super.readRetainedSlice(length));
111     }
112 
113     @Override
114     public ByteBuf asReadOnly() {
115         return newLeakAwareByteBuf(super.asReadOnly());
116     }
117 
118     private SimpleLeakAwareByteBuf newLeakAwareByteBuf(ByteBuf wrapped) {
119         return newLeakAwareByteBuf(wrapped, unwrap(), leak);
120     }
121 
122     protected SimpleLeakAwareByteBuf newLeakAwareByteBuf(
123             ByteBuf wrapped, ByteBuf trackedByteBuf, ResourceLeakTracker<ByteBuf> leakTracker) {
124         return new SimpleLeakAwareByteBuf(wrapped, trackedByteBuf, leakTracker);
125     }
126 }