Skip to content

Commit 1e558f5

Browse files
committed
fix: fix NullOutputStream Javadoc and enhance tests
1 parent d00d5ef commit 1e558f5

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/main/java/org/apache/commons/io/output/NullOutputStream.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ public void write(final byte[] b) throws IOException {
6969
}
7070

7171
/**
72-
* Does nothing.
72+
* No-op operation.
7373
*
74-
* @param b This method ignores this parameter.
75-
* @param off This method ignores this parameter.
76-
* @param len This method ignores this parameter.
74+
* <p>Validates the arguments but does not write the data.</p>
75+
*
76+
* @param b The byte array to write from, not {@code null}.
77+
* @param off The offset to start at.
78+
* @param len The number of bytes to write.
79+
* @throws NullPointerException If {@code b} is {@code null}.
80+
* @throws IndexOutOfBoundsException If {@code off} or {@code len} are negative, {@code off + len} is greater than
81+
* {@code b.length}.
7782
*/
7883
@Override
7984
public void write(final byte[] b, final int off, final int len) {

src/test/java/org/apache/commons/io/output/NullOutputStreamTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.commons.io.output;
1818

19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
1921
import java.io.IOException;
2022

2123
import org.junit.jupiter.api.Test;
@@ -34,11 +36,18 @@ private void process(final NullOutputStream nos) throws IOException {
3436
nos.close();
3537
nos.write("allowed".getBytes());
3638
nos.write(255);
39+
// Test arguments validation
40+
final byte[] b = new byte[1];
41+
assertThrows(IndexOutOfBoundsException.class, () -> nos.write(b, -1, 0));
42+
assertThrows(IndexOutOfBoundsException.class, () -> nos.write(b, 0, -1));
43+
assertThrows(IndexOutOfBoundsException.class, () -> nos.write(b, 0, 2));
44+
assertThrows(NullPointerException.class, () -> nos.write(null, 0, 0));
3745
}
3846

3947
@Test
48+
@SuppressWarnings("deprecation")
4049
void testNewInstance() throws IOException {
41-
try (NullOutputStream nos = NullOutputStream.INSTANCE) {
50+
try (NullOutputStream nos = new NullOutputStream()) {
4251
process(nos);
4352
}
4453
}

0 commit comments

Comments
 (0)