1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
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
85
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 }