@@ -50,7 +50,10 @@ public static int write (OutputStream outputStream, byte[] buffer, int length) {
5050 @ SneakyThrows
5151 public static int write (@ NonNull OutputStream outputStream , @ NonNull byte [] bytes , int offset , int length ) {
5252 if (offset < 0 || offset >= bytes .length ) {
53- val msg = String .format (ENGLISH , "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length" , offset );
53+ val msg = String .format (ENGLISH ,
54+ "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length (%d)" ,
55+ offset , bytes .length
56+ );
5457 throw new IndexOutOfBoundsException (msg );
5558 }
5659 if (length < 0 ) {
@@ -81,13 +84,13 @@ public static int write (@NonNull OutputStream outputStream, @NonNull ByteBuffer
8184 }
8285
8386 public static int write (OutputStream outputStream , @ NonNull Bytes buffer ) {
84- return write (outputStream , buffer , buffer .writableBytes ());
87+ return write (outputStream , buffer , buffer .readableBytes ());
8588 }
8689
8790 public static int write (@ NonNull OutputStream outputStream , @ NonNull Bytes buffer , int length ) {
8891 val bytes = buffer .array ();
89- val written = write (outputStream , bytes , buffer .writerIndex (), length );
90- buffer .writerIndex (buffer .writerIndex () + written );
92+ val written = write (outputStream , bytes , buffer .readerIndex (), length );
93+ buffer .readerIndex (buffer .readerIndex () + written );
9194 return written ;
9295 }
9396
@@ -102,7 +105,10 @@ public static int write (WritableByteChannel channel, byte[] buffer, int length)
102105 @ SneakyThrows
103106 public static int write (@ NonNull WritableByteChannel channel , @ NonNull byte [] bytes , int offset , int length ) {
104107 if (offset < 0 || offset >= bytes .length ) {
105- val msg = String .format (ENGLISH , "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length" , offset );
108+ val msg = String .format (ENGLISH ,
109+ "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length (%d)" ,
110+ offset , bytes .length
111+ );
106112 throw new IndexOutOfBoundsException (msg );
107113 }
108114 if (length < 0 ) {
@@ -121,6 +127,7 @@ public static int write (@NonNull WritableByteChannel channel, @NonNull byte[] b
121127 if (written < 0 ) {
122128 break ;
123129 }
130+ totalWritten += written ;
124131 }
125132 return totalWritten ;
126133 }
@@ -137,13 +144,13 @@ public static int write (@NonNull WritableByteChannel channel, @NonNull ByteBuff
137144 }
138145
139146 public static int write (WritableByteChannel channel , @ NonNull Bytes buffer ) {
140- return write (channel , buffer , buffer .writableBytes ());
147+ return write (channel , buffer , buffer .readableBytes ());
141148 }
142149
143150 public static int write (@ NonNull WritableByteChannel channel , @ NonNull Bytes buffer , int length ) {
144151 val bytes = buffer .array ();
145- val written = write (channel , bytes , buffer .writerIndex (), length );
146- buffer .writerIndex (buffer .writerIndex () + written );
152+ val written = write (channel , bytes , buffer .readerIndex (), length );
153+ buffer .readerIndex (buffer .readerIndex () + written );
147154 return written ;
148155 }
149156
@@ -168,7 +175,7 @@ public static int write (@NonNull File file, ByteBuffer buffer, int length) {
168175 }
169176
170177 public static int write (File file , @ NonNull Bytes buffer ) {
171- return write (file , buffer , buffer .writableBytes ());
178+ return write (file , buffer , buffer .readableBytes ());
172179 }
173180
174181 public static int write (@ NonNull File file , Bytes buffer , int length ) {
@@ -202,7 +209,7 @@ public static int write (@NonNull Path path, ByteBuffer buffer, int length) {
202209 }
203210
204211 public static int write (Path path , @ NonNull Bytes buffer ) {
205- return write (path , buffer , buffer .writableBytes ());
212+ return write (path , buffer , buffer .readableBytes ());
206213 }
207214
208215 @ SneakyThrows
0 commit comments