1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.kqueue;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufAllocator;
20 import io.netty.channel.ChannelConfig;
21 import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
22 import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
23 import io.netty.channel.unix.PreferredDirectByteBufAllocator;
24 import io.netty.util.UncheckedBooleanSupplier;
25
26 import static java.lang.Math.max;
27 import static java.lang.Math.min;
28
29 final class KQueueRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
30 private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
31 new PreferredDirectByteBufAllocator();
32
33 private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = new UncheckedBooleanSupplier() {
34 @Override
35 public boolean get() {
36 return maybeMoreDataToRead();
37 }
38 };
39 private boolean overrideGuess;
40 private boolean readEOF;
41 private long numberBytesPending;
42
43 KQueueRecvByteAllocatorHandle(ExtendedHandle handle) {
44 super(handle);
45 }
46
47 @Override
48 public int guess() {
49 return overrideGuess ? guess0() : delegate().guess();
50 }
51
52 @Override
53 public void reset(ChannelConfig config) {
54 overrideGuess = ((KQueueChannelConfig) config).getRcvAllocTransportProvidesGuess();
55 delegate().reset(config);
56 }
57
58 @Override
59 public ByteBuf allocate(ByteBufAllocator alloc) {
60
61 preferredDirectByteBufAllocator.updateAllocator(alloc);
62 return overrideGuess ? preferredDirectByteBufAllocator.ioBuffer(guess0()) :
63 delegate().allocate(preferredDirectByteBufAllocator);
64 }
65
66 @Override
67 public void lastBytesRead(int bytes) {
68 numberBytesPending = bytes < 0 ? 0 : max(0, numberBytesPending - bytes);
69 delegate().lastBytesRead(bytes);
70 }
71
72 @Override
73 public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
74 return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
75 }
76
77 @Override
78 public boolean continueReading() {
79
80 return continueReading(defaultMaybeMoreDataSupplier);
81 }
82
83 void readEOF() {
84 readEOF = true;
85 }
86
87 boolean isReadEOF() {
88 return readEOF;
89 }
90
91 void numberBytesPending(long numberBytesPending) {
92 this.numberBytesPending = numberBytesPending;
93 }
94
95 boolean maybeMoreDataToRead() {
96
97
98
99
100
101
102
103
104
105
106 return numberBytesPending != 0;
107 }
108
109 private int guess0() {
110 return (int) min(numberBytesPending, Integer.MAX_VALUE);
111 }
112 }