1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
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
48
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
59
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 slice(int index, int length) {
80 return newLeakAwareByteBuf(super.slice(index, length));
81 }
82
83 @Override
84 public ByteBuf duplicate() {
85 return newLeakAwareByteBuf(super.duplicate());
86 }
87
88 @Override
89 public ByteBuf readSlice(int length) {
90 return newLeakAwareByteBuf(super.readSlice(length));
91 }
92
93 private SimpleLeakAwareByteBuf newLeakAwareByteBuf(ByteBuf wrapped) {
94 return newLeakAwareByteBuf(wrapped, unwrap(), leak);
95 }
96
97 protected SimpleLeakAwareByteBuf newLeakAwareByteBuf(
98 ByteBuf wrapped, ByteBuf trackedByteBuf, ResourceLeakTracker<ByteBuf> leakTracker) {
99 return new SimpleLeakAwareByteBuf(wrapped, trackedByteBuf, leakTracker);
100 }
101 }