You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/08/14 21:38:26 UTC
(commons-io) branch master updated (ec5e2e12f -> f130a805b)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
from ec5e2e12f Add ProxyOutputStream tests
new fb54abad6 Update release version in changes.xml
new f130a805b Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(*, long)
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 5 +-
.../commons/io/output/ProxyOutputStream.java | 85 +++++++++++-----
.../commons/io/output/ProxyOutputStreamTest.java | 110 ++++++++++++++++++---
3 files changed, 162 insertions(+), 38 deletions(-)
(commons-io) 02/02: Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(*, long)
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit f130a805b580eec1727a6df790e080aa88aa9baa
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 14 17:38:21 2025 -0400
Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(*, long)
---
src/changes/changes.xml | 3 +
.../commons/io/output/ProxyOutputStream.java | 85 +++++++++++-----
.../commons/io/output/ProxyOutputStreamTest.java | 110 ++++++++++++++++++---
3 files changed, 161 insertions(+), 37 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f7c8187a6..d3bbf77d6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="strangelookingnerd, Gary Gregory">FileUtils#byteCountToDisplaySize() supports Zettabyte, Yottabyte, Ronnabyte and Quettabyte #763.</action>
<action dev="ggregory" type="add" due-to="strangelookingnerd, Gary Gregory">Add org.apache.commons.io.FileUtils.ONE_RB #763.</action>
<action dev="ggregory" type="add" due-to="strangelookingnerd, Gary Gregory">Add org.apache.commons.io.FileUtils.ONE_QB #763.</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(byte[], int, int, long).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(byte[], long).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(int, long).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.18.0 to 1.19.0.</action>
</release>
diff --git a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
index d435abfbd..1fb6f8ad8 100644
--- a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.commons.io.output;
import java.io.FilterOutputStream;
@@ -24,13 +25,10 @@
import org.apache.commons.io.build.AbstractStreamBuilder;
/**
- * A Proxy stream which acts as expected, that is it passes the method
- * calls on to the proxied stream and doesn't change which methods are
- * being called. It is an alternative base class to FilterOutputStream
- * to increase reusability.
+ * A Proxy stream which acts as expected, that is it passes the method calls on to the proxied stream and doesn't change which methods are being called. It is
+ * an alternative base class to FilterOutputStream to increase reusability.
* <p>
- * See the protected methods for ways in which a subclass can easily decorate
- * a stream with custom pre-, post- or error processing functionality.
+ * See the protected methods for ways in which a subclass can easily decorate a stream with custom pre-, post- or error processing functionality.
* </p>
*/
public class ProxyOutputStream extends FilterOutputStream {
@@ -72,7 +70,6 @@ public Builder() {
public ProxyOutputStream get() throws IOException {
return new ProxyOutputStream(this);
}
-
}
@SuppressWarnings("resource") // caller closes
@@ -84,7 +81,7 @@ public ProxyOutputStream get() throws IOException {
/**
* Constructs a new ProxyOutputStream.
*
- * @param delegate the OutputStream to delegate to.
+ * @param delegate the OutputStream to delegate to.
*/
public ProxyOutputStream(final OutputStream delegate) {
// the delegate is stored in a protected superclass variable named 'out'
@@ -92,14 +89,11 @@ public ProxyOutputStream(final OutputStream delegate) {
}
/**
- * Invoked by the write methods after the proxied call has returned
- * successfully. The number of bytes written (1 for the
- * {@link #write(int)} method, buffer length for {@link #write(byte[])},
- * etc.) is given as an argument.
+ * Invoked by the write methods after the proxied call has returned successfully. The number of bytes written (1 for the {@link #write(int)} method, buffer
+ * length for {@link #write(byte[])}, etc.) is given as an argument.
* <p>
- * Subclasses can override this method to add common post-processing
- * functionality without having to override all the write methods.
- * The default implementation does nothing.
+ * Subclasses can override this method to add common post-processing functionality without having to override all the write methods. The default
+ * implementation does nothing.
* </p>
*
* @param n number of bytes written.
@@ -112,13 +106,11 @@ protected void afterWrite(final int n) throws IOException {
}
/**
- * Invoked by the write methods before the call is proxied. The number
- * of bytes to be written (1 for the {@link #write(int)} method, buffer
- * length for {@link #write(byte[])}, etc.) is given as an argument.
+ * Invoked by the write methods before the call is proxied. The number of bytes to be written (1 for the {@link #write(int)} method, buffer length for
+ * {@link #write(byte[])}, etc.) is given as an argument.
* <p>
- * Subclasses can override this method to add common pre-processing
- * functionality without having to override all the write methods.
- * The default implementation does nothing.
+ * Subclasses can override this method to add common pre-processing functionality without having to override all the write methods. The default
+ * implementation does nothing.
* </p>
*
* @param n number of bytes to be written.
@@ -157,8 +149,7 @@ public void flush() throws IOException {
/**
* Handle any IOExceptions thrown.
* <p>
- * This method provides a point to implement custom exception.
- * handling. The default behavior is to re-throw the exception.
+ * This method provides a point to implement custom exception. handling. The default behavior is to re-throw the exception.
* </p>
*
* @param e The IOException thrown.
@@ -214,7 +205,7 @@ public void write(final byte[] b) throws IOException {
/**
* Invokes the delegate's {@code write(byte[])} method.
*
- * @param b the bytes to write.
+ * @param b the bytes to write.
* @param off The start offset.
* @param len The number of bytes to write.
* @throws IOException if an I/O error occurs.
@@ -247,4 +238,50 @@ public void write(final int b) throws IOException {
}
}
+ /**
+ * Invokes the delegate's {@code write(byte[])} method for the {@code repeat} count.
+ *
+ * @param b the bytes to write.
+ * @param off The start offset.
+ * @param len The number of bytes to write.
+ * @param repeat How many times to write the bytes in {@code b}.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.21.0
+ */
+ public void writeRepeat(final byte[] b, final int off, final int len, final long repeat) throws IOException {
+ long remains = repeat;
+ while (remains-- > 0) {
+ write(b, off, len);
+ }
+ }
+
+ /**
+ * Invokes the delegate's {@code write(byte[])} method for the {@code repeat} count.
+ *
+ * @param b the bytes to write.
+ * @param repeat How many times to write the bytes in {@code b}.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.21.0
+ */
+ public void writeRepeat(final byte[] b, final long repeat) throws IOException {
+ long remains = repeat;
+ while (remains-- > 0) {
+ write(b);
+ }
+ }
+
+ /**
+ * Invokes the delegate's {@code write(int)} method.
+ *
+ * @param b the byte to write.
+ * @param repeat How many times to write the byte in {@code b}.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.21.0
+ */
+ public void writeRepeat(final int b, final long repeat) throws IOException {
+ long remains = repeat;
+ while (remains-- > 0) {
+ write(b);
+ }
+ }
}
diff --git a/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java
index 67a2f3373..45b9e759c 100644
--- a/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java
@@ -34,7 +34,7 @@
*/
class ProxyOutputStreamTest {
- private ByteArrayOutputStream original;
+ private ByteArrayOutputStream target;
private ProxyOutputStream proxied;
@@ -44,7 +44,7 @@ class ProxyOutputStreamTest {
@BeforeEach
public void setUp() {
- original = new ByteArrayOutputStream() {
+ target = new ByteArrayOutputStream() {
@Override
public void write(final byte[] ba) {
@@ -64,12 +64,12 @@ public synchronized void write(final int ba) {
super.write(ba);
}
};
- proxied = new ProxyOutputStream(original);
+ proxied = new ProxyOutputStream(target);
}
@Test
void testBuilder() throws Exception {
- assertSame(original, new ProxyOutputStream.Builder().setOutputStream(original).get().unwrap());
+ assertSame(target, new ProxyOutputStream.Builder().setOutputStream(target).get().unwrap());
}
@SuppressWarnings("resource")
@@ -79,8 +79,8 @@ void testSetReference() throws Exception {
proxied.setReference(new ByteArrayOutputStream());
proxied.write('y');
assertFalse(hitByteArray.get());
- assertEquals(0, original.size());
- assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, original.toByteArray());
+ assertEquals(0, target.size());
+ assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, target.toByteArray());
}
@Test
@@ -88,8 +88,8 @@ void testWriteByteArray() throws Exception {
assertFalse(hitByteArray.get());
proxied.write(new byte[] { 'y', 'z' });
assertTrue(hitByteArray.get());
- assertEquals(2, original.size());
- assertArrayEquals(new byte[] { 'y', 'z' }, original.toByteArray());
+ assertEquals(2, target.size());
+ assertArrayEquals(new byte[] { 'y', 'z' }, target.toByteArray());
}
@Test
@@ -97,8 +97,62 @@ void testWriteByteArrayAt() throws Exception {
assertFalse(hitByteArrayAt.get());
proxied.write(new byte[] { 'y', 'z' }, 1, 1);
assertTrue(hitByteArrayAt.get());
- assertEquals(1, original.size());
- assertArrayEquals(new byte[] { 'z' }, original.toByteArray());
+ assertEquals(1, target.size());
+ assertArrayEquals(new byte[] { 'z' }, target.toByteArray());
+ }
+
+ @Test
+ void testWriteByteArrayAtRepeat() throws Exception {
+ // repeat -1
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 0);
+ assertFalse(hitByteArrayAt.get());
+ hitByteArray.set(false);
+ assertEquals(0, target.size());
+ assertArrayEquals(new byte[] {}, target.toByteArray());
+ // repeat 0
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 0);
+ assertFalse(hitByteArrayAt.get());
+ hitByteArray.set(false);
+ assertEquals(0, target.size());
+ assertArrayEquals(new byte[] {}, target.toByteArray());
+ // repeat 1
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 1);
+ assertTrue(hitByteArrayAt.get());
+ hitByteArray.set(false);
+ assertEquals(1, target.size());
+ assertArrayEquals(new byte[] { 'z' }, target.toByteArray());
+ // repeat 2
+ proxied.writeRepeat(new byte[] { 'y', 'x' }, 1, 1, 2);
+ assertTrue(hitByteArrayAt.get());
+ assertEquals(3, target.size());
+ assertArrayEquals(new byte[] { 'z', 'x', 'x' }, target.toByteArray());
+ }
+
+ @Test
+ void testWriteByteArrayRepeat() throws Exception {
+ // repeat -1
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, -1);
+ assertFalse(hitByteArray.get());
+ hitByteArray.set(false);
+ assertEquals(0, target.size());
+ assertArrayEquals(new byte[] {}, target.toByteArray());
+ // repeat 0
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, 0);
+ assertFalse(hitByteArray.get());
+ hitByteArray.set(false);
+ assertEquals(0, target.size());
+ assertArrayEquals(new byte[] {}, target.toByteArray());
+ // repeat 1
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, 1);
+ assertTrue(hitByteArray.get());
+ hitByteArray.set(false);
+ assertEquals(2, target.size());
+ assertArrayEquals(new byte[] { 'y', 'z' }, target.toByteArray());
+ // repeat 2
+ proxied.writeRepeat(new byte[] { 'y', 'z' }, 2);
+ assertTrue(hitByteArray.get());
+ assertEquals(6, target.size());
+ assertArrayEquals(new byte[] { 'y', 'z', 'y', 'z' , 'y', 'z' }, target.toByteArray());
}
@Test
@@ -106,15 +160,45 @@ void testWriteInt() throws Exception {
assertFalse(hitInt.get());
proxied.write('y');
assertTrue(hitInt.get());
- assertEquals(1, original.size());
- assertEquals('y', original.toByteArray()[0]);
+ assertEquals(1, target.size());
+ assertEquals('y', target.toByteArray()[0]);
+ }
+
+ @Test
+ void testWriteIntRepeat() throws Exception {
+ // repeat -1
+ assertFalse(hitInt.get());
+ proxied.writeRepeat('y', -1);
+ assertFalse(hitInt.get());
+ assertEquals(0, target.size());
+ assertArrayEquals(new byte[] {}, target.toByteArray());
+ // repeat 0
+ assertFalse(hitInt.get());
+ proxied.writeRepeat('y', 0);
+ assertFalse(hitInt.get());
+ assertEquals(0, target.size());
+ assertArrayEquals(new byte[] {}, target.toByteArray());
+ // repeat 1
+ assertFalse(hitInt.get());
+ proxied.writeRepeat('y', 1);
+ assertTrue(hitInt.get());
+ hitInt.set(false);
+ assertEquals(1, target.size());
+ assertArrayEquals(new byte[] { 'y' }, target.toByteArray());
+ // repeat 2
+ assertFalse(hitInt.get());
+ proxied.writeRepeat('z', 2);
+ assertTrue(hitInt.get());
+ hitInt.set(false);
+ assertEquals(3, target.size());
+ assertArrayEquals(new byte[] { 'y', 'z', 'z' }, target.toByteArray());
}
@Test
void testWriteNullArrayProxiesToUnderlying() throws Exception {
assertFalse(hitByteArray.get());
final byte[] ba = null;
- assertThrows(NullPointerException.class, () -> original.write(ba));
+ assertThrows(NullPointerException.class, () -> target.write(ba));
assertTrue(hitByteArray.get());
assertThrows(NullPointerException.class, () -> proxied.write(ba));
assertTrue(hitByteArray.get());
(commons-io) 01/02: Update release version in changes.xml
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit fb54abad67e3212f98cad4bd47d86f2fbc5c0487
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 14 17:35:52 2025 -0400
Update release version in changes.xml
---
src/changes/changes.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 877ea45f4..f7c8187a6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,7 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
<title>Apache Commons IO Release Notes</title>
</properties>
<body>
- <release version="2.20.1" date="YYYY-MM-DD" description="Version 2.20.1: Java 8 or later is required.">
+ <release version="2.21.0" date="YYYY-MM-DD" description="Version 2.20.1: Java 8 or later is required.">
<!-- FIX -->
<action type="fix" dev="ggregory" due-to="Gary Gregory">When testing on Java 21 and up, enable -XX:+EnableDynamicAgentLoading.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">When testing on Java 24 and up, don't fail FileUtilsListFilesTest for a different behavior in the JRE.</action>