Skip to content

Commit 65d6d11

Browse files
committed
Allocate a string look ahead buffer in
CSVFormat.printWithEscapes(CharSequence, Appendable) As opposed to one for each character read.
1 parent 7e43e94 commit 65d6d11

2 files changed

Lines changed: 6 additions & 17 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,15 +2191,17 @@ private void printWithEscapes(final Reader reader, final Appendable appendable)
21912191
int pos = 0;
21922192
@SuppressWarnings("resource") // Temp reader on input reader.
21932193
final ExtendedBufferedReader bufferedReader = new ExtendedBufferedReader(reader);
2194-
final char[] delim = getDelimiterCharArray();
2195-
final int delimLength = delim.length;
2194+
final char[] delimArray = getDelimiterCharArray();
2195+
final int delimLength = delimArray.length;
21962196
final char escape = getEscapeChar();
21972197
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
21982198
int c;
2199+
final char[] lookAheadBuffer = new char[delimLength - 1];
21992200
while (EOF != (c = bufferedReader.read())) {
22002201
builder.append((char) c);
2201-
final boolean isDelimiterStart = isDelimiter((char) c, builder.toString() + new String(bufferedReader.lookAhead(delimLength - 1)), pos, delim,
2202-
delimLength);
2202+
Arrays.fill(lookAheadBuffer, (char) 0);
2203+
final String test = builder.toString() + new String(bufferedReader.lookAhead(lookAheadBuffer));
2204+
final boolean isDelimiterStart = isDelimiter((char) c, test, pos, delimArray, delimLength);
22032205
final boolean isCr = c == CR;
22042206
final boolean isLf = c == LF;
22052207
if (isCr || isLf || c == escape || isDelimiterStart) {

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,6 @@ char[] lookAhead(final char[] buf) throws IOException {
139139
return buf;
140140
}
141141

142-
/**
143-
* Returns the next n characters in the current reader without consuming them. The next call to {@link #read()} will still return the next value. This
144-
* doesn't affect line number or last character.
145-
*
146-
* @param n the number characters look ahead.
147-
* @return the next n characters.
148-
* @throws IOException If an I/O error occurs
149-
*/
150-
char[] lookAhead(final int n) throws IOException {
151-
final char[] buf = new char[n];
152-
return lookAhead(buf);
153-
}
154-
155142
@Override
156143
public int read() throws IOException {
157144
final int current = super.read();

0 commit comments

Comments
 (0)