Fix compiler warnings, use try-with-resources.
MODIFY
src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
@@ -104,77 +104,71 @@ private void checkStreams(
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
- public void testWriteZero(final String baosName, final BAOSFactory<?> baosFactory) {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
+ public void testWriteZero(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
baout.write(new byte[0], 0, 0);
assertTrue(true, "Dummy");
}
+ }
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
- public void testInvalidWriteOffsetUnder(final String baosName, final BAOSFactory<?> baosFactory) {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- assertThrows(IndexOutOfBoundsException.class, () ->
- baout.write(null, -1, 0)
- );
+ public void testInvalidWriteOffsetUnder(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
+ assertThrows(IndexOutOfBoundsException.class, () -> baout.write(null, -1, 0));
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
- public void testInvalidWriteOffsetOver(final String baosName, final BAOSFactory<?> baosFactory) {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- assertThrows(IndexOutOfBoundsException.class, () ->
- baout.write(new byte[0], 1, 0)
- );
+ public void testInvalidWriteOffsetOver(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
+ assertThrows(IndexOutOfBoundsException.class, () -> baout.write(new byte[0], 1, 0));
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
- public void testInvalidWriteLenUnder(final String baosName, final BAOSFactory<?> baosFactory) {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- assertThrows(IndexOutOfBoundsException.class, () ->
- baout.write(new byte[1], 0, -1)
- );
+ public void testInvalidWriteLenUnder(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
+ assertThrows(IndexOutOfBoundsException.class, () -> baout.write(new byte[1], 0, -1));
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
- public void testInvalidWriteOffsetAndLenUnder(final String baosName, final BAOSFactory<?> baosFactory) {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- assertThrows(IndexOutOfBoundsException.class, () ->
- baout.write(new byte[1], 1, -2)
- );
+ public void testInvalidWriteOffsetAndLenUnder(final String baosName, final BAOSFactory<?> baosFactory)
+ throws IOException {
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
+ assertThrows(IndexOutOfBoundsException.class, () -> baout.write(new byte[1], 1, -2));
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
- public void testInvalidWriteOffsetAndLenOver(final String baosName, final BAOSFactory<?> baosFactory) {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- assertThrows(IndexOutOfBoundsException.class, () ->
- baout.write(new byte[1], 0, 2)
- );
+ public void testInvalidWriteOffsetAndLenOver(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
+ assertThrows(IndexOutOfBoundsException.class, () -> baout.write(new byte[1], 0, 2));
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
public void testInvalidParameterizedConstruction(final String baosName, final BAOSFactory<?> baosFactory) {
assertThrows(IllegalArgumentException.class, () ->
- baosFactory.instance(-1)
+ baosFactory.newInstance(-1)
);
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
public void testToInputStreamEmpty(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
-
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance();
// Get data before more writes
- final InputStream in = baout.toInputStream();
+ final InputStream in = baout.toInputStream()) {
assertEquals(0, in.available());
assertTrue(in instanceof ClosedInputStream);
-
- in.close();
- baout.close();
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@@ -210,8 +204,8 @@ public void testToBufferedInputStream(final String baosName, final IOFunction<In
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
public void testToInputStream(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance();
+ final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream()) {
// Write 8224 bytes
writeData(baout, ref, 32);
@@ -220,7 +214,7 @@ public void testToInputStream(final String baosName, final BAOSFactory<?> baosFa
}
// Get data before more writes
- final InputStream in = baout.toInputStream();
+ try (final InputStream in = baout.toInputStream()) {
byte refData[] = ref.toByteArray();
// Write some more data
@@ -232,20 +226,22 @@ public void testToInputStream(final String baosName, final BAOSFactory<?> baosFa
checkByteArrays(refData, baoutData);
// Check all data written
- baoutData = IOUtils.toByteArray(baout.toInputStream());
+ try (final InputStream in2 = baout.toInputStream()) {
+ baoutData = IOUtils.toByteArray(in2);
+ }
refData = ref.toByteArray();
assertEquals(8254, baoutData.length);
checkByteArrays(refData, baoutData);
- baout.close();
- in.close();
+ }
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("baosFactories")
public void testToInputStreamWithReset(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
// Make sure reset() do not destroy InputStream returned from toInputStream()
- final AbstractByteArrayOutputStream baout = baosFactory.instance();
- final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance();
+ final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream()) {
// Write 8224 bytes
writeData(baout, ref, 32);
@@ -254,7 +250,7 @@ public void testToInputStreamWithReset(final String baosName, final BAOSFactory<
}
// Get data before reset
- final InputStream in = baout.toInputStream();
+ try (final InputStream in = baout.toInputStream()) {
byte refData[] = ref.toByteArray();
// Reset and write some new data
@@ -268,12 +264,14 @@ public void testToInputStreamWithReset(final String baosName, final BAOSFactory<
checkByteArrays(refData, baoutData);
// Check new data written after reset
- baoutData = IOUtils.toByteArray(baout.toInputStream());
+ try (final InputStream in2 = baout.toInputStream()) {
+ baoutData = IOUtils.toByteArray(in2);
+ }
refData = ref.toByteArray();
assertEquals(30, baoutData.length);
checkByteArrays(refData, baoutData);
- baout.close();
- in.close();
+ }
+ }
}
@ParameterizedTest(name = "[{index}] {0}")
@@ -283,8 +281,8 @@ public void testStream(final String baosName, final BAOSFactory<?> baosFactory)
// The ByteArrayOutputStream is initialized with 32 bytes to match
// the original more closely for this test.
- final AbstractByteArrayOutputStream baout = baosFactory.instance(32);
- final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
+ try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance(32);
+ final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream()) {
// First three writes
written = writeData(baout, ref, new int[] {4, 10, 22});
@@ -313,7 +311,7 @@ public void testStream(final String baosName, final BAOSFactory<?> baosFactory)
// Write the commons Byte[]OutputStream to a java.io.Byte[]OutputStream
// and vice-versa to test the writeTo() method.
- final AbstractByteArrayOutputStream baout1 = baosFactory.instance(32);
+ try (final AbstractByteArrayOutputStream baout1 = baosFactory.newInstance(32)) {
ref.writeTo(baout1);
final java.io.ByteArrayOutputStream ref1 = new java.io.ByteArrayOutputStream();
baout.writeTo(ref1);
@@ -326,13 +324,12 @@ public void testStream(final String baosName, final BAOSFactory<?> baosFactory)
// Make sure that empty ByteArrayOutputStreams really don't create garbage
// on toByteArray()
- final AbstractByteArrayOutputStream baos1 = baosFactory.instance();
- final AbstractByteArrayOutputStream baos2 = baosFactory.instance();
+ try (final AbstractByteArrayOutputStream baos1 = baosFactory.newInstance();
+ final AbstractByteArrayOutputStream baos2 = baosFactory.newInstance()) {
assertSame(baos1.toByteArray(), baos2.toByteArray());
- baos1.close();
- baos2.close();
- baout.close();
- baout1.close();
+ }
+ }
+ }
}
private static Stream<Arguments> baosFactories() {
@@ -344,31 +341,31 @@ public void testStream(final String baosName, final BAOSFactory<?> baosFactory)
private static class ByteArrayOutputStreamFactory implements BAOSFactory<ByteArrayOutputStream> {
@Override
- public ByteArrayOutputStream instance() {
+ public ByteArrayOutputStream newInstance() {
return new ByteArrayOutputStream();
}
@Override
- public ByteArrayOutputStream instance(final int size) {
+ public ByteArrayOutputStream newInstance(final int size) {
return new ByteArrayOutputStream(size);
}
}
private static class UnsynchronizedByteArrayOutputStreamFactory implements BAOSFactory<UnsynchronizedByteArrayOutputStream> {
@Override
- public UnsynchronizedByteArrayOutputStream instance() {
+ public UnsynchronizedByteArrayOutputStream newInstance() {
return new UnsynchronizedByteArrayOutputStream();
}
@Override
- public UnsynchronizedByteArrayOutputStream instance(final int size) {
+ public UnsynchronizedByteArrayOutputStream newInstance(final int size) {
return new UnsynchronizedByteArrayOutputStream(size);
}
}
private interface BAOSFactory<T extends AbstractByteArrayOutputStream> {
- T instance();
- T instance(final int size);
+ T newInstance();
+ T newInstance(final int size);
}
private static Stream<Arguments> toBufferedInputStreamFunctionFactories() {
@@ -385,4 +382,3 @@ public UnsynchronizedByteArrayOutputStream instance(final int size) {
);
}
}
-

Fix compiler warnings, use try-with-resources.