Applied patch contributed by Henri Yandell in SANDBOX-219: "ExtendedBufferedReader does too much"

git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1065948 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacopo Cappellato 2011-02-01 08:18:59 +00:00
parent c6bdecabd8
commit fe5bd51f8a
2 changed files with 7 additions and 142 deletions

View File

@ -29,9 +29,6 @@ import java.io.Reader;
* In particular the reader supports a look-ahead option,
* which allows you to see the next char returned by
* next().
* Furthermore the skip-method supports skipping until
* (but excluding) a given char. Similar functionality
* is supported by the reader as well.
*
*/
class ExtendedBufferedReader extends BufferedReader {
@ -150,29 +147,6 @@ class ExtendedBufferedReader extends BufferedReader {
return cOff - off;
}
/**
* Reads all characters up to (but not including) the given character.
*
* @param c the character to read up to
* @return the string up to the character <code>c</code>
* @throws IOException
*/
public String readUntil(char c) throws IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
line.clear(); // reuse
while (lookaheadChar != c && lookaheadChar != END_OF_STREAM) {
line.append((char) lookaheadChar);
if (lookaheadChar == '\n') {
lineCounter++;
}
lastChar = lookaheadChar;
lookaheadChar = super.read();
}
return line.toString();
}
/**
* @return A String containing the contents of the line, not
* including any line-termination characters, or null
@ -217,60 +191,10 @@ class ExtendedBufferedReader extends BufferedReader {
}
/**
* Skips char in the stream
*
* ATTENTION: invalidates the line-counter !!!!!
*
* @return nof skiped chars
* Unsupported
*/
public long skip(long n) throws IllegalArgumentException, IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
// illegal argument
if (n < 0) {
throw new IllegalArgumentException("negative argument not supported");
}
// no skipping
if (n == 0 || lookaheadChar == END_OF_STREAM) {
return 0;
}
// skip and reread the lookahead-char
long skiped = 0;
if (n > 1) {
skiped = super.skip(n - 1);
}
lookaheadChar = super.read();
// fixme uh: we should check the skiped sequence for line-terminations...
lineCounter = Integer.MIN_VALUE;
return skiped + 1;
}
/**
* Skips all chars in the input until (but excluding) the given char
*
* @param c
* @return
* @throws IllegalArgumentException
* @throws IOException
*/
public long skipUntil(char c) throws IllegalArgumentException, IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
long counter = 0;
while (lookaheadChar != c && lookaheadChar != END_OF_STREAM) {
if (lookaheadChar == '\n') {
lineCounter++;
}
lookaheadChar = super.read();
counter++;
}
return counter;
throw new UnsupportedOperationException("CSV has no reason to implement this");
}
/**
@ -291,7 +215,6 @@ class ExtendedBufferedReader extends BufferedReader {
/**
* Returns the nof line read
* ATTENTION: the skip-method does invalidate the line-number counter
*
* @return the current-line-number (or -1)
*/
@ -302,11 +225,12 @@ class ExtendedBufferedReader extends BufferedReader {
return -1;
}
}
/**
* Unsupported
*/
public boolean markSupported() {
/* note uh: marking is not supported, cause we cannot
* see into the future...
*/
return false;
throw new UnsupportedOperationException("CSV has no reason to implement this");
}
}

View File

@ -115,10 +115,6 @@ public class ExtendedBufferedReaderTest extends TestCase {
}
public void testMarkSupported() {
assertFalse(getEBR("foo").markSupported());
}
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine() == null);
@ -161,61 +157,6 @@ public class ExtendedBufferedReaderTest extends TestCase {
assertTrue(br.readLine() == null);
}
public void testSkip0() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertEquals(0, br.skip(0));
assertEquals(0, br.skip(1));
br = getEBR("");
assertEquals(0, br.skip(1));
br = getEBR("abcdefg");
assertEquals(0, br.skip(0));
assertEquals('a', br.lookAhead());
assertEquals(0, br.skip(0));
assertEquals('a', br.lookAhead());
assertEquals(1, br.skip(1));
assertEquals('b', br.lookAhead());
assertEquals('b', br.read());
assertEquals(3, br.skip(3));
assertEquals('f', br.lookAhead());
assertEquals(2, br.skip(5));
assertTrue(br.readLine() == null);
br = getEBR("12345");
assertEquals(5, br.skip(5));
assertTrue (br.lookAhead() == ExtendedBufferedReader.END_OF_STREAM);
}
public void testSkipUntil() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertEquals(0, br.skipUntil(';'));
br = getEBR("ABCDEF,GHL,,MN");
assertEquals(6, br.skipUntil(','));
assertEquals(0, br.skipUntil(','));
br.skip(1);
assertEquals(3, br.skipUntil(','));
br.skip(1);
assertEquals(0, br.skipUntil(','));
br.skip(1);
assertEquals(2, br.skipUntil(','));
}
public void testReadUntil() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readUntil(';').equals(""));
br = getEBR("ABCDEF;GHL;;MN");
assertTrue(br.readUntil(';').equals("ABCDEF"));
assertTrue(br.readUntil(';').length() == 0);
br.skip(1);
assertTrue(br.readUntil(';').equals("GHL"));
br.skip(1);
assertTrue(br.readUntil(';').equals(""));
br.skip(1);
assertTrue(br.readUntil(',').equals("MN"));
}
private ExtendedBufferedReader getEBR(String s) {
return new ExtendedBufferedReader(new StringReader(s));
}