Skip to content

Commit c1d92d8

Browse files
committed
[IO-505] Make LineIterator implement Closeable to support try-with-resources statements.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1742672 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0c36127 commit c1d92d8

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ The <action> type attribute can be add,update,fix,remove.
5656
<action issue="IO-506" dev="ggregory" type="update" due-to="Christian Schulte">
5757
Deprecate methods FileSystemUtils.freeSpaceKb().
5858
</action>
59+
<action issue="IO-505" dev="ggregory" type="update" due-to="Christian Schulte">
60+
Make LineIterator implement Closeable to support try-with-resources statements.
61+
</action>
5962
</release>
6063
<release version="2.5" date="2016-04-22" description="New features and bug fixes.">
6164
<action issue="IO-492" dev="ggregory" type="fix" due-to="Santiago Castro">

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.io;
1818

1919
import java.io.BufferedReader;
20+
import java.io.Closeable;
2021
import java.io.IOException;
2122
import java.io.Reader;
2223
import java.util.Iterator;
@@ -47,7 +48,7 @@
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

Comments
 (0)