3333import org .elasticsearch .common .UUIDs ;
3434import org .elasticsearch .common .blobstore .BlobContainer ;
3535import org .elasticsearch .common .blobstore .BlobPath ;
36+ import org .elasticsearch .common .bytes .BytesArray ;
3637import org .elasticsearch .common .bytes .BytesReference ;
3738import org .elasticsearch .common .collect .Tuple ;
3839import org .elasticsearch .common .io .Streams ;
5253import org .junit .Before ;
5354import org .threeten .bp .Duration ;
5455
55- import java .io .ByteArrayOutputStream ;
5656import java .io .IOException ;
5757import java .io .InputStream ;
5858import java .net .InetAddress ;
@@ -323,14 +323,16 @@ public void testWriteLargeBlob() throws IOException {
323323 logger .debug ("starting with resumable upload id [{}]" , sessionUploadId .get ());
324324
325325 httpServer .createContext ("/upload/storage/v1/b/bucket/o" , safeHandler (exchange -> {
326+ final BytesReference requestBody = Streams .readFully (exchange .getRequestBody ());
327+
326328 final Map <String , String > params = new HashMap <>();
327329 RestUtils .decodeQueryString (exchange .getRequestURI ().getQuery (), 0 , params );
328330 assertThat (params .get ("uploadType" ), equalTo ("resumable" ));
329331
330332 if ("POST" .equals (exchange .getRequestMethod ())) {
331333 assertThat (params .get ("name" ), equalTo ("write_large_blob" ));
332334 if (countInits .decrementAndGet () <= 0 ) {
333- byte [] response = Streams . readFully ( exchange . getRequestBody ()) .utf8ToString ().getBytes (UTF_8 );
335+ byte [] response = requestBody .utf8ToString ().getBytes (UTF_8 );
334336 exchange .getResponseHeaders ().add ("Content-Type" , "application/json" );
335337 exchange .getResponseHeaders ().add ("Location" , httpServerUrl () +
336338 "/upload/storage/v1/b/bucket/o?uploadType=resumable&upload_id=" + sessionUploadId .get ());
@@ -348,7 +350,6 @@ public void testWriteLargeBlob() throws IOException {
348350 if (uploadId .equals (sessionUploadId .get ()) == false ) {
349351 logger .debug ("session id [{}] is gone" , uploadId );
350352 assertThat (wrongChunk , greaterThan (0 ));
351- Streams .readFully (exchange .getRequestBody ());
352353 exchange .sendResponseHeaders (HttpStatus .SC_GONE , -1 );
353354 return ;
354355 }
@@ -367,7 +368,6 @@ public void testWriteLargeBlob() throws IOException {
367368 countInits .set (nbErrors );
368369 countUploads .set (nbErrors * totalChunks );
369370
370- Streams .readFully (exchange .getRequestBody ());
371371 exchange .sendResponseHeaders (HttpStatus .SC_GONE , -1 );
372372 return ;
373373 }
@@ -377,14 +377,12 @@ public void testWriteLargeBlob() throws IOException {
377377 assertTrue (Strings .hasLength (range ));
378378
379379 if (countUploads .decrementAndGet () % 2 == 0 ) {
380- final ByteArrayOutputStream requestBody = new ByteArrayOutputStream ();
381- final long bytesRead = Streams .copy (exchange .getRequestBody (), requestBody );
382- assertThat (Math .toIntExact (bytesRead ), anyOf (equalTo (defaultChunkSize ), equalTo (lastChunkSize )));
380+ assertThat (Math .toIntExact (requestBody .length ()), anyOf (equalTo (defaultChunkSize ), equalTo (lastChunkSize )));
383381
384382 final int rangeStart = getContentRangeStart (range );
385383 final int rangeEnd = getContentRangeEnd (range );
386- assertThat (rangeEnd + 1 - rangeStart , equalTo (Math .toIntExact (bytesRead )));
387- assertArrayEquals ( Arrays . copyOfRange (data , rangeStart , rangeEnd + 1 ), requestBody . toByteArray ( ));
384+ assertThat (rangeEnd + 1 - rangeStart , equalTo (Math .toIntExact (requestBody . length () )));
385+ assertThat ( new BytesArray (data , rangeStart , rangeEnd - rangeStart + 1 ), is ( requestBody ));
388386
389387 final Integer limit = getContentRangeLimit (range );
390388 if (limit != null ) {
@@ -399,8 +397,6 @@ public void testWriteLargeBlob() throws IOException {
399397 }
400398 }
401399
402- // read all the request body, otherwise the SDK client throws a non-retryable StorageException
403- Streams .readFully (exchange .getRequestBody ());
404400 if (randomBoolean ()) {
405401 exchange .sendResponseHeaders (HttpStatus .SC_INTERNAL_SERVER_ERROR , -1 );
406402 }
0 commit comments