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 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 }