11/*
2- * Copyright 2002-2023 the original author or authors.
2+ * Copyright 2002-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -678,6 +678,38 @@ void toByteBufferDestination(DataBufferFactory bufferFactory) {
678678 void readableByteBuffers (DataBufferFactory bufferFactory ) throws IOException {
679679 super .bufferFactory = bufferFactory ;
680680
681+ DataBuffer dataBuffer = this .bufferFactory .allocateBuffer (3 );
682+ dataBuffer .write ("abc" .getBytes (StandardCharsets .UTF_8 ));
683+ dataBuffer .readPosition (1 );
684+ dataBuffer .writePosition (2 );
685+
686+
687+ byte [] result = new byte [1 ];
688+ try (var iterator = dataBuffer .readableByteBuffers ()) {
689+ assertThat (iterator ).hasNext ();
690+ int i = 0 ;
691+ while (iterator .hasNext ()) {
692+ ByteBuffer byteBuffer = iterator .next ();
693+ assertThat (byteBuffer .position ()).isEqualTo (0 );
694+ assertThat (byteBuffer .limit ()).isEqualTo (1 );
695+ assertThat (byteBuffer .capacity ()).isEqualTo (1 );
696+ assertThat (byteBuffer .remaining ()).isEqualTo (1 );
697+
698+ byteBuffer .get (result , i , 1 );
699+
700+ assertThat (iterator ).isExhausted ();
701+ }
702+ }
703+
704+ assertThat (result ).containsExactly ('b' );
705+
706+ release (dataBuffer );
707+ }
708+
709+ @ ParameterizedDataBufferAllocatingTest
710+ void readableByteBuffersJoined (DataBufferFactory bufferFactory ) {
711+ super .bufferFactory = bufferFactory ;
712+
681713 DataBuffer dataBuffer = this .bufferFactory .join (Arrays .asList (stringBuffer ("a" ),
682714 stringBuffer ("b" ), stringBuffer ("c" )));
683715
@@ -703,17 +735,26 @@ void readableByteBuffers(DataBufferFactory bufferFactory) throws IOException {
703735 void writableByteBuffers (DataBufferFactory bufferFactory ) {
704736 super .bufferFactory = bufferFactory ;
705737
706- DataBuffer dataBuffer = this .bufferFactory .allocateBuffer (1 );
738+ DataBuffer dataBuffer = this .bufferFactory .allocateBuffer (3 );
739+ dataBuffer .write ("ab" .getBytes (StandardCharsets .UTF_8 ));
740+ dataBuffer .readPosition (1 );
707741
708742 try (DataBuffer .ByteBufferIterator iterator = dataBuffer .writableByteBuffers ()) {
709743 assertThat (iterator ).hasNext ();
710744 ByteBuffer byteBuffer = iterator .next ();
711- byteBuffer .put ((byte ) 'a' );
712- dataBuffer .writePosition (1 );
745+ assertThat (byteBuffer .position ()).isEqualTo (0 );
746+ assertThat (byteBuffer .limit ()).isEqualTo (1 );
747+ assertThat (byteBuffer .capacity ()).isEqualTo (1 );
748+ assertThat (byteBuffer .remaining ()).isEqualTo (1 );
749+
750+ byteBuffer .put ((byte ) 'c' );
751+ dataBuffer .writePosition (3 );
713752
714753 assertThat (iterator ).isExhausted ();
715754 }
716- assertThat (dataBuffer .read ()).isEqualTo ((byte ) 'a' );
755+ byte [] result = new byte [2 ];
756+ dataBuffer .read (result );
757+ assertThat (result ).containsExactly ('b' , 'c' );
717758
718759 release (dataBuffer );
719760 }
@@ -945,4 +986,21 @@ void shouldHonorSourceBuffersReadPosition(DataBufferFactory bufferFactory) {
945986 assertThat (StandardCharsets .UTF_8 .decode (byteBuffer ).toString ()).isEqualTo ("b" );
946987 }
947988
989+ @ ParameterizedDataBufferAllocatingTest // gh-31873
990+ void repeatedWrites (DataBufferFactory bufferFactory ) {
991+ super .bufferFactory = bufferFactory ;
992+
993+ DataBuffer buffer = bufferFactory .allocateBuffer (256 );
994+ String name = "Müller" ;
995+ int repeatCount = 19 ;
996+ for (int i = 0 ; i < repeatCount ; i ++) {
997+ buffer .write (name , StandardCharsets .UTF_8 );
998+ }
999+ String result = buffer .toString (StandardCharsets .UTF_8 );
1000+ String expected = name .repeat (repeatCount );
1001+ assertThat (result ).isEqualTo (expected );
1002+
1003+ release (buffer );
1004+ }
1005+
9481006}
0 commit comments