diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollRecvByteAllocatorHandle.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollRecvByteAllocatorHandle.java index 7eba32009a..41d9887154 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollRecvByteAllocatorHandle.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollRecvByteAllocatorHandle.java @@ -15,25 +15,22 @@ */ package io.netty.channel.epoll; -import static java.util.Objects.requireNonNull; - import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelConfig; -import io.netty.channel.RecvByteBufAllocator; +import io.netty.channel.RecvByteBufAllocator.DelegatingHandle; +import io.netty.channel.RecvByteBufAllocator.ExtendedHandle; import io.netty.channel.unix.PreferredDirectByteBufAllocator; import io.netty.util.UncheckedBooleanSupplier; -class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle { +class EpollRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle { private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator = new PreferredDirectByteBufAllocator(); - private final RecvByteBufAllocator.ExtendedHandle delegate; private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead; private boolean isEdgeTriggered; private boolean receivedRdHup; - EpollRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) { - delegate = requireNonNull(handle, "handle"); + EpollRecvByteAllocatorHandle(ExtendedHandle handle) { + super(handle); } final void receivedRdHup() { @@ -70,57 +67,17 @@ class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandl public final ByteBuf allocate(ByteBufAllocator alloc) { // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI. preferredDirectByteBufAllocator.updateAllocator(alloc); - return delegate.allocate(preferredDirectByteBufAllocator); - } - - @Override - public final int guess() { - return delegate.guess(); - } - - @Override - public final void reset(ChannelConfig config) { - delegate.reset(config); - } - - @Override - public final void incMessagesRead(int numMessages) { - delegate.incMessagesRead(numMessages); - } - - @Override - public final void lastBytesRead(int bytes) { - delegate.lastBytesRead(bytes); - } - - @Override - public final int lastBytesRead() { - return delegate.lastBytesRead(); - } - - @Override - public final int attemptedBytesRead() { - return delegate.attemptedBytesRead(); - } - - @Override - public final void attemptedBytesRead(int bytes) { - delegate.attemptedBytesRead(bytes); - } - - @Override - public final void readComplete() { - delegate.readComplete(); + return delegate().allocate(preferredDirectByteBufAllocator); } @Override public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) { - return delegate.continueReading(maybeMoreDataSupplier); + return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier); } @Override public final boolean continueReading() { // We must override the supplier which determines if there maybe more data to read. - return delegate.continueReading(defaultMaybeMoreDataSupplier); + return continueReading(defaultMaybeMoreDataSupplier); } } diff --git a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueRecvByteAllocatorHandle.java b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueRecvByteAllocatorHandle.java index f4d77a66a5..e98fe7cf0e 100644 --- a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueRecvByteAllocatorHandle.java +++ b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueRecvByteAllocatorHandle.java @@ -18,42 +18,36 @@ package io.netty.channel.kqueue; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.channel.ChannelConfig; -import io.netty.channel.RecvByteBufAllocator; +import io.netty.channel.RecvByteBufAllocator.DelegatingHandle; +import io.netty.channel.RecvByteBufAllocator.ExtendedHandle; import io.netty.channel.unix.PreferredDirectByteBufAllocator; import io.netty.util.UncheckedBooleanSupplier; import static java.lang.Math.max; import static java.lang.Math.min; -import static java.util.Objects.requireNonNull; -final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle { +final class KQueueRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle { private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator = new PreferredDirectByteBufAllocator(); - private final RecvByteBufAllocator.ExtendedHandle delegate; private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead; private boolean overrideGuess; private boolean readEOF; private long numberBytesPending; - KQueueRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) { - delegate = requireNonNull(handle, "handle"); + KQueueRecvByteAllocatorHandle(ExtendedHandle handle) { + super(handle); } @Override public int guess() { - return overrideGuess ? guess0() : delegate.guess(); + return overrideGuess ? guess0() : delegate().guess(); } @Override public void reset(ChannelConfig config) { overrideGuess = ((KQueueChannelConfig) config).getRcvAllocTransportProvidesGuess(); - delegate.reset(config); - } - - @Override - public void incMessagesRead(int numMessages) { - delegate.incMessagesRead(numMessages); + delegate().reset(config); } @Override @@ -61,44 +55,24 @@ final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.Extend // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI. preferredDirectByteBufAllocator.updateAllocator(alloc); return overrideGuess ? preferredDirectByteBufAllocator.ioBuffer(guess0()) : - delegate.allocate(preferredDirectByteBufAllocator); + delegate().allocate(preferredDirectByteBufAllocator); } @Override public void lastBytesRead(int bytes) { numberBytesPending = bytes < 0 ? 0 : max(0, numberBytesPending - bytes); - delegate.lastBytesRead(bytes); - } - - @Override - public int lastBytesRead() { - return delegate.lastBytesRead(); - } - - @Override - public void attemptedBytesRead(int bytes) { - delegate.attemptedBytesRead(bytes); - } - - @Override - public int attemptedBytesRead() { - return delegate.attemptedBytesRead(); - } - - @Override - public void readComplete() { - delegate.readComplete(); + delegate().lastBytesRead(bytes); } @Override public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) { - return delegate.continueReading(maybeMoreDataSupplier); + return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier); } @Override public boolean continueReading() { // We must override the supplier which determines if there maybe more data to read. - return delegate.continueReading(defaultMaybeMoreDataSupplier); + return continueReading(defaultMaybeMoreDataSupplier); } void readEOF() {