Source:
<code>public static List<BatchItem> from(final String events) { final List<BatchItem> batch = new ArrayList<>(); <START> StringBuilder sb = new StringBuilder(); <END> int brackets = 0; boolean insideQuote = false; boolean escaped = false; if ((!events.startsWith("[")) || (!events.endsWith("]"))) { throw new JSONException("Array must be surrounded with square brackets"); } for (int i = 1; i < events.length() - 1; i++) { if (!escaped && events.charAt(i) == '"') { if (insideQuote) insideQuote = false; else insideQuote = true; } if (escaped) { sb.append(events.charAt(i)); escaped = false; } else if (!escaped && events.charAt(i) == '\\') { sb.append(events.charAt(i)); escaped = true; } else if (insideQuote) { sb.append(events.charAt(i)); } else { if (events.charAt(i) == '{') { brackets++; } if (events.charAt(i) == '}') { brackets--; } if (!((brackets == 0) && (events.charAt(i) == ','))) { sb.append(events.charAt(i)); } if (brackets == 0 && (events.charAt(i) != ' ') && (events.charAt(i) != '\t' && (events.charAt(i) != '\n') && (events.charAt(i) != '\r'))) { if (sb.length() > 0) { batch.add(new BatchItem(sb.toString())); } sb = new StringBuilder(); } } } if (sb.length() != 0) { batch.add(new BatchItem(sb.toString())); } return batch; }</code><technical_language>I wondering if a build this substring zero copy. I find, Java 7 String.substring creates a copy of original char array. I valuable JSONTokener try figure out if a avoid extra copies of this string. This is executed code path in project. I expect this change impact garbage collection performance</technical_language>

Target:
public static List<BatchItem> from(final String events) { final List<BatchItem> batch = new ArrayList<>(); StringBuilder sb = new StringBuilder(); int brackets = 0; boolean insideQuote = false; boolean escaped = false; int start = 0; final int length = events.length(); int end = length - 1; while (isEmptyCharacter(events.charAt(start)) && start < end) { start++; } while (isEmptyCharacter(events.charAt(end)) && end > start) { end--; } if (!(events.charAt(start) == '[')) { throw new JSONException(String.format("Unexpected character %s in position %d, expected '['", events.charAt(start), start)); } start++; if (!(events.charAt(end) == ']')) { throw new JSONException(String.format("Unexpected character %s in position %d, expected ']'", events.charAt(end), end)); } for (int i = start; i < end; i++) { if (!escaped && events.charAt(i) == '"') { if (insideQuote) { insideQuote = false; } else { insideQuote = true; } } if (escaped) { sb.append(events.charAt(i)); escaped = false; } else if (!escaped && events.charAt(i) == '\\') { sb.append(events.charAt(i)); escaped = true; } else if (insideQuote) { sb.append(events.charAt(i)); } else { if (events.charAt(i) == '{') { brackets++; } if (events.charAt(i) == '}') { brackets--; } if (!((brackets == 0) && ((events.charAt(i) == ',') || isEmptyCharacter(events.charAt(i))))) { sb.append(events.charAt(i)); } if (brackets == 0 && !isEmptyCharacter(events.charAt(i))) { if (sb.length() > 0) { batch.add(new BatchItem(sb.toString())); } sb = new StringBuilder(); } } } if (sb.length() != 0) { batch.add(new BatchItem(sb.toString())); } return batch; }