@@ -81,7 +81,7 @@ public void setUp() {
8181 @ Override
8282 void addInput (ByteBufAllocator alloc , CompositeByteBuf composite , ByteBuf in ) {
8383 // To limit the testing scope to NettyAdaptiveCumulator.cumulate(), always compose
84- composite .addFlattenedComponents (true , in );
84+ composite .addComponent (true , in );
8585 }
8686 };
8787
@@ -208,8 +208,8 @@ public void setUp() {
208208 in = ByteBufUtil .writeAscii (alloc , inData );
209209 tail = ByteBufUtil .writeAscii (alloc , tailData );
210210 composite = alloc .compositeBuffer (Integer .MAX_VALUE );
211- // Note that addFlattenedComponents () will not add a new component when tail is not readable.
212- composite .addFlattenedComponents (true , tail );
211+ // Note that addComponent () will not add a new component when tail is not readable.
212+ composite .addComponent (true , tail );
213213 }
214214
215215 @ After
@@ -345,7 +345,7 @@ public void mergeWithCompositeTail_tailExpandable_write() {
345345 assertThat (in .readableBytes ()).isAtMost (tail .writableBytes ());
346346
347347 // All fits, so tail capacity must stay the same.
348- composite .addFlattenedComponents (true , tail );
348+ composite .addComponent (true , tail );
349349 assertTailExpanded (EXPECTED_TAIL_DATA , fitCapacity );
350350 }
351351
@@ -362,7 +362,7 @@ public void mergeWithCompositeTail_tailExpandable_fastWrite() {
362362 alloc .calculateNewCapacity (EXPECTED_TAIL_DATA .length (), Integer .MAX_VALUE );
363363
364364 // Tail capacity is extended to its fast capacity.
365- composite .addFlattenedComponents (true , tail );
365+ composite .addComponent (true , tail );
366366 assertTailExpanded (EXPECTED_TAIL_DATA , tailFastCapacity );
367367 }
368368
@@ -372,7 +372,7 @@ public void mergeWithCompositeTail_tailExpandable_reallocateInMemory() {
372372 @ SuppressWarnings ("InlineMeInliner" ) // Requires Java 11
373373 String inSuffixOverFastBytes = Strings .repeat ("a" , tailFastCapacity + 1 );
374374 int newTailSize = tail .readableBytes () + inSuffixOverFastBytes .length ();
375- composite .addFlattenedComponents (true , tail );
375+ composite .addComponent (true , tail );
376376
377377 // Make input larger than tailFastCapacity
378378 in .writeCharSequence (inSuffixOverFastBytes , US_ASCII );
@@ -435,21 +435,21 @@ public void mergeWithCompositeTail_tailNotExpandable_maxCapacityReached() {
435435 @ SuppressWarnings ("InlineMeInliner" ) // Requires Java 11
436436 String tailSuffixFullCapacity = Strings .repeat ("a" , tail .maxWritableBytes ());
437437 tail .writeCharSequence (tailSuffixFullCapacity , US_ASCII );
438- composite .addFlattenedComponents (true , tail );
438+ composite .addComponent (true , tail );
439439 assertTailReplaced ();
440440 }
441441
442442 @ Test
443443 public void mergeWithCompositeTail_tailNotExpandable_shared () {
444444 tail .retain ();
445- composite .addFlattenedComponents (true , tail );
445+ composite .addComponent (true , tail );
446446 assertTailReplaced ();
447447 tail .release ();
448448 }
449449
450450 @ Test
451451 public void mergeWithCompositeTail_tailNotExpandable_readOnly () {
452- composite .addFlattenedComponents (true , tail .asReadOnly ());
452+ composite .addComponent (true , tail .asReadOnly ());
453453 assertTailReplaced ();
454454 }
455455
@@ -527,8 +527,7 @@ public void mergeWithCompositeTail_tailExpandable_mergedReleaseOnThrow() {
527527 CompositeByteBuf compositeThrows = new CompositeByteBuf (alloc , false , Integer .MAX_VALUE ,
528528 tail ) {
529529 @ Override
530- public CompositeByteBuf addFlattenedComponents (boolean increaseWriterIndex ,
531- ByteBuf buffer ) {
530+ public CompositeByteBuf addComponent (boolean increaseWriterIndex , ByteBuf buffer ) {
532531 throw expectedError ;
533532 }
534533 };
@@ -561,8 +560,7 @@ public void mergeWithCompositeTail_tailNotExpandable_mergedReleaseOnThrow() {
561560 CompositeByteBuf compositeRo = new CompositeByteBuf (alloc , false , Integer .MAX_VALUE ,
562561 tail .asReadOnly ()) {
563562 @ Override
564- public CompositeByteBuf addFlattenedComponents (boolean increaseWriterIndex ,
565- ByteBuf buffer ) {
563+ public CompositeByteBuf addComponent (boolean increaseWriterIndex , ByteBuf buffer ) {
566564 throw expectedError ;
567565 }
568566 };
@@ -616,14 +614,14 @@ public void mergeWithCompositeTail_outOfSyncComposite() {
616614 ByteBuf buf = alloc .buffer (32 ).writeBytes ("---01234" .getBytes (US_ASCII ));
617615
618616 // Start with a regular cumulation and add the buf as the only component.
619- CompositeByteBuf composite1 = alloc .compositeBuffer (8 ).addFlattenedComponents (true , buf );
617+ CompositeByteBuf composite1 = alloc .compositeBuffer (8 ).addComponent (true , buf );
620618 // Read composite1 buf to the beginning of the numbers.
621619 assertThat (composite1 .readCharSequence (3 , US_ASCII ).toString ()).isEqualTo ("---" );
622620
623621 // Wrap composite1 into another cumulation. This is similar to
624622 // what NettyAdaptiveCumulator.cumulate() does in the case the cumulation has refCnt != 1.
625623 CompositeByteBuf composite2 =
626- alloc .compositeBuffer (8 ).addFlattenedComponents (true , composite1 );
624+ alloc .compositeBuffer (8 ).addComponent (true , composite1 );
627625 assertThat (composite2 .toString (US_ASCII )).isEqualTo ("01234" );
628626
629627 // The previous operation does not adjust the read indexes of the underlying buffers,
@@ -639,13 +637,27 @@ public void mergeWithCompositeTail_outOfSyncComposite() {
639637 CompositeByteBuf cumulation = (CompositeByteBuf ) cumulator .cumulate (alloc , composite2 ,
640638 ByteBufUtil .writeAscii (alloc , "56789" ));
641639 assertThat (cumulation .toString (US_ASCII )).isEqualTo ("0123456789" );
640+ }
641+
642+ @ Test
643+ public void mergeWithNonCompositeTail () {
644+ NettyAdaptiveCumulator cumulator = new NettyAdaptiveCumulator (1024 );
645+ ByteBufAllocator alloc = new PooledByteBufAllocator ();
646+ ByteBuf buf = alloc .buffer ().writeBytes ("tail" .getBytes (US_ASCII ));
647+ ByteBuf in = alloc .buffer ().writeBytes ("-012345" .getBytes (US_ASCII ));
648+
649+ CompositeByteBuf composite = alloc .compositeBuffer ().addComponent (true , buf );
642650
643- // Correctness check: we still have a single component, and this component is still the
644- // original underlying buffer.
645- assertThat (cumulation .numComponents ()).isEqualTo (1 );
646- // Replace '2' with '*', and '8' with '$'.
647- buf .setByte (5 , '*' ).setByte (11 , '$' );
648- assertThat (cumulation .toString (US_ASCII )).isEqualTo ("01*34567$9" );
651+ CompositeByteBuf cumulation = (CompositeByteBuf ) cumulator .cumulate (alloc , composite , in );
652+
653+ assertEquals ("tail-012345" , cumulation .toString (US_ASCII ));
654+ assertEquals (0 , in .refCnt ());
655+ assertEquals (1 , cumulation .numComponents ());
656+
657+ buf .setByte (2 , '*' ).setByte (7 , '$' );
658+ assertEquals ("ta*l-01$345" , cumulation .toString (US_ASCII ));
659+
660+ composite .release ();
649661 }
650662 }
651663}
0 commit comments