Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save intermediate text encoded buffer copies #5394

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -239,7 +240,13 @@ public Future<Void> writeBinaryMessage(Buffer data) {

@Override
public Future<Void> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Loading