Source:
private void useCustomBufferSizeSelector(final Integer netReadBufSizeStart, final Integer netWriteBufSizeStart, final Integer appReadBufSizeStart) throws Exception { selector.close(); NetworkMetrics metrics = new NetworkMetrics(new MetricRegistry()); Time time = SystemTime.getInstance(); selector = new Selector(metrics, time, clientSSLFactory, 0) { @Override protected Transmission createTransmission(String connectionId, SelectionKey key, String hostname, int port, PortType portType, SSLFactory.Mode mode) throws IOException { AtomicReference<Integer> netReadBufSizeOverride = new AtomicReference<>(netReadBufSizeStart); AtomicReference<Integer> netWriteBufSizeOverride = new AtomicReference<>(netWriteBufSizeStart); AtomicReference<Integer> appReadBufSizeOverride = new AtomicReference<>(appReadBufSizeStart); return new SSLTransmission(clientSSLFactory, connectionId, (SocketChannel) key.channel(), key, hostname, port, time, metrics, mode) { @Override protected int netReadBufferSize() { boolean updateAllowed = netReadBuffer() != null && !netReadBuffer().hasRemaining(); return getAndUpdateSize(super.netReadBufferSize(), netReadBufSizeOverride, updateAllowed); } @Override protected int netWriteBufferSize() { return getAndUpdateSize(super.netWriteBufferSize(), netWriteBufSizeOverride, true); } @Override protected int appReadBufferSize() { return getAndUpdateSize(super.appReadBufferSize(), appReadBufSizeOverride, true); } private int getAndUpdateSize(int defaultSize, AtomicReference<Integer> sizeOverride, boolean updateAllowed) { return sizeOverride.get() == null ? defaultSize : sizeOverride.updateAndGet(i -> updateAllowed ? Math.min(i * 2, defaultSize) : i); } }; } }; }

Target:
private void useCustomBufferSizeSelector(final Integer netReadBufSizeStart, final Integer netWriteBufSizeStart, final Integer appReadBufSizeStart) throws Exception { selector.close(); NetworkMetrics metrics = new NetworkMetrics(new MetricRegistry()); Time time = SystemTime.getInstance(); selector = new Selector(metrics, time, clientSSLFactory, poolSize) { @Override protected Transmission createTransmission(String connectionId, SelectionKey key, String hostname, int port, PortType portType, SSLFactory.Mode mode) throws IOException { AtomicReference<Integer> netReadBufSizeOverride = new AtomicReference<>(netReadBufSizeStart); AtomicReference<Integer> netWriteBufSizeOverride = new AtomicReference<>(netWriteBufSizeStart); AtomicReference<Integer> appReadBufSizeOverride = new AtomicReference<>(appReadBufSizeStart); return new SSLTransmission(clientSSLFactory, connectionId, (SocketChannel) key.channel(), key, hostname, port, time, metrics, mode) { @Override protected int netReadBufferSize() { boolean updateAllowed = netReadBuffer() != null && !netReadBuffer().hasRemaining(); return getAndUpdateSize(super.netReadBufferSize(), netReadBufSizeOverride, updateAllowed); } @Override protected int netWriteBufferSize() { return getAndUpdateSize(super.netWriteBufferSize(), netWriteBufSizeOverride, true); } @Override protected int appReadBufferSize() { return getAndUpdateSize(super.appReadBufferSize(), appReadBufSizeOverride, true); } private int getAndUpdateSize(int defaultSize, AtomicReference<Integer> sizeOverride, boolean updateAllowed) { return sizeOverride.get() == null ? defaultSize : sizeOverride.updateAndGet(i -> updateAllowed ? Math.min(i * 2, defaultSize) : i); } }; } }; }