Allocate a string look ahead buffer in

CSVFormat.printWithEscapes(CharSequence, Appendable)

As opposed to one for each character read.
This commit is contained in:
Gary Gregory 2024-03-11 16:04:00 -04:00
parent 7e43e94b4c
commit 65d6d1101c
2 changed files with 6 additions and 17 deletions

View File

@ -2191,15 +2191,17 @@ public final class CSVFormat implements Serializable {
int pos = 0; int pos = 0;
@SuppressWarnings("resource") // Temp reader on input reader. @SuppressWarnings("resource") // Temp reader on input reader.
final ExtendedBufferedReader bufferedReader = new ExtendedBufferedReader(reader); final ExtendedBufferedReader bufferedReader = new ExtendedBufferedReader(reader);
final char[] delim = getDelimiterCharArray(); final char[] delimArray = getDelimiterCharArray();
final int delimLength = delim.length; final int delimLength = delimArray.length;
final char escape = getEscapeChar(); final char escape = getEscapeChar();
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE); final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
int c; int c;
final char[] lookAheadBuffer = new char[delimLength - 1];
while (EOF != (c = bufferedReader.read())) { while (EOF != (c = bufferedReader.read())) {
builder.append((char) c); builder.append((char) c);
final boolean isDelimiterStart = isDelimiter((char) c, builder.toString() + new String(bufferedReader.lookAhead(delimLength - 1)), pos, delim, Arrays.fill(lookAheadBuffer, (char) 0);
delimLength); final String test = builder.toString() + new String(bufferedReader.lookAhead(lookAheadBuffer));
final boolean isDelimiterStart = isDelimiter((char) c, test, pos, delimArray, delimLength);
final boolean isCr = c == CR; final boolean isCr = c == CR;
final boolean isLf = c == LF; final boolean isLf = c == LF;
if (isCr || isLf || c == escape || isDelimiterStart) { if (isCr || isLf || c == escape || isDelimiterStart) {

View File

@ -139,19 +139,6 @@ final class ExtendedBufferedReader extends BufferedReader {
return buf; return buf;
} }
/**
* 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
* doesn't affect line number or last character.
*
* @param n the number characters look ahead.
* @return the next n characters.
* @throws IOException If an I/O error occurs
*/
char[] lookAhead(final int n) throws IOException {
final char[] buf = new char[n];
return lookAhead(buf);
}
@Override @Override
public int read() throws IOException { public int read() throws IOException {
final int current = super.read(); final int current = super.read();