Skip to content

Commit 0ee3f59

Browse files
committed
8358533: Use a char[] instead of StringBuilder; expand regression test
1 parent d616c16 commit 0ee3f59

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

src/java.base/share/classes/java/io/Reader.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,49 +450,70 @@ public int read(char[] cbuf) throws IOException {
450450
*/
451451
public List<String> readAllLines() throws IOException {
452452
List<String> lines = new ArrayList<>();
453-
StringBuilder sb = new StringBuilder(0);
453+
454+
int writePos = 0;
455+
int fragPos = -1;
456+
int fragLen = 0;
454457

455458
char[] cb = new char[TRANSFER_BUFFER_SIZE];
456459
boolean skipLF = false;
457460
int n;
458-
while ((n = read(cb, 0, cb.length)) != -1) {
459-
int pos = 0;
460-
while (pos < n) {
461-
// find next line terminator; if none found, "term" equals "n"
461+
while ((n = read(cb, writePos, cb.length - writePos)) != -1) {
462+
int pos = writePos;
463+
int limit = pos + n;
464+
while (pos < limit) {
465+
// find next line terminator
462466
int term = pos;
463-
while (term < n) {
467+
while (term < limit) {
464468
char c = cb[term];
465469
if (c == '\n' || c == '\r')
466470
break;
467471
term++;
468472
}
469473

470-
if (term < n) { // line terminator
474+
if (term < limit) { // line terminator
471475
boolean isCR = (cb[term] == '\r');
472476
if (isCR || !(skipLF && term == pos)) {
473477
// line terminator is a CR or an LF just after a CR
474-
if (sb.isEmpty()) {
475-
// avoid the StringBuilder if possible
476-
lines.add(new String(cb, pos, term - pos));
478+
if (fragPos != -1) {
479+
lines.add(new String(cb, fragPos, term - fragPos));
480+
fragPos = -1;
477481
} else {
478-
sb.append(cb, pos, term - pos);
479-
lines.add(sb.toString());
480-
sb.setLength(0);
482+
lines.add(new String(cb, pos, term - pos));
481483
}
482484
}
483485
pos = term + 1;
486+
if (pos == limit)
487+
writePos = 0;
484488
skipLF = isCR;
485489
} else { // no line terminator
486-
sb.append(cb, pos, n - pos);
487-
pos = n;
490+
int len = term - pos;
491+
if (fragPos == -1) {
492+
fragPos = pos;
493+
fragLen = len;
494+
} else {
495+
fragLen += len;
496+
}
497+
if (fragLen >= cb.length/2) {
498+
// allocate larger buffer and copy chars to beginning
499+
char[] tmp = new char[2*cb.length];
500+
System.arraycopy(cb, fragPos, tmp, 0, fragLen);
501+
cb = tmp;
502+
} else if (fragPos != 0) {
503+
// move fragment to beginning of buffer
504+
System.arraycopy(cb, fragPos, cb, 0, fragLen);
505+
}
506+
writePos = fragLen;
507+
fragPos = 0;
508+
pos = limit;
488509
skipLF = false;
489510
}
490511
}
491512
}
492513

493514
// add a string if EOS terminates the last line
494-
if (!sb.isEmpty())
495-
lines.add(sb.toString());
515+
if (fragPos != -1)
516+
lines.add(new String(cb, fragPos, fragLen));
496517

497518
return Collections.unmodifiableList(lines);
498519
}

test/jdk/java/io/Reader/ReadAll.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public static void setup() throws IOException {
9494
strings.add(sb.toString());
9595
sb.setLength(0);
9696
}
97+
98+
strings.add(PHRASE.repeat((4096 + plen - 1)/plen) + "\n" +
99+
" ".repeat(8192) + PHRASE);
100+
strings.add("x".repeat(8191) + "\n");
101+
strings.add("x".repeat(9000) + "\n" + "y".repeat(100));
102+
strings.add("x".repeat(8200) + "y".repeat(8200) + "x".repeat(8200) +
103+
PHRASE + "\n");
104+
97105
Files.write(path, strings);
98106
System.out.println(strings.size() + " lines written");
99107
}

test/micro/org/openjdk/bench/java/io/ReaderReadAllLines.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void setup() throws IOException {
4545
Random rnd = new Random(System.nanoTime());
4646
int off = 0;
4747
while (off < len) {
48-
int lineLen = 40 + rnd.nextInt(40);
48+
int lineLen = 40 + rnd.nextInt(8192);
4949
if (lineLen > len - off) {
5050
off = len;
5151
} else {

0 commit comments

Comments
 (0)