From 90fb46a131e8f06fe2cb5c7d0cc73f08247758e6 Mon Sep 17 00:00:00 2001 From: fabiojb Date: Sun, 9 May 2021 22:35:36 -0300 Subject: [PATCH] Reuse already allocated chars array instead of substring --- .../de/siegmar/logbackgelf/SimpleJsonEncoder.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/siegmar/logbackgelf/SimpleJsonEncoder.java b/src/main/java/de/siegmar/logbackgelf/SimpleJsonEncoder.java index 6fd5e29..ee7b4f1 100644 --- a/src/main/java/de/siegmar/logbackgelf/SimpleJsonEncoder.java +++ b/src/main/java/de/siegmar/logbackgelf/SimpleJsonEncoder.java @@ -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: @@ -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); } }