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 ee6c5fd [IO-635] Add org.apache.commons.io.IOUtils.close(Closeable).
ee6c5fd is described below
commit ee6c5fd16d20e8c6ba1af776f6fdc30615a50c72
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Nov 5 10:45:13 2019 -0500
[IO-635] Add org.apache.commons.io.IOUtils.close(Closeable).
---
src/changes/changes.xml | 3 +
src/main/java/org/apache/commons/io/IOUtils.java | 73 +++++++++++-------------
2 files changed, 36 insertions(+), 40 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 09d6f2d..cb053b2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -158,6 +158,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-634" dev="ggregory" type="update" due-to="Václav
Haisman, Bruno P. Kinoshita, Gary Gregory">
Make getCause synchronized and use a Deque instead of a Stack #64.
</action>
+ <action issue="IO-635" dev="ggregory" type="add" due-to="Gary Gregory">
+ Add org.apache.commons.io.IOUtils.close(Closeable).
+ </action>
</release>
<release version="2.6" date="2017-10-15" description="Java 7 required,
Java 9 supported.">
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java
b/src/main/java/org/apache/commons/io/IOUtils.java
index 56b83f0..597f224 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -302,18 +302,6 @@ public class IOUtils {
}
/**
- * Closes a URLConnection.
- *
- * @param conn the connection to close.
- * @since 2.4
- */
- public static void close(final URLConnection conn) {
- if (conn instanceof HttpURLConnection) {
- ((HttpURLConnection) conn).disconnect();
- }
- }
-
- /**
* Closes a <code>Closeable</code> unconditionally.
* <p>
* Equivalent to {@link Closeable#close()}, except any exceptions will be
ignored. This is typically used in
@@ -355,15 +343,38 @@ public class IOUtils {
@Deprecated
public static void closeQuietly(final Closeable closeable) {
try {
- if (closeable != null) {
- closeable.close();
- }
+ close(closeable);
} catch (final IOException ioe) {
// ignore
}
}
/**
+ * Closes the given {@link Closeable} as a null-safe operation.
+ *
+ * @param closeable The resource to close, may be null.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.7
+ */
+ public static void close(final Closeable closeable) throws IOException {
+ if (closeable != null) {
+ closeable.close();
+ }
+ }
+
+ /**
+ * Closes a URLConnection.
+ *
+ * @param conn the connection to close.
+ * @since 2.4
+ */
+ public static void close(final URLConnection conn) {
+ if (conn instanceof HttpURLConnection) {
+ ((HttpURLConnection) conn).disconnect();
+ }
+ }
+
+ /**
* Closes a <code>Closeable</code> unconditionally.
* <p>
* Equivalent to {@link Closeable#close()}, except any exceptions will be
ignored.
@@ -546,13 +557,7 @@ public class IOUtils {
*/
@Deprecated
public static void closeQuietly(final Selector selector) {
- if (selector != null) {
- try {
- selector.close();
- } catch (final IOException ioe) {
- // ignored
- }
- }
+ closeQuietly((Closeable) selector);
}
/**
@@ -575,7 +580,7 @@ public class IOUtils {
* }
* </pre>
*
- * @param sock the ServerSocket to close, may be null or already closed
+ * @param serverSocket the ServerSocket to close, may be null or already
closed
* @since 2.2
*
* @deprecated As of 2.6 removed without replacement. Please use the
try-with-resources statement or handle
@@ -583,14 +588,8 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
- public static void closeQuietly(final ServerSocket sock) {
- if (sock != null) {
- try {
- sock.close();
- } catch (final IOException ioe) {
- // ignored
- }
- }
+ public static void closeQuietly(final ServerSocket serverSocket) {
+ closeQuietly((Closeable) serverSocket);
}
/**
@@ -613,7 +612,7 @@ public class IOUtils {
* }
* </pre>
*
- * @param sock the Socket to close, may be null or already closed
+ * @param socket the Socket to close, may be null or already closed
* @since 2.0
*
* @deprecated As of 2.6 removed without replacement. Please use the
try-with-resources statement or handle
@@ -621,14 +620,8 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
- public static void closeQuietly(final Socket sock) {
- if (sock != null) {
- try {
- sock.close();
- } catch (final IOException ioe) {
- // ignored
- }
- }
+ public static void closeQuietly(final Socket socket) {
+ closeQuietly((Closeable) socket);
}
/**