Skip to content

Commit a368b77

Browse files
franz1981vietj
authored andcommitted
Save intermediate text encoded buffer copies
1 parent 23ca78a commit a368b77

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/io/vertx/core/http/impl/WebSocketImplBase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.net.ssl.SSLPeerUnverifiedException;
4545
import javax.net.ssl.SSLSession;
4646
import javax.security.cert.X509Certificate;
47+
import java.nio.charset.StandardCharsets;
4748
import java.security.cert.Certificate;
4849
import java.util.List;
4950
import java.util.UUID;
@@ -308,7 +309,13 @@ public final S writeBinaryMessage(Buffer data, Handler<AsyncResult<Void>> handle
308309

309310
@Override
310311
public Future<Void> writeTextMessage(String text) {
311-
return writePartialMessage(WebSocketFrameType.TEXT, Buffer.buffer(text), 0);
312+
byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
313+
boolean isFinal = utf8Bytes.length <= maxWebSocketFrameSize;
314+
if (isFinal) {
315+
return writeFrame(new WebSocketFrameImpl(WebSocketFrameType.TEXT, utf8Bytes, true));
316+
}
317+
// we could save to copy the byte[] if the unsafe heap version of Netty ByteBuf could expose wrapping byte[] directly
318+
return writePartialMessage(WebSocketFrameType.TEXT, Buffer.buffer(utf8Bytes), 0);
312319
}
313320

314321
@Override

src/main/java/io/vertx/core/http/impl/ws/WebSocketFrameImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public WebSocketFrameImpl(String textData) {
7979
this(textData, true);
8080
}
8181

82+
/**
83+
* Creates a new raw frame from with the specified encoded content.
84+
*/
85+
public WebSocketFrameImpl(WebSocketFrameType type, byte[] utf8TextData, boolean isFinalFrame) {
86+
this.type = type;
87+
this.isFinalFrame = isFinalFrame;
88+
this.binaryData = Unpooled.wrappedBuffer(utf8TextData);
89+
}
90+
8291
/**
8392
* Creates a new text frame from with the specified string.
8493
*/

0 commit comments

Comments
 (0)