Add and reuse UncheckedIOExceptions.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -123,6 +123,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add UncheckedAppendable.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add and reuse UncheckedIOExceptions.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1924,7 +1924,7 @@ public static boolean isSymlink(final File file) {
try {
return StreamIterator.iterator(streamFiles(directory, recursive, extensions));
} catch (final IOException e) {
- throw new UncheckedIOException(directory.toString(), e);
+ throw UncheckedIOExceptions.create(directory, e);
}
}
@@ -1998,7 +1998,7 @@ public static long lastModifiedUnchecked(final File file) {
try {
return lastModified(file);
} catch (final IOException e) {
- throw new UncheckedIOException(file.toString(), e);
+ throw UncheckedIOExceptions.create(file, e);
}
}
@@ -2131,7 +2131,7 @@ private static AccumulatorPathVisitor listAccumulate(final File directory, final
final AccumulatorPathVisitor visitor = listAccumulate(directory, fileFilter, dirFilter);
return visitor.getFileList().stream().map(Path::toFile).collect(Collectors.toList());
} catch (final IOException e) {
- throw new UncheckedIOException(directory.toString(), e);
+ throw UncheckedIOExceptions.create(directory, e);
}
}
@@ -2149,7 +2149,7 @@ private static AccumulatorPathVisitor listAccumulate(final File directory, final
try {
return toList(streamFiles(directory, recursive, extensions));
} catch (final IOException e) {
- throw new UncheckedIOException(directory.toString(), e);
+ throw UncheckedIOExceptions.create(directory, e);
}
}
@@ -2180,7 +2180,7 @@ private static AccumulatorPathVisitor listAccumulate(final File directory, final
list.addAll(visitor.getDirList());
return list.stream().map(Path::toFile).collect(Collectors.toList());
} catch (final IOException e) {
- throw new UncheckedIOException(directory.toString(), e);
+ throw UncheckedIOExceptions.create(directory, e);
}
}

ADD
src/main/java/org/apache/commons/io/UncheckedIOExceptions.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Objects;
+
+/**
+ * Helps use {@link UncheckedIOException}.
+ *
+ * @since 2.12.0
+ */
+public class UncheckedIOExceptions {
+
+ /**
+ * Creates a new UncheckedIOException for the given detail message.
+ * <p>
+ * This method exists because there is no String constructor in UncheckedIOException.
+ * </p>
+ *
+ * @param message the detail message.
+ * @return a new UncheckedIOException.
+ */
+ public static UncheckedIOException create(Object message) {
+ final String string = Objects.toString(message);
+ return new UncheckedIOException(string, new IOException(string));
+ }
+
+ /**
+ * Creates a new UncheckedIOException for the given detail message.
+ * <p>
+ * This method exists because there is no String constructor in UncheckedIOException.
+ * </p>
+ *
+ * @param message the detail message.
+ * @param e cause the {@code IOException}.
+ * @return a new UncheckedIOException.
+ */
+ public static UncheckedIOException create(Object message, final IOException e) {
+ return new UncheckedIOException(Objects.toString(message), e);
+ }
+
+}

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOExceptionList;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.UncheckedIOExceptions;
import org.apache.commons.io.file.Counters.PathCounters;
import org.apache.commons.io.filefilter.IOFileFilter;
@@ -858,7 +859,7 @@ public static BasicFileAttributes readBasicFileAttributesUnchecked(final Path pa
try {
return readBasicFileAttributes(path);
} catch (final IOException e) {
- throw new UncheckedIOException(e);
+ throw UncheckedIOExceptions.create(path, e);
}
}

MODIFY
src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java
src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java
import java.io.UncheckedIOException;
import java.util.Objects;
+import org.apache.commons.io.UncheckedIOExceptions;
+
/**
* An {@link Appendable} implementation that throws {@link UncheckedIOException} instead of {@link IOException}.
*
@@ -43,7 +45,7 @@ public UncheckedAppendable append(final char c) {
try {
appendable.append(c);
} catch (final IOException e) {
- throw new UncheckedIOException(String.valueOf(c), e);
+ throw UncheckedIOExceptions.create(c, e);
}
return this;
}
@@ -53,7 +55,7 @@ public UncheckedAppendable append(final CharSequence csq) {
try {
appendable.append(csq);
} catch (final IOException e) {
- throw new UncheckedIOException(Objects.toString(csq), e);
+ throw UncheckedIOExceptions.create(csq, e);
}
return this;
}
@@ -63,7 +65,7 @@ public UncheckedAppendable append(final CharSequence csq, final int start, final
try {
appendable.append(csq, start, end);
} catch (final IOException e) {
- throw new UncheckedIOException(Objects.toString(csq), e);
+ throw UncheckedIOExceptions.create(csq, e);
}
return this;
}

ADD
src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+
+import org.junit.jupiter.api.Test;
+
+public class UncheckedIOExceptionsTest {
+
+ @Test
+ public void testCreate() {
+ final Object message = "test";
+ try {
+ throw UncheckedIOExceptions.create(message);
+ } catch (UncheckedIOException e) {
+ assertEquals(message, e.getMessage());
+ assertEquals(message, e.getCause().getMessage());
+ }
+
+ }
+
+ @Test
+ public void testCreateWithException() {
+ final Object message1 = "test1";
+ final Object message2 = "test2";
+ final IOException ioe = new IOException(message2.toString());
+ try {
+ throw UncheckedIOExceptions.create(message1, ioe);
+ } catch (UncheckedIOException e) {
+ assertEquals(message1, e.getMessage());
+ assertEquals(message2, e.getCause().getMessage());
+ }
+
+ }
+}

Add and reuse UncheckedIOExceptions.