3434import org .openqa .selenium .remote .http .HttpRequest ;
3535import org .openqa .selenium .remote .http .HttpResponse ;
3636
37- import java .io .BufferedInputStream ;
3837import java .io .IOException ;
3938import java .io .InputStream ;
4039import java .io .OutputStreamWriter ;
4342import java .util .Map ;
4443import java .util .Objects ;
4544import java .util .function .Function ;
45+ import java .util .function .Supplier ;
4646import java .util .logging .Level ;
4747import java .util .logging .Logger ;
4848import 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