Skip to content

Commit 583638b

Browse files
committed
[IO-426] Add API IOUtils.closeQuietly(Closeable...)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1565317 13f79535-47bb-0310-9956-ffa450edef68
1 parent 279f0c6 commit 583638b

4 files changed

Lines changed: 89 additions & 26 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2014-??-??" description="New features and bug fixes.">
50+
<action issue="IO-426" dev="ggregory" type="add">
51+
Add API IOUtils.closeQuietly(Closeable...)
52+
</action>
5053
<action issue="IO-424" dev="ggregory" type="fix" due-to="Ville Skyttä">
5154
Javadoc fixes, mostly to appease 1.8.0
5255
</action>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,10 +1161,7 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
11611161
pos += bytesCopied;
11621162
}
11631163
} finally {
1164-
IOUtils.closeQuietly(output);
1165-
IOUtils.closeQuietly(fos);
1166-
IOUtils.closeQuietly(input);
1167-
IOUtils.closeQuietly(fis);
1164+
IOUtils.closeQuietly(output, fos, input, fis);
11681165
}
11691166

11701167
final long srcLen = srcFile.length(); // TODO See IO-386

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

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,24 +281,37 @@ public static void closeQuietly(final OutputStream output) {
281281
/**
282282
* Closes a <code>Closeable</code> unconditionally.
283283
* <p>
284-
* Equivalent to {@link Closeable#close()}, except any exceptions will be ignored.
285-
* This is typically used in finally blocks.
284+
* Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in
285+
* finally blocks.
286286
* <p>
287287
* Example code:
288+
*
288289
* <pre>
289-
* Closeable closeable = null;
290-
* try {
291-
* closeable = new FileReader("foo.txt");
292-
* // process closeable
293-
* closeable.close();
294-
* } catch (Exception e) {
295-
* // error handling
296-
* } finally {
297-
* IOUtils.closeQuietly(closeable);
298-
* }
290+
* Closeable closeable = null;
291+
* try {
292+
* closeable = new FileReader(&quot;foo.txt&quot;);
293+
* // process closeable
294+
* closeable.close();
295+
* } catch (Exception e) {
296+
* // error handling
297+
* } finally {
298+
* IOUtils.closeQuietly(closeable);
299+
* }
299300
* </pre>
300-
*
301-
* @param closeable the object to close, may be null or already closed
301+
*
302+
* Closing all streams:
303+
*
304+
* <pre>
305+
* try {
306+
* return IOUtils.copy(inputStream, outputStream);
307+
* } finally {
308+
* IOUtils.closeQuietly(inputStream);
309+
* IOUtils.closeQuietly(outputStream);
310+
* }
311+
* </pre>
312+
*
313+
* @param closeable
314+
* the objects to close, may be null or already closed
302315
* @since 2.0
303316
*/
304317
public static void closeQuietly(final Closeable closeable) {
@@ -311,6 +324,51 @@ public static void closeQuietly(final Closeable closeable) {
311324
}
312325
}
313326

327+
/**
328+
* Closes a <code>Closeable</code> unconditionally.
329+
* <p>
330+
* Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in
331+
* finally blocks.
332+
* <p>
333+
* Example code:
334+
*
335+
* <pre>
336+
* Closeable closeable = null;
337+
* try {
338+
* closeable = new FileReader(&quot;foo.txt&quot;);
339+
* // process closeable
340+
* closeable.close();
341+
* } catch (Exception e) {
342+
* // error handling
343+
* } finally {
344+
* IOUtils.closeQuietly(closeable);
345+
* }
346+
* </pre>
347+
*
348+
* Closing all streams:
349+
*
350+
* <pre>
351+
* try {
352+
* return IOUtils.copy(inputStream, outputStream);
353+
* } finally {
354+
* IOUtils.closeQuietly(inputStream);
355+
* IOUtils.closeQuietly(outputStream);
356+
* }
357+
* </pre>
358+
*
359+
* @param closeables
360+
* the objects to close, may be null or already closed
361+
* @since 2.5
362+
*/
363+
public static void closeQuietly(final Closeable... closeables) {
364+
if (closeables == null) {
365+
return;
366+
}
367+
for (final Closeable closeable : closeables) {
368+
closeQuietly(closeable);
369+
}
370+
}
371+
314372
/**
315373
* Closes a <code>Socket</code> unconditionally.
316374
* <p>

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public void tearDown() {
124124
}
125125
}
126126

127+
public void testCloseQuietly_AllCloseableIOException() {
128+
final Closeable closeable = new Closeable() {
129+
public void close() throws IOException {
130+
throw new IOException();
131+
}
132+
};
133+
IOUtils.closeQuietly(closeable, null, closeable);
134+
}
135+
127136
public void testCloseQuietly_CloseableIOException() {
128137
IOUtils.closeQuietly(new Closeable() {
129138
public void close() throws IOException {
@@ -614,8 +623,7 @@ public void testRead_ReadableByteChannel() throws Exception {
614623
// expected
615624
}
616625
} finally {
617-
IOUtils.closeQuietly(input);
618-
IOUtils.closeQuietly(fileInputStream);
626+
IOUtils.closeQuietly(input, fileInputStream);
619627
}}
620628

621629
public void testReadFully_InputStream_ByteArray() throws Exception {
@@ -684,8 +692,7 @@ public void testReadFully_ReadableByteChannel() throws Exception {
684692
// expected
685693
}
686694
} finally {
687-
IOUtils.closeQuietly(input);
688-
IOUtils.closeQuietly(fileInputStream);
695+
IOUtils.closeQuietly(input, fileInputStream);
689696
}
690697
}
691698

@@ -802,8 +809,7 @@ public void testSkip_ReadableByteChannel() throws Exception {
802809
assertEquals(10, IOUtils.skip(fileChannel, 20));
803810
assertEquals(0, IOUtils.skip(fileChannel, 10));
804811
} finally {
805-
IOUtils.closeQuietly(fileChannel);
806-
IOUtils.closeQuietly(fileInputStream);
812+
IOUtils.closeQuietly(fileChannel, fileInputStream);
807813
}
808814
}
809815

@@ -848,8 +854,7 @@ public void testSkipFully_ReadableByteChannel() throws Exception {
848854
// expected
849855
}
850856
} finally {
851-
IOUtils.closeQuietly(fileChannel);
852-
IOUtils.closeQuietly(fileInputStream);
857+
IOUtils.closeQuietly(fileChannel, fileInputStream);
853858
}
854859
}
855860

0 commit comments

Comments
 (0)