Skip to content

Commit 3cb5801

Browse files
committed
Replaced CharBuffer with StringBuilder (CSV-59)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1300659 13f79535-47bb-0310-9956-ffa450edef68
1 parent f8294d2 commit 3cb5801

3 files changed

Lines changed: 10 additions & 351 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class CSVLexer {
235235
/** length of the initial token (content-)buffer */
236236
private static final int INITIAL_TOKEN_LENGTH = 50;
237237

238-
private final CharBuffer wsBuf = new CharBuffer();
238+
private final StringBuilder wsBuf = new StringBuilder();
239239

240240
private final CSVFormat format;
241241

@@ -267,13 +267,13 @@ enum Type {
267267
Type type = INVALID;
268268

269269
/** The content buffer. */
270-
CharBuffer content = new CharBuffer(INITIAL_TOKEN_LENGTH);
270+
StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
271271

272272
/** Token ready flag: indicates a valid token with content (ready for the parser). */
273273
boolean isReady;
274274

275275
Token reset() {
276-
content.clear();
276+
content.setLength(0);
277277
type = INVALID;
278278
isReady = false;
279279
return this;
@@ -299,7 +299,7 @@ public int getLineNumber() {
299299
* @throws IOException on stream access error
300300
*/
301301
Token nextToken(Token tkn) throws IOException {
302-
wsBuf.clear(); // reuse
302+
wsBuf.setLength(0); // reuse
303303

304304
// get the last read char (required for empty line detection)
305305
int lastChar = in.readAgain();
@@ -308,7 +308,6 @@ Token nextToken(Token tkn) throws IOException {
308308
/* note: unfortunately isEndOfLine may consumes a character silently.
309309
* this has no effect outside of the method. so a simple workaround
310310
* is to call 'readAgain' on the stream...
311-
* uh: might using objects instead of base-types (jdk1.5 autoboxing!)
312311
*/
313312
int c = in.read();
314313
boolean eol = isEndOfLine(c);
@@ -427,12 +426,17 @@ private Token simpleTokenLexer(Token tkn, int c) throws IOException {
427426
}
428427

429428
if (format.isTrailingSpacesIgnored()) {
430-
tkn.content.trimTrailingWhitespace();
429+
trimTrailingSpaces(tkn.content);
431430
}
432431

433432
return tkn;
434433
}
435434

435+
private void trimTrailingSpaces(StringBuilder buffer) {
436+
while (buffer.length() > 0 && Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
437+
buffer.setLength(buffer.length() - 1);
438+
}
439+
}
436440

437441
/**
438442
* An encapsulated token lexer

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

Lines changed: 0 additions & 166 deletions
This file was deleted.

0 commit comments

Comments
 (0)