From 16b64b3b7b67e29dfbe5c46e1c0db0b832f48de2 Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Wed, 3 Jul 2024 10:43:07 +0200 Subject: [PATCH] #11965 introduce EMPTY_WRITABLE_BUFFER constant and use it everywhere fill() is called with an empty buffer Signed-off-by: Ludovic Orban --- .../org/eclipse/jetty/client/ssl/SslConnectionTest.java | 4 ++-- .../org/eclipse/jetty/io/NegotiatingClientConnection.java | 4 +--- .../main/java/org/eclipse/jetty/io/ssl/SslConnection.java | 6 +++--- .../org/eclipse/jetty/quic/common/QuicStreamEndPoint.java | 3 +-- .../eclipse/jetty/server/NegotiatingServerConnection.java | 5 ++--- .../src/main/java/org/eclipse/jetty/util/BufferUtil.java | 3 ++- 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java index dae684b80d91..df8c0f870884 100644 --- a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java +++ b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java @@ -62,7 +62,7 @@ public void onFillable() // There are no bytes in the endPoint, so we fill zero. // However, this will trigger state changes in SSLEngine // that will later cause it to throw ISE("Internal error"). - sslEndPoint.fill(BufferUtil.EMPTY_BUFFER); + sslEndPoint.fill(BufferUtil.EMPTY_WRITABLE_BUFFER); // Close the connection before filling. sslEndPoint.shutdownOutput(); @@ -76,6 +76,6 @@ public void onFillable() // We want SSLHandshakeException to be thrown instead, because it is // handled better (it is an IOException) by the Connection code that // reads from the EndPoint. - assertThrows(SSLHandshakeException.class, () -> sslEndPoint.fill(BufferUtil.EMPTY_BUFFER)); + assertThrows(SSLHandshakeException.class, () -> sslEndPoint.fill(BufferUtil.EMPTY_WRITABLE_BUFFER)); } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NegotiatingClientConnection.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NegotiatingClientConnection.java index 1d35e7cb1328..36cf67c75382 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NegotiatingClientConnection.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NegotiatingClientConnection.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.io; import java.io.IOException; -import java.nio.ByteBuffer; import java.util.Map; import java.util.concurrent.Executor; import javax.net.ssl.SSLEngine; @@ -26,7 +25,6 @@ public abstract class NegotiatingClientConnection extends AbstractConnection { private static final Logger LOG = LoggerFactory.getLogger(NegotiatingClientConnection.class); - private static final ByteBuffer EMPTY_WRITABLE_BUFFER = ByteBuffer.allocate(0); private final SSLEngine engine; private final ClientConnectionFactory connectionFactory; @@ -101,7 +99,7 @@ private int fill() { try { - return getEndPoint().fill(EMPTY_WRITABLE_BUFFER); + return getEndPoint().fill(BufferUtil.EMPTY_WRITABLE_BUFFER); } catch (IOException x) { diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java index 39f74d7dde11..60cad8e9dbfd 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java @@ -580,7 +580,7 @@ protected void onFillable() waitingForFill = _flushState == FlushState.WAIT_FOR_FILL; } if (waitingForFill) - fill(BufferUtil.EMPTY_BUFFER); + fill(BufferUtil.EMPTY_WRITABLE_BUFFER); } } catch (Throwable e) @@ -1080,7 +1080,7 @@ public boolean flush(ByteBuffer... appOuts) throws IOException break; if (_fillState == FillState.IDLE) { - int filled = fill(BufferUtil.EMPTY_BUFFER); + int filled = fill(BufferUtil.EMPTY_WRITABLE_BUFFER); if (_sslEngine.getHandshakeStatus() != status) continue; if (filled < 0) @@ -1270,7 +1270,7 @@ protected void onIncompleteFlush() // Try filling ourselves try { - int filled = fill(BufferUtil.EMPTY_BUFFER); + int filled = fill(BufferUtil.EMPTY_WRITABLE_BUFFER); // If this changed the status, let's try again if (_sslEngine.getHandshakeStatus() != status) continue; diff --git a/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicStreamEndPoint.java b/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicStreamEndPoint.java index 227af953034c..7986d4b2a6b1 100644 --- a/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicStreamEndPoint.java +++ b/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicStreamEndPoint.java @@ -42,7 +42,6 @@ public class QuicStreamEndPoint extends AbstractEndPoint { private static final Logger LOG = LoggerFactory.getLogger(QuicStreamEndPoint.class); private static final ByteBuffer LAST_FLAG = ByteBuffer.allocate(0); - private static final ByteBuffer EMPTY_WRITABLE_BUFFER = ByteBuffer.allocate(0); private final QuicSession session; private final long streamId; @@ -272,7 +271,7 @@ public boolean onReadable() // Check if the stream was finished normally. try { - fill(EMPTY_WRITABLE_BUFFER); + fill(BufferUtil.EMPTY_WRITABLE_BUFFER); } catch (EOFException x) { diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NegotiatingServerConnection.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NegotiatingServerConnection.java index 038e02d48c35..3292ce948f37 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NegotiatingServerConnection.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NegotiatingServerConnection.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.server; import java.io.IOException; -import java.nio.ByteBuffer; import java.util.List; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngineResult; @@ -22,13 +21,13 @@ import org.eclipse.jetty.io.AbstractConnection; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class NegotiatingServerConnection extends AbstractConnection { private static final Logger LOG = LoggerFactory.getLogger(NegotiatingServerConnection.class); - private static final ByteBuffer EMPTY_WRITABLE_BUFFER = ByteBuffer.allocate(0); public interface CipherDiscriminator { @@ -145,7 +144,7 @@ private int fill() { try { - return getEndPoint().fill(EMPTY_WRITABLE_BUFFER); + return getEndPoint().fill(BufferUtil.EMPTY_WRITABLE_BUFFER); } catch (IOException x) { diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index 82d39988bcd4..96f61e524826 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -107,7 +107,8 @@ public class BufferUtil }; public static final byte[] EMPTY_BYTES = new byte[0]; - public static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(EMPTY_BYTES).asReadOnlyBuffer(); + public static final ByteBuffer EMPTY_WRITABLE_BUFFER = ByteBuffer.wrap(EMPTY_BYTES); + public static final ByteBuffer EMPTY_BUFFER = EMPTY_WRITABLE_BUFFER.asReadOnlyBuffer(); /** * Allocate ByteBuffer in flush mode.