Skip to content

Commit ee6c5fd

Browse files
author
Gary Gregory
committed
[IO-635] Add org.apache.commons.io.IOUtils.close(Closeable).
1 parent 5f83d30 commit ee6c5fd

2 files changed

Lines changed: 36 additions & 40 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ The <action> type attribute can be add,update,fix,remove.
158158
<action issue="IO-634" dev="ggregory" type="update" due-to="Václav Haisman, Bruno P. Kinoshita, Gary Gregory">
159159
Make getCause synchronized and use a Deque instead of a Stack #64.
160160
</action>
161+
<action issue="IO-635" dev="ggregory" type="add" due-to="Gary Gregory">
162+
Add org.apache.commons.io.IOUtils.close(Closeable).
163+
</action>
161164
</release>
162165

163166
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

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

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,6 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
301301
return writer instanceof BufferedWriter ? (BufferedWriter) writer : new BufferedWriter(writer, size);
302302
}
303303

304-
/**
305-
* Closes a URLConnection.
306-
*
307-
* @param conn the connection to close.
308-
* @since 2.4
309-
*/
310-
public static void close(final URLConnection conn) {
311-
if (conn instanceof HttpURLConnection) {
312-
((HttpURLConnection) conn).disconnect();
313-
}
314-
}
315-
316304
/**
317305
* Closes a <code>Closeable</code> unconditionally.
318306
* <p>
@@ -355,14 +343,37 @@ public static void close(final URLConnection conn) {
355343
@Deprecated
356344
public static void closeQuietly(final Closeable closeable) {
357345
try {
358-
if (closeable != null) {
359-
closeable.close();
360-
}
346+
close(closeable);
361347
} catch (final IOException ioe) {
362348
// ignore
363349
}
364350
}
365351

352+
/**
353+
* Closes the given {@link Closeable} as a null-safe operation.
354+
*
355+
* @param closeable The resource to close, may be null.
356+
* @throws IOException if an I/O error occurs.
357+
* @since 2.7
358+
*/
359+
public static void close(final Closeable closeable) throws IOException {
360+
if (closeable != null) {
361+
closeable.close();
362+
}
363+
}
364+
365+
/**
366+
* Closes a URLConnection.
367+
*
368+
* @param conn the connection to close.
369+
* @since 2.4
370+
*/
371+
public static void close(final URLConnection conn) {
372+
if (conn instanceof HttpURLConnection) {
373+
((HttpURLConnection) conn).disconnect();
374+
}
375+
}
376+
366377
/**
367378
* Closes a <code>Closeable</code> unconditionally.
368379
* <p>
@@ -546,13 +557,7 @@ public static void closeQuietly(final Reader input) {
546557
*/
547558
@Deprecated
548559
public static void closeQuietly(final Selector selector) {
549-
if (selector != null) {
550-
try {
551-
selector.close();
552-
} catch (final IOException ioe) {
553-
// ignored
554-
}
555-
}
560+
closeQuietly((Closeable) selector);
556561
}
557562

558563
/**
@@ -575,22 +580,16 @@ public static void closeQuietly(final Selector selector) {
575580
* }
576581
* </pre>
577582
*
578-
* @param sock the ServerSocket to close, may be null or already closed
583+
* @param serverSocket the ServerSocket to close, may be null or already closed
579584
* @since 2.2
580585
*
581586
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
582587
* suppressed exceptions manually.
583588
* @see Throwable#addSuppressed(java.lang.Throwable)
584589
*/
585590
@Deprecated
586-
public static void closeQuietly(final ServerSocket sock) {
587-
if (sock != null) {
588-
try {
589-
sock.close();
590-
} catch (final IOException ioe) {
591-
// ignored
592-
}
593-
}
591+
public static void closeQuietly(final ServerSocket serverSocket) {
592+
closeQuietly((Closeable) serverSocket);
594593
}
595594

596595
/**
@@ -613,22 +612,16 @@ public static void closeQuietly(final ServerSocket sock) {
613612
* }
614613
* </pre>
615614
*
616-
* @param sock the Socket to close, may be null or already closed
615+
* @param socket the Socket to close, may be null or already closed
617616
* @since 2.0
618617
*
619618
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
620619
* suppressed exceptions manually.
621620
* @see Throwable#addSuppressed(java.lang.Throwable)
622621
*/
623622
@Deprecated
624-
public static void closeQuietly(final Socket sock) {
625-
if (sock != null) {
626-
try {
627-
sock.close();
628-
} catch (final IOException ioe) {
629-
// ignored
630-
}
631-
}
623+
public static void closeQuietly(final Socket socket) {
624+
closeQuietly((Closeable) socket);
632625
}
633626

634627
/**

0 commit comments

Comments
 (0)