34
34
import org .openqa .selenium .remote .http .HttpRequest ;
35
35
import org .openqa .selenium .remote .http .HttpResponse ;
36
36
37
- import java .io .BufferedInputStream ;
38
37
import java .io .IOException ;
39
38
import java .io .InputStream ;
40
39
import java .io .OutputStreamWriter ;
43
42
import java .util .Map ;
44
43
import java .util .Objects ;
45
44
import java .util .function .Function ;
45
+ import java .util .function .Supplier ;
46
46
import java .util .logging .Level ;
47
47
import java .util .logging .Logger ;
48
48
import java .util .stream .Collectors ;
@@ -91,21 +91,23 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept
91
91
92
92
public Either <SessionNotCreatedException , Result > createSession (HttpHandler client , NewSessionPayload payload ) throws IOException {
93
93
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 );
95
95
96
96
try (CountingOutputStream counter = new CountingOutputStream (os );
97
97
Writer writer = new OutputStreamWriter (counter , UTF_8 )) {
98
98
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 ());
105
107
}
106
108
}
107
109
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 ) {
109
111
// Create the http request and send it
110
112
HttpRequest request = new HttpRequest (HttpMethod .POST , "/session" );
111
113
@@ -117,7 +119,7 @@ private Either<SessionNotCreatedException, Result> createSession(HttpHandler cli
117
119
// session e.g. with profiles.
118
120
request .setHeader (CONTENT_LENGTH , String .valueOf (size ));
119
121
request .setHeader (CONTENT_TYPE , JSON_UTF_8 );
120
- request .setContent (() -> newSessionBlob );
122
+ request .setContent (contentSupplier );
121
123
122
124
response = client .execute (request );
123
125
long time = System .currentTimeMillis () - start ;
0 commit comments