1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -678,6 +678,38 @@ void toByteBufferDestination(DataBufferFactory bufferFactory) {
678
678
void readableByteBuffers (DataBufferFactory bufferFactory ) throws IOException {
679
679
super .bufferFactory = bufferFactory ;
680
680
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
+
681
713
DataBuffer dataBuffer = this .bufferFactory .join (Arrays .asList (stringBuffer ("a" ),
682
714
stringBuffer ("b" ), stringBuffer ("c" )));
683
715
@@ -703,17 +735,26 @@ void readableByteBuffers(DataBufferFactory bufferFactory) throws IOException {
703
735
void writableByteBuffers (DataBufferFactory bufferFactory ) {
704
736
super .bufferFactory = bufferFactory ;
705
737
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 );
707
741
708
742
try (DataBuffer .ByteBufferIterator iterator = dataBuffer .writableByteBuffers ()) {
709
743
assertThat (iterator ).hasNext ();
710
744
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 );
713
752
714
753
assertThat (iterator ).isExhausted ();
715
754
}
716
- assertThat (dataBuffer .read ()).isEqualTo ((byte ) 'a' );
755
+ byte [] result = new byte [2 ];
756
+ dataBuffer .read (result );
757
+ assertThat (result ).containsExactly ('b' , 'c' );
717
758
718
759
release (dataBuffer );
719
760
}
@@ -945,4 +986,21 @@ void shouldHonorSourceBuffersReadPosition(DataBufferFactory bufferFactory) {
945
986
assertThat (StandardCharsets .UTF_8 .decode (byteBuffer ).toString ()).isEqualTo ("b" );
946
987
}
947
988
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
+
948
1006
}
0 commit comments