5353import java .nio .file .StandardOpenOption ;
5454import java .time .Duration ;
5555import java .util .Collections ;
56+ import java .util .List ;
5657import java .util .UUID ;
5758import java .util .concurrent .CompletableFuture ;
5859import java .util .concurrent .ExecutionException ;
6566import static com .azure .core .CoreTestUtils .fillArray ;
6667import static org .junit .jupiter .api .Assertions .assertEquals ;
6768import static org .junit .jupiter .api .Assertions .assertFalse ;
69+ import static org .junit .jupiter .api .Assertions .assertNotSame ;
6870import static org .junit .jupiter .api .Assertions .assertTrue ;
6971
7072public class FluxUtilTest {
@@ -524,6 +526,15 @@ public void toFluxByteBufferEmitsPartialReadBeforeEndOfStream(boolean defaultAva
524526 }
525527
526528 private static InputStream withDefaultAvailable (InputStream inputStream , int maxReadSize ) {
529+ return withDefaultAvailable (inputStream , new int [] { maxReadSize });
530+ }
531+
532+ /*
533+ * Wraps the stream so that available() reports 0 and each read returns at most the next of readSizes, repeating
534+ * the last once exhausted.
535+ */
536+ private static InputStream withDefaultAvailable (InputStream inputStream , int [] readSizes ) {
537+ AtomicInteger readIndex = new AtomicInteger ();
527538 return new InputStream () {
528539 @ Override
529540 public int read () throws IOException {
@@ -532,6 +543,7 @@ public int read() throws IOException {
532543
533544 @ Override
534545 public int read (byte [] buffer , int offset , int length ) throws IOException {
546+ int maxReadSize = readSizes [Math .min (readIndex .getAndIncrement (), readSizes .length - 1 )];
535547 return inputStream .read (buffer , offset , Math .min (length , maxReadSize ));
536548 }
537549
@@ -542,6 +554,68 @@ public void close() throws IOException {
542554 };
543555 }
544556
557+ @ Test
558+ public void toFluxByteBufferPartialReadsAreRightSized () throws IOException {
559+ byte [] expected = new byte [100 ];
560+ fillArray (expected );
561+
562+ try (InputStream inputStream = withDefaultAvailable (new ByteArrayInputStream (expected ), 7 )) {
563+ List <ByteBuffer > buffers = FluxUtil .toFluxByteBuffer (inputStream , 64 ).collectList ().block ();
564+
565+ assertEquals (15 , buffers .size ());
566+ for (ByteBuffer buffer : buffers ) {
567+ // A consumer retaining partial reads must not retain a chunkSize array for each one.
568+ assertEquals (buffer .remaining (), buffer .array ().length );
569+ }
570+ assertArraysEqual (expected , collectBytes (buffers ));
571+ }
572+ }
573+
574+ @ Test
575+ public void toFluxByteBufferMixedPartialAndFullReads () throws IOException {
576+ byte [] expected = new byte [20 ];
577+ fillArray (expected );
578+
579+ // Partial, full, partial, full, then the 2 remaining bytes, all read into the same reused array.
580+ try (InputStream inputStream
581+ = withDefaultAvailable (new ByteArrayInputStream (expected ), new int [] { 3 , 8 , 1 , 8 , 8 })) {
582+ List <ByteBuffer > buffers = FluxUtil .toFluxByteBuffer (inputStream , 8 ).collectList ().block ();
583+
584+ int [] expectedSizes = { 3 , 8 , 1 , 8 };
585+ assertEquals (expectedSizes .length , buffers .size ());
586+ for (int i = 0 ; i < expectedSizes .length ; i ++) {
587+ assertEquals (expectedSizes [i ], buffers .get (i ).remaining ());
588+ assertEquals (expectedSizes [i ], buffers .get (i ).array ().length );
589+ }
590+ // Retained buffers must not share an array that a later read overwrites.
591+ assertArraysEqual (expected , collectBytes (buffers ));
592+ }
593+ }
594+
595+ @ Test
596+ public void toFluxByteBufferFullChunksAreIndependent () {
597+ byte [] expected = new byte [10 ];
598+ fillArray (expected );
599+
600+ List <ByteBuffer > buffers
601+ = FluxUtil .toFluxByteBuffer (new ByteArrayInputStream (expected ), 4 ).collectList ().block ();
602+
603+ assertEquals (3 , buffers .size ());
604+ assertEquals (4 , buffers .get (0 ).array ().length );
605+ assertEquals (4 , buffers .get (1 ).array ().length );
606+ assertEquals (2 , buffers .get (2 ).array ().length );
607+ assertNotSame (buffers .get (0 ).array (), buffers .get (1 ).array ());
608+ assertArraysEqual (expected , collectBytes (buffers ));
609+ }
610+
611+ private static byte [] collectBytes (List <ByteBuffer > buffers ) {
612+ ByteArrayOutputStream collected = new ByteArrayOutputStream ();
613+ for (ByteBuffer buffer : buffers ) {
614+ collected .write (buffer .array (), buffer .arrayOffset () + buffer .position (), buffer .remaining ());
615+ }
616+ return collected .toByteArray ();
617+ }
618+
545619 @ Test
546620 public void toFluxByteBufferMultipleSubscriptions () {
547621 byte [] singleRead = new byte [4096 ];
0 commit comments