Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11ac5e9

Browse files
committedJun 9, 2025
improve write performance by by buffer out stream
1 parent 8c5766a commit 11ac5e9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
 

‎src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.io.OutputStream;
31+
import java.io.BufferedOutputStream;
3132
import java.lang.reflect.InvocationTargetException;
3233
import java.net.InetAddress;
3334
import java.net.InetSocketAddress;
@@ -850,17 +851,20 @@ public void run() {
850851
* @throws IOException if write or flush did not work
851852
*/
852853
private void runWriteData() throws IOException {
854+
BufferedOutputStream bufferedInputStream = new BufferedOutputStream(ostream);
853855
try {
854856
while (!Thread.interrupted()) {
855857
ByteBuffer buffer = engine.outQueue.take();
856-
ostream.write(buffer.array(), 0, buffer.limit());
857-
ostream.flush();
858+
bufferedInputStream.write(buffer.array(), 0, buffer.limit());
859+
if(engine.outQueue.isEmpty()) {
860+
bufferedInputStream.flush();
861+
}
858862
}
859863
} catch (InterruptedException e) {
860864
for (ByteBuffer buffer : engine.outQueue) {
861-
ostream.write(buffer.array(), 0, buffer.limit());
862-
ostream.flush();
865+
bufferedInputStream.write(buffer.array(), 0, buffer.limit());
863866
}
867+
bufferedInputStream.flush();
864868
Thread.currentThread().interrupt();
865869
}
866870
}

0 commit comments

Comments
 (0)
Please sign in to comment.