ppkarwasz commented on code in PR #784:
URL: https://github.com/apache/commons-io/pull/784#discussion_r2368015636


##########
src/main/java/org/apache/commons/io/channels/ByteArraySeekableByteChannel.java:
##########
@@ -37,41 +37,66 @@
  * accessed via {@link ByteArraySeekableByteChannel#array()}.
  * </p>
  *
- * @since 2.19.0
+ * @since 2.21.0
  */
 public class ByteArraySeekableByteChannel implements SeekableByteChannel {
 
     private static final int RESIZE_LIMIT = Integer.MAX_VALUE >> 1;
+
+    /**
+     * Constructs a channel that wraps the given byte array.
+     * <p>
+     * The resulting channel will share the given array as its buffer, until a 
write operation
+     * requires a larger capacity.
+     * The initial size of the channel is the length of the given array, and 
the initial position is 0.
+     * </p>
+     * @param bytes The byte array to wrap; must not be {@code null}.
+     * @return A new channel that wraps the given byte array; never {@code 
null}.
+     * @throws NullPointerException If the byte array is {@code null}.
+     */
+    public static ByteArraySeekableByteChannel wrap(byte[] bytes) {
+        Objects.requireNonNull(bytes, "bytes");
+        return new ByteArraySeekableByteChannel(bytes, bytes.length);
+    }
+
     private byte[] data;
-    private final AtomicBoolean closed = new AtomicBoolean();
+    private volatile boolean closed;
     private int position;
     private int size;
     private final ReentrantLock lock = new ReentrantLock();
 
     /**
      * Constructs a new instance using a default empty buffer.
+     * <p>
+     * The initial size of the channel is 0, and the initial position is 0.
+     * </p>
      */
     public ByteArraySeekableByteChannel() {
         this(0);
     }
 
-    /**
-     * Constructs a new instance from a byte array.
-     *
-     * @param data input data or pre-allocated array.
-     */
-    public ByteArraySeekableByteChannel(final byte[] data) {
-        this.data = data;
-        this.size = data.length;
-    }
-
     /**
      * Constructs a new instance from a size of storage to be allocated.
-     *
+     * <p>
+     * The initial size of the channel is 0, and the initial position is 0.
+     * </p>
      * @param size size of internal buffer to allocate, in bytes.
      */
     public ByteArraySeekableByteChannel(final int size) {
-        this(new byte[size]);
+        this(byteArray(size), 0);
+    }
+
+    private static byte[] byteArray(int value) {
+        if (value < 0) {
+            throw new IllegalArgumentException("Size must be non-negative");
+        }
+        return new byte[value];
+    }
+
+    private ByteArraySeekableByteChannel(byte[] data, int size) {

Review Comment:
   The lack of validation is the reason the constructor is private…



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to