You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/05/06 23:38:23 UTC

svn commit: r1742672 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/LineIterator.java

Author: ggregory
Date: Fri May  6 23:38:23 2016
New Revision: 1742672

URL: http://svn.apache.org/viewvc?rev=1742672&view=rev
Log:
[IO-505] Make LineIterator implement Closeable to support try-with-resources statements.

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/LineIterator.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1742672&r1=1742671&r2=1742672&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Fri May  6 23:38:23 2016
@@ -56,6 +56,9 @@ The <action> type attribute can be add,u
       <action issue="IO-506" dev="ggregory" type="update" due-to="Christian Schulte">
         Deprecate methods FileSystemUtils.freeSpaceKb().
       </action>
+      <action issue="IO-505" dev="ggregory" type="update" due-to="Christian Schulte">
+        Make LineIterator implement Closeable to support try-with-resources statements.
+      </action>
     </release>
     <release version="2.5" date="2016-04-22" description="New features and bug fixes.">
       <action issue="IO-492" dev="ggregory" type="fix" due-to="Santiago Castro">

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/LineIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/LineIterator.java?rev=1742672&r1=1742671&r2=1742672&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/LineIterator.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/LineIterator.java Fri May  6 23:38:23 2016
@@ -17,6 +17,7 @@
 package org.apache.commons.io;
 
 import java.io.BufferedReader;
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.Reader;
 import java.util.Iterator;
@@ -47,7 +48,7 @@ import java.util.NoSuchElementException;
  * @version $Id$
  * @since 1.2
  */
-public class LineIterator implements Iterator<String> {
+public class LineIterator implements Iterator<String>, Closeable {
 
     // N.B. This class deliberately does not implement Iterable, see https://issues.apache.org/jira/browse/IO-181
 
@@ -102,7 +103,11 @@ public class LineIterator implements Ite
                     }
                 }
             } catch(final IOException ioe) {
-                close();
+                try {
+                    close();
+                } catch (final IOException e) {
+                    ioe.addSuppressed(e);
+                }
                 throw new IllegalStateException(ioe);
             }
         }
@@ -144,16 +149,21 @@ public class LineIterator implements Ite
     }
 
     /**
-     * Closes the underlying <code>Reader</code> quietly.
+     * Closes the underlying {@code Reader}.
      * This method is useful if you only want to process the first few
      * lines of a larger file. If you do not close the iterator
-     * then the <code>Reader</code> remains open.
+     * then the {@code Reader} remains open.
      * This method can safely be called multiple times.
+     *
+     * @throws IOException if closing the underlying {@code Reader} fails.
      */
-    public void close() {
+    @Override
+    public void close() throws IOException {
         finished = true;
-        IOUtils.closeQuietly(bufferedReader);
         cachedLine = null;
+        if (this.bufferedReader != null) {
+            this.bufferedReader.close();
+        }
     }
 
     /**
@@ -167,13 +177,21 @@ public class LineIterator implements Ite
 
     //-----------------------------------------------------------------------
     /**
-     * Closes the iterator, handling null and ignoring exceptions.
+     * Closes a {@code LineIterator} quietly.
      *
-     * @param iterator  the iterator to close
+     * @param iterator The iterator to close, or {@code null}.
+     * @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
+     * suppressed exceptions manually.
+     * @see Throwable#addSuppressed(java.lang.Throwable)
      */
+    @Deprecated
     public static void closeQuietly(final LineIterator iterator) {
-        if (iterator != null) {
-            iterator.close();
+        try {
+            if (iterator != null) {
+                iterator.close();
+            }
+        } catch(final IOException e) {
+            // Suppressed.
         }
     }