Skip to content

Commit

Permalink
Save intermediate text encoded buffer copies
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored and vietj committed Nov 18, 2024
1 parent 21498ca commit 4c38e2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 4c38e2d

Please sign in to comment.