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 5b2c6ab  [IO-635] Add org.apache.commons.io.IOUtils.close(Closeable).
5b2c6ab is described below

commit 5b2c6abaebe7622ba93b836e6ae9249b7b9a93a7
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Tue Nov 5 11:04:46 2019 -0500

    [IO-635] Add org.apache.commons.io.IOUtils.close(Closeable).
    
    Reuse own API.
---
 src/main/java/org/apache/commons/io/FileUtils.java           | 10 ++++------
 src/main/java/org/apache/commons/io/LineIterator.java        | 12 ++----------
 .../java/org/apache/commons/io/input/DemuxInputStream.java   |  7 +++----
 .../java/org/apache/commons/io/output/DemuxOutputStream.java |  7 +++----
 .../org/apache/commons/io/output/FileWriterWithEncoding.java |  5 ++---
 5 files changed, 14 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/FileUtils.java 
b/src/main/java/org/apache/commons/io/FileUtils.java
index baf4ec8..dbbbcad 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1842,15 +1842,13 @@ public class FileUtils {
      * @since 1.2
      */
     public static LineIterator lineIterator(final File file, final String 
encoding) throws IOException {
-        InputStream in = null;
+        InputStream inputStream = null;
         try {
-            in = openInputStream(file);
-            return IOUtils.lineIterator(in, encoding);
+            inputStream = openInputStream(file);
+            return IOUtils.lineIterator(inputStream, encoding);
         } catch (final IOException | RuntimeException ex) {
             try {
-                if (in != null) {
-                    in.close();
-                }
+                IOUtils.close(inputStream);
             }
             catch (final IOException e) {
                 ex.addSuppressed(e);
diff --git a/src/main/java/org/apache/commons/io/LineIterator.java 
b/src/main/java/org/apache/commons/io/LineIterator.java
index 680a87e..986c72e 100644
--- a/src/main/java/org/apache/commons/io/LineIterator.java
+++ b/src/main/java/org/apache/commons/io/LineIterator.java
@@ -162,9 +162,7 @@ public class LineIterator implements Iterator<String>, 
Closeable {
     public void close() throws IOException {
         finished = true;
         cachedLine = null;
-        if (this.bufferedReader != null) {
-            this.bufferedReader.close();
-        }
+        IOUtils.close(bufferedReader);
     }
 
     /**
@@ -188,13 +186,7 @@ public class LineIterator implements Iterator<String>, 
Closeable {
      */
     @Deprecated
     public static void closeQuietly(final LineIterator iterator) {
-        try {
-            if (iterator != null) {
-                iterator.close();
-            }
-        } catch(final IOException e) {
-            // Suppressed.
-        }
+        IOUtils.closeQuietly(iterator);
     }
 
 }
diff --git a/src/main/java/org/apache/commons/io/input/DemuxInputStream.java 
b/src/main/java/org/apache/commons/io/input/DemuxInputStream.java
index dbdcc4c..8207ec6 100644
--- a/src/main/java/org/apache/commons/io/input/DemuxInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/DemuxInputStream.java
@@ -21,6 +21,8 @@ import static org.apache.commons.io.IOUtils.EOF;
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.commons.io.IOUtils;
+
 /**
  * Data written to this stream is forwarded to a stream that has been 
associated with this thread.
  *
@@ -47,10 +49,7 @@ public class DemuxInputStream extends InputStream {
      */
     @Override
     public void close() throws IOException {
-        final InputStream input = m_streams.get();
-        if (null != input) {
-            input.close();
-        }
+        IOUtils.close(m_streams.get());
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/io/output/DemuxOutputStream.java 
b/src/main/java/org/apache/commons/io/output/DemuxOutputStream.java
index 5eeb37d..acf0f0b 100644
--- a/src/main/java/org/apache/commons/io/output/DemuxOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/DemuxOutputStream.java
@@ -19,6 +19,8 @@ package org.apache.commons.io.output;
 import java.io.IOException;
 import java.io.OutputStream;
 
+import org.apache.commons.io.IOUtils;
+
 /**
  * Forwards data to a stream that has been associated with this thread.
  *
@@ -47,10 +49,7 @@ public class DemuxOutputStream extends OutputStream {
      */
     @Override
     public void close() throws IOException {
-        final OutputStream output = outputStreamThreadLocal.get();
-        if (null != output) {
-            output.close();
-        }
+        IOUtils.close(outputStreamThreadLocal.get());
     }
 
     /**
diff --git 
a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java 
b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
index b746ae5..0af67b9 100644
--- a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
+++ b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
@@ -26,6 +26,7 @@ import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 
 /**
  * Writer of files that allows the encoding to be set.
@@ -242,9 +243,7 @@ public class FileWriterWithEncoding extends Writer {
             }
         } catch (final IOException | RuntimeException ex) {
             try {
-                if (stream != null) {
-                    stream.close();
-                }
+                IOUtils.close(stream);
             } catch (final IOException e) {
                 ex.addSuppressed(e);
             }

Reply via email to