Skip to content

Commit 4dc97b6

Browse files
committed
[IO-505] Deprecated of all IOUtils.closeQuietly() methods and use try-with-resources internally.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1742675 13f79535-47bb-0310-9956-ffa450edef68
1 parent 842bf6f commit 4dc97b6

8 files changed

Lines changed: 167 additions & 223 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ The <action> type attribute can be add,update,fix,remove.
5959
<action issue="IO-505" dev="ggregory" type="update" due-to="Christian Schulte">
6060
Make LineIterator implement Closeable to support try-with-resources statements.
6161
</action>
62+
<action issue="IO-505" dev="ggregory" type="update" due-to="Christian Schulte">
63+
Deprecated of all IOUtils.closeQuietly() methods and use try-with-resources internally.
64+
</action>
6265
<action issue="IO-507" dev="ggregory" type="new">
6366
Add a ByteOrderFactory class.
6467
</action>

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

Lines changed: 37 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ public static String byteCountToDisplaySize(final long size) {
432432
*/
433433
public static void touch(final File file) throws IOException {
434434
if (!file.exists()) {
435-
final OutputStream out = openOutputStream(file);
436-
IOUtils.closeQuietly(out);
435+
openOutputStream(file).close();
437436
}
438437
final boolean success = file.setLastModified(System.currentTimeMillis());
439438
if (!success) {
@@ -745,16 +744,9 @@ public static boolean contentEquals(final File file1, final File file2) throws I
745744
return true;
746745
}
747746

748-
InputStream input1 = null;
749-
InputStream input2 = null;
750-
try {
751-
input1 = new FileInputStream(file1);
752-
input2 = new FileInputStream(file2);
747+
try (InputStream input1 = new FileInputStream(file1);
748+
InputStream input2 = new FileInputStream(file2)) {
753749
return IOUtils.contentEquals(input1, input2);
754-
755-
} finally {
756-
IOUtils.closeQuietly(input1);
757-
IOUtils.closeQuietly(input2);
758750
}
759751
}
760752

@@ -798,22 +790,13 @@ public static boolean contentEqualsIgnoreEOL(final File file1, final File file2,
798790
return true;
799791
}
800792

801-
Reader input1 = null;
802-
Reader input2 = null;
803-
try {
804-
if (charsetName == null) {
805-
// N.B. make explicit the use of the default charset
806-
input1 = new InputStreamReader(new FileInputStream(file1), Charset.defaultCharset());
807-
input2 = new InputStreamReader(new FileInputStream(file2), Charset.defaultCharset());
808-
} else {
809-
input1 = new InputStreamReader(new FileInputStream(file1), charsetName);
810-
input2 = new InputStreamReader(new FileInputStream(file2), charsetName);
811-
}
793+
try (Reader input1 = charsetName == null
794+
? new InputStreamReader(new FileInputStream(file1), Charset.defaultCharset())
795+
: new InputStreamReader(new FileInputStream(file1), charsetName);
796+
Reader input2 = charsetName == null
797+
? new InputStreamReader(new FileInputStream(file2), Charset.defaultCharset())
798+
: new InputStreamReader(new FileInputStream(file2), charsetName)) {
812799
return IOUtils.contentEqualsIgnoreEOL(input1, input2);
813-
814-
} finally {
815-
IOUtils.closeQuietly(input1);
816-
IOUtils.closeQuietly(input2);
817800
}
818801
}
819802

@@ -1133,15 +1116,10 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
11331116
throw new IOException("Destination '" + destFile + "' exists but is a directory");
11341117
}
11351118

1136-
FileInputStream fis = null;
1137-
FileOutputStream fos = null;
1138-
FileChannel input = null;
1139-
FileChannel output = null;
1140-
try {
1141-
fis = new FileInputStream(srcFile);
1142-
fos = new FileOutputStream(destFile);
1143-
input = fis.getChannel();
1144-
output = fos.getChannel();
1119+
try (FileInputStream fis = new FileInputStream(srcFile);
1120+
FileChannel input = fis.getChannel();
1121+
FileOutputStream fos = new FileOutputStream(destFile);
1122+
FileChannel output = fos.getChannel()) {
11451123
final long size = input.size(); // TODO See IO-386
11461124
long pos = 0;
11471125
long count = 0;
@@ -1154,20 +1132,6 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
11541132
}
11551133
pos += bytesCopied;
11561134
}
1157-
1158-
output.close();
1159-
output = null;
1160-
1161-
fos.close();
1162-
fos = null;
1163-
1164-
input.close();
1165-
input = null;
1166-
1167-
fis.close();
1168-
fis = null;
1169-
} finally {
1170-
IOUtils.closeQuietly(output, fos, input, fis);
11711135
}
11721136

11731137
final long srcLen = srcFile.length(); // TODO See IO-386
@@ -1536,10 +1500,8 @@ public static void copyURLToFile(final URL source, final File destination,
15361500
* @since 2.0
15371501
*/
15381502
public static void copyInputStreamToFile(final InputStream source, final File destination) throws IOException {
1539-
try {
1540-
copyToFile(source, destination);
1541-
} finally {
1542-
IOUtils.closeQuietly(source);
1503+
try (InputStream in = source) {
1504+
copyToFile(in, destination);
15431505
}
15441506
}
15451507

@@ -1561,12 +1523,9 @@ public static void copyInputStreamToFile(final InputStream source, final File de
15611523
* @since 2.5
15621524
*/
15631525
public static void copyToFile(final InputStream source, final File destination) throws IOException {
1564-
final FileOutputStream output = openOutputStream(destination);
1565-
try {
1566-
IOUtils.copy(source, output);
1567-
output.close(); // don't swallow close Exception if copy completes normally
1568-
} finally {
1569-
IOUtils.closeQuietly(output);
1526+
try (InputStream in = source;
1527+
OutputStream out = openOutputStream(destination)) {
1528+
IOUtils.copy(in, out);
15701529
}
15711530
}
15721531

@@ -1772,12 +1731,8 @@ public static boolean waitFor(final File file, final int seconds) {
17721731
* @since 2.3
17731732
*/
17741733
public static String readFileToString(final File file, final Charset encoding) throws IOException {
1775-
InputStream in = null;
1776-
try {
1777-
in = openInputStream(file);
1734+
try (InputStream in = openInputStream(file)) {
17781735
return IOUtils.toString(in, Charsets.toCharset(encoding));
1779-
} finally {
1780-
IOUtils.closeQuietly(in);
17811736
}
17821737
}
17831738

@@ -1822,12 +1777,8 @@ public static String readFileToString(final File file) throws IOException {
18221777
* @since 1.1
18231778
*/
18241779
public static byte[] readFileToByteArray(final File file) throws IOException {
1825-
InputStream in = null;
1826-
try {
1827-
in = openInputStream(file);
1780+
try (InputStream in = openInputStream(file)) {
18281781
return IOUtils.toByteArray(in); // Do NOT use file.length() - see IO-453
1829-
} finally {
1830-
IOUtils.closeQuietly(in);
18311782
}
18321783
}
18331784

@@ -1842,12 +1793,8 @@ public static byte[] readFileToByteArray(final File file) throws IOException {
18421793
* @since 2.3
18431794
*/
18441795
public static List<String> readLines(final File file, final Charset encoding) throws IOException {
1845-
InputStream in = null;
1846-
try {
1847-
in = openInputStream(file);
1796+
try (InputStream in = openInputStream(file)) {
18481797
return IOUtils.readLines(in, Charsets.toCharset(encoding));
1849-
} finally {
1850-
IOUtils.closeQuietly(in);
18511798
}
18521799
}
18531800

@@ -1917,11 +1864,15 @@ public static LineIterator lineIterator(final File file, final String encoding)
19171864
try {
19181865
in = openInputStream(file);
19191866
return IOUtils.lineIterator(in, encoding);
1920-
} catch (final IOException ex) {
1921-
IOUtils.closeQuietly(in);
1922-
throw ex;
1923-
} catch (final RuntimeException ex) {
1924-
IOUtils.closeQuietly(in);
1867+
} catch (final IOException | RuntimeException ex) {
1868+
try {
1869+
if (in != null) {
1870+
in.close();
1871+
}
1872+
}
1873+
catch (final IOException e) {
1874+
ex.addSuppressed(e);
1875+
}
19251876
throw ex;
19261877
}
19271878
}
@@ -1986,15 +1937,10 @@ public static void writeStringToFile(final File file, final String data, final S
19861937
* @throws IOException in case of an I/O error
19871938
* @since 2.3
19881939
*/
1989-
public static void writeStringToFile(final File file, final String data, final Charset encoding, final boolean
1990-
append) throws IOException {
1991-
OutputStream out = null;
1992-
try {
1993-
out = openOutputStream(file, append);
1940+
public static void writeStringToFile(final File file, final String data, final Charset encoding,
1941+
final boolean append) throws IOException {
1942+
try (OutputStream out = openOutputStream(file, append)) {
19941943
IOUtils.write(data, out, encoding);
1995-
out.close(); // don't swallow close Exception if copy completes normally
1996-
} finally {
1997-
IOUtils.closeQuietly(out);
19981944
}
19991945
}
20001946

@@ -2200,13 +2146,8 @@ public static void writeByteArrayToFile(final File file, final byte[] data, fina
22002146
*/
22012147
public static void writeByteArrayToFile(final File file, final byte[] data, final int off, final int len,
22022148
final boolean append) throws IOException {
2203-
OutputStream out = null;
2204-
try {
2205-
out = openOutputStream(file, append);
2149+
try (OutputStream out = openOutputStream(file, append)) {
22062150
out.write(data, off, len);
2207-
out.close(); // don't swallow close Exception if copy completes normally
2208-
} finally {
2209-
IOUtils.closeQuietly(out);
22102151
}
22112152
}
22122153

@@ -2317,15 +2258,8 @@ public static void writeLines(final File file, final String encoding, final Coll
23172258
*/
23182259
public static void writeLines(final File file, final String encoding, final Collection<?> lines,
23192260
final String lineEnding, final boolean append) throws IOException {
2320-
FileOutputStream out = null;
2321-
try {
2322-
out = openOutputStream(file, append);
2323-
final BufferedOutputStream buffer = new BufferedOutputStream(out);
2324-
IOUtils.writeLines(lines, lineEnding, buffer, encoding);
2325-
buffer.flush();
2326-
out.close(); // don't swallow close Exception if copy completes normally
2327-
} finally {
2328-
IOUtils.closeQuietly(out);
2261+
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, append))) {
2262+
IOUtils.writeLines(lines, lineEnding, out, encoding);
23292263
}
23302264
}
23312265

@@ -2880,12 +2814,8 @@ public static Checksum checksum(final File file, final Checksum checksum) throws
28802814
if (file.isDirectory()) {
28812815
throw new IllegalArgumentException("Checksums can't be computed on directories");
28822816
}
2883-
InputStream in = null;
2884-
try {
2885-
in = new CheckedInputStream(new FileInputStream(file), checksum);
2817+
try (InputStream in = new CheckedInputStream(new FileInputStream(file), checksum)) {
28862818
IOUtils.copy(in, new NullOutputStream());
2887-
} finally {
2888-
IOUtils.closeQuietly(in);
28892819
}
28902820
return checksum;
28912821
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ public static void close(final URLConnection conn) {
212212
* </pre>
213213
*
214214
* @param input the Reader to close, may be null or already closed
215+
*
216+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
217+
* suppressed exceptions manually.
218+
* @see Throwable#addSuppressed(java.lang.Throwable)
215219
*/
220+
@Deprecated
216221
public static void closeQuietly(final Reader input) {
217222
closeQuietly((Closeable) input);
218223
}
@@ -238,7 +243,12 @@ public static void closeQuietly(final Reader input) {
238243
* </pre>
239244
*
240245
* @param output the Writer to close, may be null or already closed
246+
*
247+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
248+
* suppressed exceptions manually.
249+
* @see Throwable#addSuppressed(java.lang.Throwable)
241250
*/
251+
@Deprecated
242252
public static void closeQuietly(final Writer output) {
243253
closeQuietly((Closeable) output);
244254
}
@@ -265,7 +275,12 @@ public static void closeQuietly(final Writer output) {
265275
* </pre>
266276
*
267277
* @param input the InputStream to close, may be null or already closed
278+
*
279+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
280+
* suppressed exceptions manually.
281+
* @see Throwable#addSuppressed(java.lang.Throwable)
268282
*/
283+
@Deprecated
269284
public static void closeQuietly(final InputStream input) {
270285
closeQuietly((Closeable) input);
271286
}
@@ -293,7 +308,12 @@ public static void closeQuietly(final InputStream input) {
293308
* </pre>
294309
*
295310
* @param output the OutputStream to close, may be null or already closed
311+
*
312+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
313+
* suppressed exceptions manually.
314+
* @see Throwable#addSuppressed(java.lang.Throwable)
296315
*/
316+
@Deprecated
297317
public static void closeQuietly(final OutputStream output) {
298318
closeQuietly((Closeable) output);
299319
}
@@ -332,7 +352,12 @@ public static void closeQuietly(final OutputStream output) {
332352
*
333353
* @param closeable the objects to close, may be null or already closed
334354
* @since 2.0
355+
*
356+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
357+
* suppressed exceptions manually.
358+
* @see Throwable#addSuppressed(java.lang.Throwable)
335359
*/
360+
@Deprecated
336361
public static void closeQuietly(final Closeable closeable) {
337362
try {
338363
if (closeable != null) {
@@ -385,7 +410,12 @@ public static void closeQuietly(final Closeable closeable) {
385410
* @param closeables the objects to close, may be null or already closed
386411
* @see #closeQuietly(Closeable)
387412
* @since 2.5
413+
*
414+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
415+
* suppressed exceptions manually.
416+
* @see Throwable#addSuppressed(java.lang.Throwable)
388417
*/
418+
@Deprecated
389419
public static void closeQuietly(final Closeable... closeables) {
390420
if (closeables == null) {
391421
return;
@@ -417,7 +447,12 @@ public static void closeQuietly(final Closeable... closeables) {
417447
*
418448
* @param sock the Socket to close, may be null or already closed
419449
* @since 2.0
450+
*
451+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
452+
* suppressed exceptions manually.
453+
* @see Throwable#addSuppressed(java.lang.Throwable)
420454
*/
455+
@Deprecated
421456
public static void closeQuietly(final Socket sock) {
422457
if (sock != null) {
423458
try {
@@ -450,7 +485,12 @@ public static void closeQuietly(final Socket sock) {
450485
*
451486
* @param selector the Selector to close, may be null or already closed
452487
* @since 2.2
488+
*
489+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
490+
* suppressed exceptions manually.
491+
* @see Throwable#addSuppressed(java.lang.Throwable)
453492
*/
493+
@Deprecated
454494
public static void closeQuietly(final Selector selector) {
455495
if (selector != null) {
456496
try {
@@ -483,7 +523,12 @@ public static void closeQuietly(final Selector selector) {
483523
*
484524
* @param sock the ServerSocket to close, may be null or already closed
485525
* @since 2.2
526+
*
527+
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
528+
* suppressed exceptions manually.
529+
* @see Throwable#addSuppressed(java.lang.Throwable)
486530
*/
531+
@Deprecated
487532
public static void closeQuietly(final ServerSocket sock) {
488533
if (sock != null) {
489534
try {

0 commit comments

Comments
 (0)