Merge pull request #1015 from yasirmhd/master

Memory optimisation in JsonReader
This commit is contained in:
Jesse Wilson 2017-02-16 22:58:16 -05:00 committed by GitHub
commit 2d072bae3a

View File

@ -985,7 +985,7 @@ public class JsonReader implements Closeable {
private String nextQuotedValue(char quote) throws IOException {
// Like nextNonWhitespace, this uses locals 'p' and 'l' to save inner-loop field access.
char[] buffer = this.buffer;
StringBuilder builder = new StringBuilder();
StringBuilder builder = null;
while (true) {
int p = pos;
int l = limit;
@ -996,12 +996,23 @@ public class JsonReader implements Closeable {
if (c == quote) {
pos = p;
builder.append(buffer, start, p - start - 1);
int len = p - start - 1;
if (builder == null) {
return new String(buffer, start, len);
} else {
builder.append(buffer, start, len);
return builder.toString();
}
} else if (c == '\\') {
pos = p;
builder.append(buffer, start, p - start - 1);
builder.append(readEscapeCharacter());
int len = p - start - 1;
char escapeChar = readEscapeCharacter();
if (builder == null) {
int estimatedLength = (len + pos - p) * 2;
builder = new StringBuilder(Math.max(estimatedLength, 16));
}
builder.append(buffer, start, len);
builder.append(escapeChar);
p = pos;
l = limit;
start = p;
@ -1011,6 +1022,10 @@ public class JsonReader implements Closeable {
}
}
if (builder == null) {
int estimatedLength = (p - start) * 2;
builder = new StringBuilder(Math.max(estimatedLength, 16));
}
builder.append(buffer, start, p - start);
pos = p;
if (!fillBuffer(1)) {