From 964a4dc30b87178e3836734258e56affa2ca786c Mon Sep 17 00:00:00 2001 From: franz1981 Date: Thu, 10 Oct 2024 09:57:25 +0200 Subject: [PATCH] Save intermediate text encoded buffer copies --- .../java/io/vertx/core/http/impl/WebSocketImplBase.java | 9 ++++++++- .../io/vertx/core/http/impl/ws/WebSocketFrameImpl.java | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/WebSocketImplBase.java b/vertx-core/src/main/java/io/vertx/core/http/impl/WebSocketImplBase.java index 9ca0f33de7f..97af3f0be88 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/impl/WebSocketImplBase.java +++ b/vertx-core/src/main/java/io/vertx/core/http/impl/WebSocketImplBase.java @@ -40,6 +40,7 @@ import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; +import java.nio.charset.StandardCharsets; import java.security.cert.Certificate; import java.util.List; import java.util.UUID; @@ -239,7 +240,13 @@ public Future writeBinaryMessage(Buffer data) { @Override public Future writeTextMessage(String text) { - return writePartialMessage(WebSocketFrameType.TEXT, Buffer.buffer(text), 0); + byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8); + boolean isFinal = utf8Bytes.length <= maxWebSocketFrameSize; + if (isFinal) { + return writeFrame(new WebSocketFrameImpl(WebSocketFrameType.TEXT, utf8Bytes, true)); + } + // we could save to copy the byte[] if the unsafe heap version of Netty ByteBuf could expose wrapping byte[] directly + return writePartialMessage(WebSocketFrameType.TEXT, Buffer.buffer(utf8Bytes), 0); } @Override diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/ws/WebSocketFrameImpl.java b/vertx-core/src/main/java/io/vertx/core/http/impl/ws/WebSocketFrameImpl.java index 2d4a01c28e6..7c71d06f0d2 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/impl/ws/WebSocketFrameImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/http/impl/ws/WebSocketFrameImpl.java @@ -80,6 +80,15 @@ public WebSocketFrameImpl(String textData) { this(textData, true); } + /** + * Creates a new raw frame from with the specified encoded content. + */ + public WebSocketFrameImpl(WebSocketFrameType type, byte[] utf8TextData, boolean isFinalFrame) { + this.type = type; + this.isFinalFrame = isFinalFrame; + this.binaryData = Unpooled.wrappedBuffer(utf8TextData); + } + /** * Creates a new text frame from with the specified string. */