@@ -50,7 +50,20 @@ public MultiBuffer(long capacity) {
5050 * @param buffers the backing buffers (cloned into an internal array)
5151 * @param chunkSize the size of each buffer chunk
5252 */
53- private MultiBuffer (ByteBuffer [] buffers , int chunkSize ) {
53+ MultiBuffer (ByteBuffer [] buffers , int chunkSize ) {
54+ for (int i = 0 ; i < buffers .length ; i ++) {
55+ ByteBuffer chunk = buffers [i ];
56+ if (chunk .capacity () == chunkSize ) {
57+ continue ;
58+ }
59+ if (i == buffers .length - 1 ) {
60+ // The last chunk can have a different size
61+ continue ;
62+ }
63+ throw new IllegalArgumentException ("Chunk at index " + i
64+ + " is smaller than expected chunk size" );
65+ }
66+
5467 this .buffers = buffers .clone ();
5568 long capacity = 0 ;
5669 for (ByteBuffer buffer : buffers ) {
@@ -306,29 +319,18 @@ public String decode(CharsetDecoder decoder)
306319 }
307320
308321 /**
309- * Wraps the given byte arrays in a new {@code MultiBuffer}.
322+ * Wraps the given {@link ByteBuffer}s in a new {@code MultiBuffer}.
310323 *
311- * <p>If any array exceeds {@link #DEFAULT_CHUNK_SIZE}, it will be split across multiple
312- * underlying {@link ByteBuffer}s. The data is copied into new buffers so the
313- * returned {@code MultiBuffer} is fully independent .
324+ * <p>All chunks must have size {@link #DEFAULT_CHUNK_SIZE}, except that
325+ * the final chunk may be smaller. The buffers are used directly and not
326+ * copied .
314327 *
315- * @param chunks the byte arrays to wrap
316- * @return a new {@code MultiBuffer} backed by the arrays
328+ * @param chunks the backing buffers
329+ * @return a new {@code MultiBuffer} backed by the given buffers
330+ * @throws IllegalArgumentException if a non-final chunk is smaller than
331+ * {@link #DEFAULT_CHUNK_SIZE}
317332 */
318333 public static MultiBuffer wrap (ByteBuffer [] chunks ) {
319- for (int i = 0 ; i < chunks .length ; i ++) {
320- ByteBuffer chunk = chunks [i ];
321- if (chunk .capacity () == DEFAULT_CHUNK_SIZE ) {
322- continue ;
323- }
324- if (i == chunks .length - 1 ) {
325- // The last chunk can have a different size
326- continue ;
327- }
328- throw new IllegalArgumentException ("Chunk at index " + i
329- + " is smaller than expected chunk size" );
330- }
331-
332334 return new MultiBuffer (chunks , DEFAULT_CHUNK_SIZE );
333335 }
334336
0 commit comments