diff --git a/src/changes/changes.xml b/src/changes/changes.xml index badc1289c71..dc2024d3923 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -76,6 +76,9 @@ The type attribute can be add,update,fix,remove. Add Uncheck.run(IORunnable, Supplier<String>) + + Add Uncheck.get(IOSupplier, Supplier<String>) + ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1. diff --git a/src/main/java/org/apache/commons/io/function/Uncheck.java b/src/main/java/org/apache/commons/io/function/Uncheck.java index e9da1633f2c..6419db31156 100644 --- a/src/main/java/org/apache/commons/io/function/Uncheck.java +++ b/src/main/java/org/apache/commons/io/function/Uncheck.java @@ -200,6 +200,23 @@ public static T get(final IOSupplier supplier) { } } + /** + * Gets the result from an IO supplier. + * + * @param the return type of the operations. + * @param supplier Supplies the return value. + * @param message The UncheckedIOException message if an I/O error occurs. + * @return result from the supplier. + * @throws UncheckedIOException if an I/O error occurs. + */ + public static T get(final IOSupplier supplier, final Supplier message) { + try { + return supplier.get(); + } catch (final IOException e) { + throw wrap(e, message); + } + } + /** * Gets the result from an IO int supplier. * diff --git a/src/test/java/org/apache/commons/io/UncheckIOTest.java b/src/test/java/org/apache/commons/io/UncheckIOTest.java index 8ea4d7961d4..3611cc3cde9 100644 --- a/src/test/java/org/apache/commons/io/UncheckIOTest.java +++ b/src/test/java/org/apache/commons/io/UncheckIOTest.java @@ -43,6 +43,13 @@ public class UncheckIOTest { private static final String CUSTOM_MESSAGE = "Custom message"; private static final byte[] BYTES = { 'a', 'b' }; + private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) { + assertEquals(CUSTOM_MESSAGE, e.getMessage()); + final IOException cause = e.getCause(); + assertEquals(expected.getClass(), cause.getClass()); + assertEquals(CAUSE_MESSAGE, cause.getMessage()); + } + private ByteArrayInputStream newInputStream() { return new ByteArrayInputStream(BYTES); } @@ -97,6 +104,23 @@ public void testGet() { assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue()); } + /** + * Tests {@link Uncheck#get(IOSupplier, Supplier)}. + */ + @Test + public void testGetMessage() { + // No exception + assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE); + // Exception + final IOException expected = new IOException(CAUSE_MESSAGE); + try { + Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE); + fail(); + } catch (final UncheckedIOException e) { + assertUncheckedIOException(expected, e); + } + } + /** * Tests {@link Uncheck#run(IORunnable)}. */ @@ -108,25 +132,23 @@ public void testRun() { } /** - * Tests {@link Uncheck#run(IORunnable)}. + * Tests {@link Uncheck#run(IORunnable, Supplier))}. * * @throws IOException */ @Test public void testRunMessage() throws IOException { + // No exception final ByteArrayInputStream stream = newInputStream(); Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE); assertEquals('b', Uncheck.get(stream::read).intValue()); final IOException expected = new IOException(CAUSE_MESSAGE); - // + // Exception try { Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE); fail(); } catch (final UncheckedIOException e) { - assertEquals(CUSTOM_MESSAGE, e.getMessage()); - final IOException cause = e.getCause(); - assertEquals(expected.getClass(), cause.getClass()); - assertEquals(CAUSE_MESSAGE, cause.getMessage()); + assertUncheckedIOException(expected, e); } } }