@@ -45,6 +45,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
4545 private final boolean direct ;
4646 private final List <Component > components = new ArrayList <Component >();
4747 private final int maxNumComponents ;
48+ private static final ByteBuffer FULL_BYTEBUFFER = (ByteBuffer ) ByteBuffer .allocate (1 ).position (1 );
4849
4950 private boolean freed ;
5051
@@ -698,6 +699,10 @@ public CompositeByteBuf getBytes(int index, ByteBuffer dst) {
698699 @ Override
699700 public CompositeByteBuf getBytes (int index , ByteBuf dst , int dstIndex , int length ) {
700701 checkDstIndex (index , length , dstIndex , dst .capacity ());
702+ if (length == 0 ) {
703+ return this ;
704+ }
705+
701706 int i = toComponentIndex (index );
702707 while (length > 0 ) {
703708 Component c = components .get (i );
@@ -842,6 +847,9 @@ protected void _setLong(int index, long value) {
842847 @ Override
843848 public CompositeByteBuf setBytes (int index , byte [] src , int srcIndex , int length ) {
844849 checkSrcIndex (index , length , srcIndex , src .length );
850+ if (length == 0 ) {
851+ return this ;
852+ }
845853
846854 int i = toComponentIndex (index );
847855 while (length > 0 ) {
@@ -864,6 +872,10 @@ public CompositeByteBuf setBytes(int index, ByteBuffer src) {
864872 int length = src .remaining ();
865873
866874 checkIndex (index , length );
875+ if (length == 0 ) {
876+ return this ;
877+ }
878+
867879 int i = toComponentIndex (index );
868880 try {
869881 while (length > 0 ) {
@@ -886,6 +898,9 @@ public CompositeByteBuf setBytes(int index, ByteBuffer src) {
886898 @ Override
887899 public CompositeByteBuf setBytes (int index , ByteBuf src , int srcIndex , int length ) {
888900 checkSrcIndex (index , length , srcIndex , src .capacity ());
901+ if (length == 0 ) {
902+ return this ;
903+ }
889904
890905 int i = toComponentIndex (index );
891906 while (length > 0 ) {
@@ -905,6 +920,9 @@ public CompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int lengt
905920 @ Override
906921 public int setBytes (int index , InputStream in , int length ) throws IOException {
907922 checkIndex (index , length );
923+ if (length == 0 ) {
924+ return in .read (EmptyArrays .EMPTY_BYTES );
925+ }
908926
909927 int i = toComponentIndex (index );
910928 int readBytes = 0 ;
@@ -941,6 +959,9 @@ public int setBytes(int index, InputStream in, int length) throws IOException {
941959 @ Override
942960 public int setBytes (int index , ScatteringByteChannel in , int length ) throws IOException {
943961 checkIndex (index , length );
962+ if (length == 0 ) {
963+ return in .read (FULL_BYTEBUFFER );
964+ }
944965
945966 int i = toComponentIndex (index );
946967 int readBytes = 0 ;
@@ -982,7 +1003,9 @@ public int setBytes(int index, ScatteringByteChannel in, int length) throws IOEx
9821003 public ByteBuf copy (int index , int length ) {
9831004 checkIndex (index , length );
9841005 ByteBuf dst = Unpooled .buffer (length );
985- copyTo (index , length , toComponentIndex (index ), dst );
1006+ if (length != 0 ) {
1007+ copyTo (index , length , toComponentIndex (index ), dst );
1008+ }
9861009 return dst ;
9871010 }
9881011
0 commit comments