1717package org .apache .commons .io ;
1818
1919import java .io .BufferedReader ;
20+ import java .io .Closeable ;
2021import java .io .IOException ;
2122import java .io .Reader ;
2223import java .util .Iterator ;
4748 * @version $Id$
4849 * @since 1.2
4950 */
50- public class LineIterator implements Iterator <String > {
51+ public class LineIterator implements Iterator <String >, Closeable {
5152
5253 // N.B. This class deliberately does not implement Iterable, see https://issues.apache.org/jira/browse/IO-181
5354
@@ -102,7 +103,11 @@ public boolean hasNext() {
102103 }
103104 }
104105 } catch (final IOException ioe ) {
105- close ();
106+ try {
107+ close ();
108+ } catch (final IOException e ) {
109+ ioe .addSuppressed (e );
110+ }
106111 throw new IllegalStateException (ioe );
107112 }
108113 }
@@ -144,16 +149,21 @@ public String nextLine() {
144149 }
145150
146151 /**
147- * Closes the underlying < code>Reader</code> quietly .
152+ * Closes the underlying {@ code Reader} .
148153 * This method is useful if you only want to process the first few
149154 * lines of a larger file. If you do not close the iterator
150- * then the < code> Reader</code> remains open.
155+ * then the {@ code Reader} remains open.
151156 * This method can safely be called multiple times.
157+ *
158+ * @throws IOException if closing the underlying {@code Reader} fails.
152159 */
153- public void close () {
160+ @ Override
161+ public void close () throws IOException {
154162 finished = true ;
155- IOUtils .closeQuietly (bufferedReader );
156163 cachedLine = null ;
164+ if (this .bufferedReader != null ) {
165+ this .bufferedReader .close ();
166+ }
157167 }
158168
159169 /**
@@ -167,13 +177,21 @@ public void remove() {
167177
168178 //-----------------------------------------------------------------------
169179 /**
170- * Closes the iterator, handling null and ignoring exceptions .
180+ * Closes a {@code LineIterator} quietly .
171181 *
172- * @param iterator the iterator to close
182+ * @param iterator The iterator to close, or {@code null}.
183+ * @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
184+ * suppressed exceptions manually.
185+ * @see Throwable#addSuppressed(java.lang.Throwable)
173186 */
187+ @ Deprecated
174188 public static void closeQuietly (final LineIterator iterator ) {
175- if (iterator != null ) {
176- iterator .close ();
189+ try {
190+ if (iterator != null ) {
191+ iterator .close ();
192+ }
193+ } catch (final IOException e ) {
194+ // Suppressed.
177195 }
178196 }
179197
0 commit comments