1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.util.internal;
17
18 import io.netty.util.ReferenceCountUtil;
19 import io.netty.util.concurrent.Promise;
20 import io.netty.util.internal.ObjectPool.Handle;
21 import io.netty.util.internal.ObjectPool.ObjectCreator;
22
23
24
25
26 public final class PendingWrite {
27 private static final ObjectPool<PendingWrite> RECYCLER = ObjectPool.newPool(new ObjectCreator<PendingWrite>() {
28 @Override
29 public PendingWrite newObject(Handle<PendingWrite> handle) {
30 return new PendingWrite(handle);
31 }
32 });
33
34
35
36
37 public static PendingWrite newInstance(Object msg, Promise<Void> promise) {
38 PendingWrite pending = RECYCLER.get();
39 pending.msg = msg;
40 pending.promise = promise;
41 return pending;
42 }
43
44 private final Handle<PendingWrite> handle;
45 private Object msg;
46 private Promise<Void> promise;
47
48 private PendingWrite(Handle<PendingWrite> handle) {
49 this.handle = handle;
50 }
51
52
53
54
55 public boolean recycle() {
56 msg = null;
57 promise = null;
58 handle.recycle(this);
59 return true;
60 }
61
62
63
64
65 public boolean failAndRecycle(Throwable cause) {
66 ReferenceCountUtil.release(msg);
67 if (promise != null) {
68 promise.setFailure(cause);
69 }
70 return recycle();
71 }
72
73
74
75
76 public boolean successAndRecycle() {
77 if (promise != null) {
78 promise.setSuccess(null);
79 }
80 return recycle();
81 }
82
83 public Object msg() {
84 return msg;
85 }
86
87 public Promise<Void> promise() {
88 return promise;
89 }
90
91
92
93
94 public Promise<Void> recycleAndGet() {
95 Promise<Void> promise = this.promise;
96 recycle();
97 return promise;
98 }
99 }