-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify BufferedLineReader by using LineNumberReader (#953)
BufferedLineReader had a TODO to be replaced by the LineNumberReader instead. This commit simplify the BufferedLineReader by extending LineNumberReader.
- Loading branch information
1 parent
1ade6c9
commit a0e38a9
Showing
1 changed file
with
11 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,29 +29,24 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.LineNumberReader; | ||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* Implementation of LineReader that is a thin wrapper around BufferedReader. On Linux, this is faster | ||
* than AsciiLineReaderImpl. If you use AsciiLineReader rather than this class, it will detect the OS | ||
* and delegate to the preferred implementation. | ||
* | ||
* TODO: Replace this with {@link java.io.LineNumberReader}? | ||
* | ||
* @author [email protected] | ||
*/ | ||
public class BufferedLineReader implements LineReader { | ||
|
||
private final BufferedReader reader; | ||
private int lineNumber = 0; | ||
private String peekedLine; | ||
public class BufferedLineReader extends LineNumberReader implements LineReader { | ||
|
||
public BufferedLineReader(final InputStream is) { | ||
this(is, Defaults.NON_ZERO_BUFFER_SIZE); | ||
} | ||
|
||
public BufferedLineReader(final InputStream is, final int bufferSize) { | ||
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")), bufferSize); | ||
super(new InputStreamReader(is, Charset.forName("UTF-8")), bufferSize); | ||
} | ||
|
||
/** | ||
|
@@ -61,57 +56,34 @@ public BufferedLineReader(final InputStream is, final int bufferSize) { | |
*/ | ||
@Override | ||
public String readLine() { | ||
++lineNumber; | ||
try { | ||
final String ret; | ||
if (peekedLine != null) { | ||
ret = peekedLine; | ||
peekedLine = null; | ||
} else { | ||
ret = reader.readLine(); | ||
} | ||
return ret; | ||
return super.readLine(); | ||
} catch (IOException e) { | ||
throw new RuntimeIOException(e); | ||
} | ||
} | ||
|
||
/** | ||
* @return 1-based number of line most recently read | ||
*/ | ||
@Override | ||
public int getLineNumber() { | ||
return lineNumber; | ||
} | ||
|
||
/** | ||
* Non-destructive one-character look-ahead. | ||
* | ||
* @return If not eof, the next character that would be read. If eof, -1. | ||
*/ | ||
@Override | ||
public int peek() { | ||
if (peekedLine == null) { | ||
try { | ||
peekedLine = reader.readLine(); | ||
} catch (IOException e) { | ||
try { | ||
mark(1); | ||
final int ret = read(); | ||
reset(); | ||
return ret; | ||
} catch (IOException e) { | ||
throw new RuntimeIOException(e); | ||
} | ||
} | ||
if (peekedLine == null) { | ||
return -1; | ||
} | ||
if (peekedLine.isEmpty()) { | ||
return '\n'; | ||
} | ||
return peekedLine.charAt(0); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
peekedLine = null; | ||
try { | ||
reader.close(); | ||
super.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeIOException(e); | ||
} | ||
|