Skip to content

Commit fe5bd51

Browse files
committed
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
1 parent c6bdeca commit fe5bd51

2 files changed

Lines changed: 7 additions & 142 deletions

File tree

src/java/org/apache/commons/csv/ExtendedBufferedReader.java

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
* In particular the reader supports a look-ahead option,
3030
* which allows you to see the next char returned by
3131
* next().
32-
* Furthermore the skip-method supports skipping until
33-
* (but excluding) a given char. Similar functionality
34-
* is supported by the reader as well.
3532
*
3633
*/
3734
class ExtendedBufferedReader extends BufferedReader {
@@ -150,29 +147,6 @@ public int read(char[] buf, int off, int len) throws IOException {
150147
return cOff - off;
151148
}
152149

153-
/**
154-
* Reads all characters up to (but not including) the given character.
155-
*
156-
* @param c the character to read up to
157-
* @return the string up to the character <code>c</code>
158-
* @throws IOException
159-
*/
160-
public String readUntil(char c) throws IOException {
161-
if (lookaheadChar == UNDEFINED) {
162-
lookaheadChar = super.read();
163-
}
164-
line.clear(); // reuse
165-
while (lookaheadChar != c && lookaheadChar != END_OF_STREAM) {
166-
line.append((char) lookaheadChar);
167-
if (lookaheadChar == '\n') {
168-
lineCounter++;
169-
}
170-
lastChar = lookaheadChar;
171-
lookaheadChar = super.read();
172-
}
173-
return line.toString();
174-
}
175-
176150
/**
177151
* @return A String containing the contents of the line, not
178152
* including any line-termination characters, or null
@@ -217,60 +191,10 @@ public String readLine() throws IOException {
217191
}
218192

219193
/**
220-
* Skips char in the stream
221-
*
222-
* ATTENTION: invalidates the line-counter !!!!!
223-
*
224-
* @return nof skiped chars
194+
* Unsupported
225195
*/
226196
public long skip(long n) throws IllegalArgumentException, IOException {
227-
228-
if (lookaheadChar == UNDEFINED) {
229-
lookaheadChar = super.read();
230-
}
231-
232-
// illegal argument
233-
if (n < 0) {
234-
throw new IllegalArgumentException("negative argument not supported");
235-
}
236-
237-
// no skipping
238-
if (n == 0 || lookaheadChar == END_OF_STREAM) {
239-
return 0;
240-
}
241-
242-
// skip and reread the lookahead-char
243-
long skiped = 0;
244-
if (n > 1) {
245-
skiped = super.skip(n - 1);
246-
}
247-
lookaheadChar = super.read();
248-
// fixme uh: we should check the skiped sequence for line-terminations...
249-
lineCounter = Integer.MIN_VALUE;
250-
return skiped + 1;
251-
}
252-
253-
/**
254-
* Skips all chars in the input until (but excluding) the given char
255-
*
256-
* @param c
257-
* @return
258-
* @throws IllegalArgumentException
259-
* @throws IOException
260-
*/
261-
public long skipUntil(char c) throws IllegalArgumentException, IOException {
262-
if (lookaheadChar == UNDEFINED) {
263-
lookaheadChar = super.read();
264-
}
265-
long counter = 0;
266-
while (lookaheadChar != c && lookaheadChar != END_OF_STREAM) {
267-
if (lookaheadChar == '\n') {
268-
lineCounter++;
269-
}
270-
lookaheadChar = super.read();
271-
counter++;
272-
}
273-
return counter;
197+
throw new UnsupportedOperationException("CSV has no reason to implement this");
274198
}
275199

276200
/**
@@ -291,7 +215,6 @@ public int lookAhead() throws IOException {
291215

292216
/**
293217
* Returns the nof line read
294-
* ATTENTION: the skip-method does invalidate the line-number counter
295218
*
296219
* @return the current-line-number (or -1)
297220
*/
@@ -302,11 +225,12 @@ public int getLineNumber() {
302225
return -1;
303226
}
304227
}
228+
229+
/**
230+
* Unsupported
231+
*/
305232
public boolean markSupported() {
306-
/* note uh: marking is not supported, cause we cannot
307-
* see into the future...
308-
*/
309-
return false;
233+
throw new UnsupportedOperationException("CSV has no reason to implement this");
310234
}
311235

312236
}

src/test/org/apache/commons/csv/ExtendedBufferedReaderTest.java

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ public void testReadLookahead2() throws Exception {
115115

116116
}
117117

118-
public void testMarkSupported() {
119-
assertFalse(getEBR("foo").markSupported());
120-
}
121-
122118
public void testReadLine() throws Exception {
123119
ExtendedBufferedReader br = getEBR("");
124120
assertTrue(br.readLine() == null);
@@ -161,61 +157,6 @@ public void testReadLine() throws Exception {
161157
assertTrue(br.readLine() == null);
162158
}
163159

164-
public void testSkip0() throws Exception {
165-
166-
ExtendedBufferedReader br = getEBR("");
167-
assertEquals(0, br.skip(0));
168-
assertEquals(0, br.skip(1));
169-
170-
br = getEBR("");
171-
assertEquals(0, br.skip(1));
172-
173-
br = getEBR("abcdefg");
174-
assertEquals(0, br.skip(0));
175-
assertEquals('a', br.lookAhead());
176-
assertEquals(0, br.skip(0));
177-
assertEquals('a', br.lookAhead());
178-
assertEquals(1, br.skip(1));
179-
assertEquals('b', br.lookAhead());
180-
assertEquals('b', br.read());
181-
assertEquals(3, br.skip(3));
182-
assertEquals('f', br.lookAhead());
183-
assertEquals(2, br.skip(5));
184-
assertTrue(br.readLine() == null);
185-
186-
br = getEBR("12345");
187-
assertEquals(5, br.skip(5));
188-
assertTrue (br.lookAhead() == ExtendedBufferedReader.END_OF_STREAM);
189-
}
190-
191-
public void testSkipUntil() throws Exception {
192-
ExtendedBufferedReader br = getEBR("");
193-
assertEquals(0, br.skipUntil(';'));
194-
br = getEBR("ABCDEF,GHL,,MN");
195-
assertEquals(6, br.skipUntil(','));
196-
assertEquals(0, br.skipUntil(','));
197-
br.skip(1);
198-
assertEquals(3, br.skipUntil(','));
199-
br.skip(1);
200-
assertEquals(0, br.skipUntil(','));
201-
br.skip(1);
202-
assertEquals(2, br.skipUntil(','));
203-
}
204-
205-
public void testReadUntil() throws Exception {
206-
ExtendedBufferedReader br = getEBR("");
207-
assertTrue(br.readUntil(';').equals(""));
208-
br = getEBR("ABCDEF;GHL;;MN");
209-
assertTrue(br.readUntil(';').equals("ABCDEF"));
210-
assertTrue(br.readUntil(';').length() == 0);
211-
br.skip(1);
212-
assertTrue(br.readUntil(';').equals("GHL"));
213-
br.skip(1);
214-
assertTrue(br.readUntil(';').equals(""));
215-
br.skip(1);
216-
assertTrue(br.readUntil(',').equals("MN"));
217-
}
218-
219160
private ExtendedBufferedReader getEBR(String s) {
220161
return new ExtendedBufferedReader(new StringReader(s));
221162
}

0 commit comments

Comments
 (0)