Skip to content

Commit

Permalink
Reuse already allocated chars array instead of substring
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiojb committed May 10, 2021
1 parent 53ca94b commit 90fb46a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/java/de/siegmar/logbackgelf/SimpleJsonEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private void appendKey(final String key) throws IOException {
private void escapeString(final String str) throws IOException {
int currentIndex = 0;
int lastCopyIndex = 0;
for (final char ch : str.toCharArray()) {
char[] chars = str.toCharArray();
for (final char ch : chars) {
String escaped = null;
switch (ch) {
case QUOTE:
Expand All @@ -132,19 +133,20 @@ private void escapeString(final String str) throws IOException {
case '\t':
escaped = "\\t";
break;
}
if (ch < ' ') {
escaped = escapeCharacter(ch);
default:
if (ch < ' ') {
escaped = escapeCharacter(ch);
}
}
if (escaped != null) {
writer.append(str.substring(lastCopyIndex, currentIndex));
writer.write(chars, lastCopyIndex, currentIndex - lastCopyIndex);
writer.append(escaped);
lastCopyIndex = currentIndex + 1;
}
currentIndex++;
}
if (lastCopyIndex != currentIndex) {
writer.append(str.substring(lastCopyIndex));
writer.write(chars, lastCopyIndex, chars.length - lastCopyIndex);
}
}

Expand Down

0 comments on commit 90fb46a

Please sign in to comment.