Size allocation of StringBuilder (#1047)

Size allocation of StringBuilder
This commit is contained in:
sourabh gupta 2017-05-25 04:19:09 +05:30 committed by inder123
parent f7012e5865
commit 5848096f3e

View File

@ -1077,7 +1077,7 @@ public class JsonReader implements Closeable {
// use a StringBuilder when the value is too long. This is too long to be a number! // use a StringBuilder when the value is too long. This is too long to be a number!
if (builder == null) { if (builder == null) {
builder = new StringBuilder(); builder = new StringBuilder(Math.max(i,16));
} }
builder.append(buffer, pos, i); builder.append(buffer, pos, i);
pos += i; pos += i;
@ -1086,14 +1086,8 @@ public class JsonReader implements Closeable {
break; break;
} }
} }
String result; String result = (null == builder) ? new String(buffer, pos, i) : builder.append(buffer, pos, i).toString();
if (builder == null) {
result = new String(buffer, pos, i);
} else {
builder.append(buffer, pos, i);
result = builder.toString();
}
pos += i; pos += i;
return result; return result;
} }
@ -1438,14 +1432,15 @@ public class JsonReader implements Closeable {
* @param toFind a string to search for. Must not contain a newline. * @param toFind a string to search for. Must not contain a newline.
*/ */
private boolean skipTo(String toFind) throws IOException { private boolean skipTo(String toFind) throws IOException {
int length = toFind.length();
outer: outer:
for (; pos + toFind.length() <= limit || fillBuffer(toFind.length()); pos++) { for (; pos + length <= limit || fillBuffer(length); pos++) {
if (buffer[pos] == '\n') { if (buffer[pos] == '\n') {
lineNumber++; lineNumber++;
lineStart = pos + 1; lineStart = pos + 1;
continue; continue;
} }
for (int c = 0; c < toFind.length(); c++) { for (int c = 0; c < length; c++) {
if (buffer[pos + c] != toFind.charAt(c)) { if (buffer[pos + c] != toFind.charAt(c)) {
continue outer; continue outer;
} }