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
The following commit(s) were added to refs/heads/master by this push: new b38cf8a Add and reuse UncheckedIOExceptions. b38cf8a is described below commit b38cf8a694f97a511a4eb50c058fc5d4789cafee Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Aug 5 12:03:23 2021 -0400 Add and reuse UncheckedIOExceptions. --- src/changes/changes.xml | 3 ++ src/main/java/org/apache/commons/io/FileUtils.java | 10 ++-- .../apache/commons/io/UncheckedIOExceptions.java | 59 ++++++++++++++++++++++ .../java/org/apache/commons/io/file/PathUtils.java | 3 +- .../commons/io/output/UncheckedAppendableImpl.java | 8 +-- .../commons/io/UncheckedIOExceptionsTest.java | 54 ++++++++++++++++++++ 6 files changed, 128 insertions(+), 9 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3aeb2be..cb4af92 100644 --- a/src/changes/changes.xml +++ b/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. diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index 263f65b..82e2f60 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -1924,7 +1924,7 @@ public class FileUtils { 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 class FileUtils { try { return lastModified(file); } catch (final IOException e) { - throw new UncheckedIOException(file.toString(), e); + throw UncheckedIOExceptions.create(file, e); } } @@ -2131,7 +2131,7 @@ public class FileUtils { 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 @@ public class FileUtils { 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 @@ public class FileUtils { 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); } } diff --git a/src/main/java/org/apache/commons/io/UncheckedIOExceptions.java b/src/main/java/org/apache/commons/io/UncheckedIOExceptions.java new file mode 100644 index 0000000..2baf06f --- /dev/null +++ b/src/main/java/org/apache/commons/io/UncheckedIOExceptions.java @@ -0,0 +1,59 @@ +/* + * 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); + } + +} diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index da90fec..f7b640c 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -62,6 +62,7 @@ import java.util.stream.Stream; 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 final class PathUtils { try { return readBasicFileAttributes(path); } catch (final IOException e) { - throw new UncheckedIOException(e); + throw UncheckedIOExceptions.create(path, e); } } diff --git a/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java b/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java index 63fef97..ccdad4d 100644 --- a/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java +++ b/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java @@ -21,6 +21,8 @@ import java.io.IOException; 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 @@ class UncheckedAppendableImpl implements UncheckedAppendable { 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 @@ class UncheckedAppendableImpl implements UncheckedAppendable { 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 @@ class UncheckedAppendableImpl implements UncheckedAppendable { try { appendable.append(csq, start, end); } catch (final IOException e) { - throw new UncheckedIOException(Objects.toString(csq), e); + throw UncheckedIOExceptions.create(csq, e); } return this; } diff --git a/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java b/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java new file mode 100644 index 0000000..2344c31 --- /dev/null +++ b/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java @@ -0,0 +1,54 @@ +/* + * 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()); + } + + } +}