Skip to content

Commit b38cf8a

Browse files
committed
Add and reuse UncheckedIOExceptions.
1 parent a4373a8 commit b38cf8a

6 files changed

Lines changed: 128 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ The <action> type attribute can be add,update,fix,remove.
123123
<action dev="ggregory" type="add" due-to="Gary Gregory">
124124
Add UncheckedAppendable.
125125
</action>
126+
<action dev="ggregory" type="add" due-to="Gary Gregory">
127+
Add and reuse UncheckedIOExceptions.
128+
</action>
126129
<!-- UPDATE -->
127130
<action dev="ggregory" type="update" due-to="Dependabot">
128131
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ public static Iterator<File> iterateFiles(final File directory, final String[] e
19241924
try {
19251925
return StreamIterator.iterator(streamFiles(directory, recursive, extensions));
19261926
} catch (final IOException e) {
1927-
throw new UncheckedIOException(directory.toString(), e);
1927+
throw UncheckedIOExceptions.create(directory, e);
19281928
}
19291929
}
19301930

@@ -1998,7 +1998,7 @@ public static long lastModifiedUnchecked(final File file) {
19981998
try {
19991999
return lastModified(file);
20002000
} catch (final IOException e) {
2001-
throw new UncheckedIOException(file.toString(), e);
2001+
throw UncheckedIOExceptions.create(file, e);
20022002
}
20032003
}
20042004

@@ -2131,7 +2131,7 @@ public static Collection<File> listFiles(
21312131
final AccumulatorPathVisitor visitor = listAccumulate(directory, fileFilter, dirFilter);
21322132
return visitor.getFileList().stream().map(Path::toFile).collect(Collectors.toList());
21332133
} catch (final IOException e) {
2134-
throw new UncheckedIOException(directory.toString(), e);
2134+
throw UncheckedIOExceptions.create(directory, e);
21352135
}
21362136
}
21372137

@@ -2149,7 +2149,7 @@ public static Collection<File> listFiles(final File directory, final String[] ex
21492149
try {
21502150
return toList(streamFiles(directory, recursive, extensions));
21512151
} catch (final IOException e) {
2152-
throw new UncheckedIOException(directory.toString(), e);
2152+
throw UncheckedIOExceptions.create(directory, e);
21532153
}
21542154
}
21552155

@@ -2180,7 +2180,7 @@ public static Collection<File> listFilesAndDirs(
21802180
list.addAll(visitor.getDirList());
21812181
return list.stream().map(Path::toFile).collect(Collectors.toList());
21822182
} catch (final IOException e) {
2183-
throw new UncheckedIOException(directory.toString(), e);
2183+
throw UncheckedIOExceptions.create(directory, e);
21842184
}
21852185
}
21862186

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io;
19+
20+
import java.io.IOException;
21+
import java.io.UncheckedIOException;
22+
import java.util.Objects;
23+
24+
/**
25+
* Helps use {@link UncheckedIOException}.
26+
*
27+
* @since 2.12.0
28+
*/
29+
public class UncheckedIOExceptions {
30+
31+
/**
32+
* Creates a new UncheckedIOException for the given detail message.
33+
* <p>
34+
* This method exists because there is no String constructor in UncheckedIOException.
35+
* </p>
36+
*
37+
* @param message the detail message.
38+
* @return a new UncheckedIOException.
39+
*/
40+
public static UncheckedIOException create(Object message) {
41+
final String string = Objects.toString(message);
42+
return new UncheckedIOException(string, new IOException(string));
43+
}
44+
45+
/**
46+
* Creates a new UncheckedIOException for the given detail message.
47+
* <p>
48+
* This method exists because there is no String constructor in UncheckedIOException.
49+
* </p>
50+
*
51+
* @param message the detail message.
52+
* @param e cause the {@code IOException}.
53+
* @return a new UncheckedIOException.
54+
*/
55+
public static UncheckedIOException create(Object message, final IOException e) {
56+
return new UncheckedIOException(Objects.toString(message), e);
57+
}
58+
59+
}

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.commons.io.Charsets;
6363
import org.apache.commons.io.IOExceptionList;
6464
import org.apache.commons.io.IOUtils;
65+
import org.apache.commons.io.UncheckedIOExceptions;
6566
import org.apache.commons.io.file.Counters.PathCounters;
6667
import org.apache.commons.io.filefilter.IOFileFilter;
6768

@@ -858,7 +859,7 @@ public static BasicFileAttributes readBasicFileAttributesUnchecked(final Path pa
858859
try {
859860
return readBasicFileAttributes(path);
860861
} catch (final IOException e) {
861-
throw new UncheckedIOException(e);
862+
throw UncheckedIOExceptions.create(path, e);
862863
}
863864
}
864865

src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.UncheckedIOException;
2222
import java.util.Objects;
2323

24+
import org.apache.commons.io.UncheckedIOExceptions;
25+
2426
/**
2527
* An {@link Appendable} implementation that throws {@link UncheckedIOException} instead of {@link IOException}.
2628
*
@@ -43,7 +45,7 @@ public UncheckedAppendable append(final char c) {
4345
try {
4446
appendable.append(c);
4547
} catch (final IOException e) {
46-
throw new UncheckedIOException(String.valueOf(c), e);
48+
throw UncheckedIOExceptions.create(c, e);
4749
}
4850
return this;
4951
}
@@ -53,7 +55,7 @@ public UncheckedAppendable append(final CharSequence csq) {
5355
try {
5456
appendable.append(csq);
5557
} catch (final IOException e) {
56-
throw new UncheckedIOException(Objects.toString(csq), e);
58+
throw UncheckedIOExceptions.create(csq, e);
5759
}
5860
return this;
5961
}
@@ -63,7 +65,7 @@ public UncheckedAppendable append(final CharSequence csq, final int start, final
6365
try {
6466
appendable.append(csq, start, end);
6567
} catch (final IOException e) {
66-
throw new UncheckedIOException(Objects.toString(csq), e);
68+
throw UncheckedIOExceptions.create(csq, e);
6769
}
6870
return this;
6971
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
import java.io.IOException;
23+
import java.io.UncheckedIOException;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
public class UncheckedIOExceptionsTest {
28+
29+
@Test
30+
public void testCreate() {
31+
final Object message = "test";
32+
try {
33+
throw UncheckedIOExceptions.create(message);
34+
} catch (UncheckedIOException e) {
35+
assertEquals(message, e.getMessage());
36+
assertEquals(message, e.getCause().getMessage());
37+
}
38+
39+
}
40+
41+
@Test
42+
public void testCreateWithException() {
43+
final Object message1 = "test1";
44+
final Object message2 = "test2";
45+
final IOException ioe = new IOException(message2.toString());
46+
try {
47+
throw UncheckedIOExceptions.create(message1, ioe);
48+
} catch (UncheckedIOException e) {
49+
assertEquals(message1, e.getMessage());
50+
assertEquals(message2, e.getCause().getMessage());
51+
}
52+
53+
}
54+
}

0 commit comments

Comments
 (0)