Skip to content

Commit baa697f

Browse files
committed
Extract the chunked test reader into a shared ChunkedReader based on StringReader
1 parent 8978489 commit baa697f

3 files changed

Lines changed: 44 additions & 64 deletions

File tree

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

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,36 +1711,8 @@ void testParseWithDelimiterStringWithEscape() throws IOException {
17111711

17121712
@Test
17131713
void testParseWithDelimiterStringFromChunkedReader() throws IOException {
1714-
// A reader that hands out one character at a time and never reports itself ready, like a socket or pipe.
1715-
final Reader chunked = new Reader() {
1716-
1717-
private final String content = "a[|]b\r\nc![!|!]d[|]e";
1718-
private int index;
1719-
1720-
@Override
1721-
public void close() {
1722-
// nothing to close
1723-
}
1724-
1725-
@Override
1726-
public boolean ready() {
1727-
return false;
1728-
}
1729-
1730-
@Override
1731-
public int read(final char[] buf, final int offset, final int length) {
1732-
if (index >= content.length()) {
1733-
return -1;
1734-
}
1735-
if (length <= 0) {
1736-
return 0;
1737-
}
1738-
buf[offset] = content.charAt(index++);
1739-
return 1;
1740-
}
1741-
};
17421714
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
1743-
try (CSVParser csvParser = csvFormat.parse(chunked)) {
1715+
try (CSVParser csvParser = csvFormat.parse(new ChunkedReader("a[|]b\r\nc![!|!]d[|]e"))) {
17441716
CSVRecord csvRecord = csvParser.nextRecord();
17451717
assertEquals("a", csvRecord.get(0));
17461718
assertEquals("b", csvRecord.get(1));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.commons.csv;
21+
22+
import java.io.IOException;
23+
import java.io.StringReader;
24+
25+
/**
26+
* A {@link StringReader} that hands out one character per call and never reports itself ready, like a socket or a pipe that delivers data in chunks.
27+
*/
28+
final class ChunkedReader extends StringReader {
29+
30+
ChunkedReader(final String content) {
31+
super(content);
32+
}
33+
34+
@Override
35+
public int read(final char[] buf, final int offset, final int length) throws IOException {
36+
return length <= 0 ? 0 : super.read(buf, offset, 1);
37+
}
38+
39+
@Override
40+
public boolean ready() {
41+
return false;
42+
}
43+
}

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -273,39 +273,4 @@ void testPeekArrayPastEndOfChunkedReader() throws Exception {
273273
assertEquals('b', peeked[1]);
274274
}
275275
}
276-
277-
/**
278-
* A reader that returns one character per call and never reports itself ready, like a socket or pipe that delivers data in chunks.
279-
*/
280-
private static final class ChunkedReader extends java.io.Reader {
281-
282-
private final String content;
283-
private int index;
284-
285-
ChunkedReader(final String content) {
286-
this.content = content;
287-
}
288-
289-
@Override
290-
public void close() {
291-
// nothing to close
292-
}
293-
294-
@Override
295-
public boolean ready() {
296-
return false;
297-
}
298-
299-
@Override
300-
public int read(final char[] buf, final int offset, final int length) {
301-
if (index >= content.length()) {
302-
return EOF;
303-
}
304-
if (length <= 0) {
305-
return 0;
306-
}
307-
buf[offset] = content.charAt(index++);
308-
return 1;
309-
}
310-
}
311276
}

0 commit comments

Comments
 (0)