Skip to content

Commit 62ae9ca

Browse files
author
Sandy McArthur Jr
committed
Cleaned up LineIterator changes include:
* Removed the IOIterator interface, it can be added back later when there is more than one Iterator implementations with a close method. * Doesn't automatically close the Reader at EOF. * made LineIterator final because the hasNext method isn't implemented in a subclassable way. * constructor throws an IllegaArgumentException, not a NPE when the argument is bogus. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@383730 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2f23904 commit 62ae9ca

5 files changed

Lines changed: 120 additions & 216 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
2626
import java.io.UnsupportedEncodingException;
27+
import java.io.FileReader;
28+
import java.io.Reader;
2729
import java.net.URL;
2830
import java.util.Collection;
2931
import java.util.Date;
@@ -923,22 +925,9 @@ public static List readLines(File file, String encoding) throws IOException {
923925

924926
/**
925927
* Return an Iterator for the lines in a <code>File</code>.
926-
* Please read the javadoc of {@link LineIterator} to understand
927-
* whether you should close the iterator.
928-
* The file is closed if an exception is thrown during this method.
929-
* <p>
930-
* The recommended usage patterm is:
931-
* <pre>
932-
* LineIterator it = FileUtils.lineIterator(file, "UTF-8");
933-
* try {
934-
* while (it.hasNext()) {
935-
* String line = it.nextLine();
936-
* /// do something with line
937-
* }
938-
* } finally {
939-
* LineIterator.closeQuietly(iterator);
940-
* }
941-
* </pre>
928+
* This neccessitates creating an InputStream for the file. The only ways
929+
* to close this stream are to call {@link LineIterator#close()} or let
930+
* the <code>LineIterator</code> be garbage collected.
942931
* <p>
943932
* There is no lineIterator method without encoding parameter because
944933
* the default encoding can differ between platforms and will have

src/java/org/apache/commons/io/IOIterator.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -509,21 +509,9 @@ public static List readLines(Reader input) throws IOException {
509509
//-----------------------------------------------------------------------
510510
/**
511511
* Return an Iterator for the lines in a <code>Reader</code>.
512-
* Please read the javadoc of {@link LineIterator} to understand
513-
* whether you should close the iterator.
514-
* <p>
515-
* The recommended usage pattern is:
516-
* <pre>
517-
* LineIterator it = IOUtils.lineIterator(reader);
518-
* try {
519-
* while (it.hasNext()) {
520-
* String line = it.nextLine();
521-
* /// do something with line
522-
* }
523-
* } finally {
524-
* LineIterator.closeQuietly(iterator);
525-
* }
526-
* </pre>
512+
* Unless you keep a reference to the <code>InputStream</code> the
513+
* only way to close it is to call {@link LineIterator#close()} or
514+
* wait for the garbage collector.
527515
*
528516
* @param reader the <code>Reader</code> to read from, not null
529517
* @return an Iterator of the lines in the reader, never null
@@ -537,21 +525,9 @@ public static LineIterator lineIterator(Reader reader) {
537525
/**
538526
* Return an Iterator for the lines in an <code>InputStream</code>, using
539527
* the character encoding specified (or default encoding if null).
540-
* Please read the javadoc of {@link LineIterator} to understand
541-
* whether you should close the iterator.
542-
* <p>
543-
* The recommended usage pattern is:
544-
* <pre>
545-
* LineIterator it = IOUtils.lineIterator(stream, "UTF-8");
546-
* try {
547-
* while (it.hasNext()) {
548-
* String line = it.nextLine();
549-
* /// do something with line
550-
* }
551-
* } finally {
552-
* LineIterator.closeQuietly(iterator);
553-
* }
554-
* </pre>
528+
* Unless you keep a reference to the <code>InputStream</code> the
529+
* only way to close it is to call {@link LineIterator#close()} or
530+
* wait for the garbage collector.
555531
*
556532
* @param input the <code>InputStream</code> to read from, not null
557533
* @param encoding the encoding to use, null means platform default
@@ -562,7 +538,7 @@ public static LineIterator lineIterator(Reader reader) {
562538
*/
563539
public static LineIterator lineIterator(InputStream input, String encoding)
564540
throws IOException {
565-
InputStreamReader reader = null;
541+
Reader reader = null;
566542
if (encoding == null) {
567543
reader = new InputStreamReader(input);
568544
} else {

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,23 @@
2222

2323
/**
2424
* An Iterator over the lines in a <code>Reader</code>.
25+
*
2526
* <p>
26-
* This iterator must be closed after use to avoid a resource leak.
27-
* If you read every line, then the final {@link #hasNext()} method
28-
* will close the iterator. If you do not fully read the iterator
29-
* then you must call the {@link #close()} method.
30-
* <p>
31-
* However, since the iterator methods can throw exception, we recommend
32-
* always calling close in a finally block:
33-
* <pre>
34-
* LineIterator it = FileUtils.lineIterator(file, "UTF-8");
35-
* try {
36-
* while (it.hasNext()) {
37-
* String line = it.nextLine();
38-
* /// do something with line
39-
* }
40-
* } finally {
41-
* LineIterator.closeQuietly(iterator);
42-
* }
43-
* </pre>
27+
* If you do not wish to maintain a reference to the <code>Reader</code>
28+
* you can call {@link #close()} to close the backing <code>Reader</code>
29+
* and free an interal resources.
4430
*
4531
* @author Niall Pemberton
4632
* @author Stephen Colebourne
33+
* @author Sandy McArthur
4734
* @version $Id$
4835
* @since Commons IO 1.2
4936
*/
50-
public class LineIterator implements IOIterator {
37+
/*
38+
* XXX: hasNext() should be reworked so this class can be
39+
* meaningfully subclassed before the final below is removed.
40+
*/
41+
public final class LineIterator {
5142

5243
/** The reader that is being read. */
5344
private final BufferedReader bufferedReader;
@@ -60,22 +51,24 @@ public class LineIterator implements IOIterator {
6051
* Constructs an iterator of the lines for a <code>Reader</code>.
6152
*
6253
* @param reader the <code>Reader</code> to read from, not null
63-
* @throws NullPointerException if the reader is null
54+
* @throws IllegalArgumentException if the reader is null
6455
*/
65-
public LineIterator(Reader reader) {
56+
public LineIterator(final Reader reader) throws IllegalArgumentException {
6657
if (reader == null) {
67-
throw new NullPointerException("Reader must not be null");
58+
throw new IllegalArgumentException("Reader must not be null.");
6859
}
6960
if (reader instanceof BufferedReader) {
70-
this.bufferedReader = (BufferedReader) reader;
61+
bufferedReader = (BufferedReader) reader;
7162
} else {
72-
this.bufferedReader = new BufferedReader(reader);
63+
bufferedReader = new BufferedReader(reader);
7364
}
7465
}
7566

7667
//-----------------------------------------------------------------------
7768
/**
7869
* Indicates whether the <code>Reader</code> has more lines.
70+
* If there is an <code>IOException</code> then {@link #close()} will
71+
* be called on this instance.
7972
*
8073
* @return <code>true</code> if the Reader has more lines
8174
* @throws IllegalStateException if an IO exception occurs
@@ -89,7 +82,7 @@ public boolean hasNext() {
8982
try {
9083
cachedLine = bufferedReader.readLine();
9184
if (cachedLine == null) {
92-
close();
85+
finished = true;
9386
return false;
9487
} else {
9588
return true;
@@ -130,15 +123,13 @@ public String nextLine() {
130123
* Closes the underlying <code>Reader</code> quietly.
131124
* This method is useful if you only want to process the first few
132125
* lines of a larger file. If you do not close the iterator
133-
* then the <code>Reader</code> remains open and is a resource leak.
126+
* then the <code>Reader</code> remains open.
134127
* This method can safely be called multiple times.
135128
*/
136129
public void close() {
137-
if (!finished) {
138-
IOUtils.closeQuietly(bufferedReader);
139-
finished = true;
140-
cachedLine = null;
141-
}
130+
finished = true;
131+
IOUtils.closeQuietly(bufferedReader);
132+
cachedLine = null;
142133
}
143134

144135
/**

0 commit comments

Comments
 (0)