Repository: netty/netty
File: handler/src/main/java/io/netty/handler/ssl/SslHandlerCoalescingBufferQueue.java (Java)

Code before:

/**
 * Each call to SSL_write will introduce about ~100 bytes of overhead. This coalescing queue attempts to increase
 * goodput by aggregating the plaintext in chunks of {@link #wrapDataSize}. If many small chunks are written
 * this can increase goodput, decrease the amount of calls to SSL_write, and decrease overall encryption operations.
 */
abstract class SslHandlerCoalescingBufferQueue extends AbstractCoalescingBufferQueue {

    private final boolean wantsDirectBuffer;

    SslHandlerCoalescingBufferQueue(Channel channel, int initSize, boolean wantsDirectBuffer) {
        super(channel, initSize);
        this.wantsDirectBuffer = wantsDirectBuffer;
    }

    protected abstract int wrapDataSize();

    @Override
    protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
        return attemptCopyToCumulation(cumulation, next, wrapDataSize()) ? cumulation :
                copyAndCompose(alloc, cumulation, next);
    }

    @Override
    protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) {
        ByteBuf newFirst;
        if (wantsDirectBuffer) {
            newFirst = allocator.directBuffer(bufferSize);
        } else {
            newFirst = allocator.heapBuffer(bufferSize);
        }
        try {
            newFirst.writeBytes(first);
        } catch (Throwable cause) {
            newFirst.release();
            PlatformDependent.throwException(cause);
        }
        assert !first.isReadable();
        first.release();
        return newFirst;
    }

    @Override
    protected ByteBuf removeEmptyValue() {
        return null;
    }

    private static boolean attemptCopyToCumulation(ByteBuf cumulation, ByteBuf next, int wrapDataSize) {
        final int inReadableBytes = next.readableBytes();
        // Nothing to copy so just release the buffer.
        if (inReadableBytes == 0) {

Review comment (suggestion):
```suggestion
        final ByteBuf newFirst;
```

Code after:

/**
 * Each call to SSL_write will introduce about ~100 bytes of overhead. This coalescing queue attempts to increase
 * goodput by aggregating the plaintext in chunks of {@link #wrapDataSize}. If many small chunks are written
 * this can increase goodput, decrease the amount of calls to SSL_write, and decrease overall encryption operations.
 */
abstract class SslHandlerCoalescingBufferQueue extends AbstractCoalescingBufferQueue {

    private final boolean wantsDirectBuffer;

    SslHandlerCoalescingBufferQueue(Channel channel, int initSize, boolean wantsDirectBuffer) {
        super(channel, initSize);
        this.wantsDirectBuffer = wantsDirectBuffer;
    }

    protected abstract int wrapDataSize();

    @Override
    protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
        return attemptCopyToCumulation(cumulation, next, wrapDataSize()) ? cumulation :
                copyAndCompose(alloc, cumulation, next);
    }

    @Override
    protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) {
        final ByteBuf newFirst;
        if (wantsDirectBuffer) {
            newFirst = allocator.directBuffer(bufferSize);
        } else {
            newFirst = allocator.heapBuffer(bufferSize);
        }
        try {
            newFirst.writeBytes(first);
        } catch (Throwable cause) {
            newFirst.release();
            PlatformDependent.throwException(cause);
        }
        assert !first.isReadable();
        first.release();
        return newFirst;
    }

    @Override
    protected ByteBuf removeEmptyValue() {
        return null;
    }

    private static boolean attemptCopyToCumulation(ByteBuf cumulation, ByteBuf next, int wrapDataSize) {
        final int inReadableBytes = next.readableBytes();
        // Nothing to copy so just release the buffer.
        if (inReadableBytes == 0) {