Skip to content

Commit 558e0a0

Browse files
authored
[java] Return new input stream to allow multiple reads (#11249)
1 parent 5f4a05c commit 558e0a0

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

java/src/org/openqa/selenium/remote/ProtocolHandshake.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.openqa.selenium.remote.http.HttpRequest;
3535
import org.openqa.selenium.remote.http.HttpResponse;
3636

37-
import java.io.BufferedInputStream;
3837
import java.io.IOException;
3938
import java.io.InputStream;
4039
import java.io.OutputStreamWriter;
@@ -43,6 +42,7 @@
4342
import java.util.Map;
4443
import java.util.Objects;
4544
import java.util.function.Function;
45+
import java.util.function.Supplier;
4646
import java.util.logging.Level;
4747
import java.util.logging.Logger;
4848
import java.util.stream.Collectors;
@@ -91,21 +91,23 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept
9191

9292
public Either<SessionNotCreatedException, Result> createSession(HttpHandler client, NewSessionPayload payload) throws IOException {
9393
int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE);
94-
FileBackedOutputStream os = new FileBackedOutputStream(threshold);
94+
FileBackedOutputStream os = new FileBackedOutputStream(threshold, true);
9595

9696
try (CountingOutputStream counter = new CountingOutputStream(os);
9797
Writer writer = new OutputStreamWriter(counter, UTF_8)) {
9898
payload.writeTo(writer);
99-
100-
try (InputStream contentStream = os.asByteSource().openBufferedStream()) {
101-
return createSession(client, contentStream, counter.getCount());
102-
}
103-
} finally {
104-
os.reset();
99+
Supplier<InputStream> contentSupplier = () -> {
100+
try {
101+
return os.asByteSource().openBufferedStream();
102+
} catch (IOException e) {
103+
throw new RuntimeException(e);
104+
}
105+
};
106+
return createSession(client, contentSupplier, counter.getCount());
105107
}
106108
}
107109

108-
private Either<SessionNotCreatedException, Result> createSession(HttpHandler client, InputStream newSessionBlob, long size) {
110+
private Either<SessionNotCreatedException, Result> createSession(HttpHandler client, Supplier<InputStream> contentSupplier, long size) {
109111
// Create the http request and send it
110112
HttpRequest request = new HttpRequest(HttpMethod.POST, "/session");
111113

@@ -117,7 +119,7 @@ private Either<SessionNotCreatedException, Result> createSession(HttpHandler cli
117119
// session e.g. with profiles.
118120
request.setHeader(CONTENT_LENGTH, String.valueOf(size));
119121
request.setHeader(CONTENT_TYPE, JSON_UTF_8);
120-
request.setContent(() -> newSessionBlob);
122+
request.setContent(contentSupplier);
121123

122124
response = client.execute(request);
123125
long time = System.currentTimeMillis() - start;

0 commit comments

Comments
 (0)