Skip to content

Commit

Permalink
[java] respect the last flag for binary data (#11108)
Browse files Browse the repository at this point in the history
respect the last flag for binary data

Respect the last flag for binary data to ensure the listener will get a
complete message and not only a partial message.

Co-authored-by: Diego Molina <[email protected]>

[skip ci]
  • Loading branch information
joerg1985 authored Oct 12, 2022
1 parent f777911 commit 4cdcffe
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
import org.openqa.selenium.remote.http.TextMessage;
import org.openqa.selenium.remote.http.WebSocket;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
Expand Down Expand Up @@ -127,6 +126,7 @@ public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
uri,
new java.net.http.WebSocket.Listener() {
final StringBuilder builder = new StringBuilder();
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

@Override
public CompletionStage<?> onText(java.net.http.WebSocket webSocket, CharSequence data, boolean last) {
Expand All @@ -145,11 +145,21 @@ public CompletionStage<?> onText(java.net.http.WebSocket webSocket, CharSequence

@Override
public CompletionStage<?> onBinary(java.net.http.WebSocket webSocket, ByteBuffer data, boolean last) {
LOG.fine("Binary data received.");
byte[] ary = new byte[data.remaining()];
data.get(ary, 0, ary.length);
LOG.fine("Binary data received. Appending data");
byte[] ary = new byte[8192];

listener.onBinary(ary);
while (data.hasRemaining()) {
int n = Math.min(ary.length, data.remaining());
data.get(ary, 0, n);
buffer.write(ary, 0, n);
}

if (last) {
LOG.fine("Final part of binary data received. Calling listener with "
+ buffer.size() + " bytes of data");
listener.onBinary(buffer.toByteArray());
buffer.reset();
}
webSocket.request(1);
return null;
}
Expand Down

0 comments on commit 4cdcffe

Please sign in to comment.