diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriterTest.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriterTest.java index 24e9a072c1d..0ae57d59f64 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriterTest.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriterTest.java @@ -38,12 +38,20 @@ void test_okay_payloads() { writer.write(new char[] {Character.MIN_VALUE, Character.MAX_VALUE}); writer.write("foo"); writer.write("foobar", 3, 3); + writer.write("empty", 3, 0); writer.write(new char[] {'f', 'o', 'o', 'b', 'a', 'r', 'b', 'u', 'z', 'z'}, 6, 4); + writer.write(new char[] {'a', 'b', 'c'}, 0, 0); + writer.write(new char[] {}, 0, 0); + writer.write(new char[] {}); + writer.write("", 0, 0); + writer.write(""); writer.append('!'); writer.append("yo"); writer.append(null); writer.append("yo dog", 3, 6); writer.append(null, -1, -1); + writer.append("", -1, -1); + writer.append(""); // Verify accessors. final char[] expectedBuffer = new char[capacity]; diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriter.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriter.java index 44cf52d0e65..468c3df2dc8 100644 --- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriter.java +++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/TruncatingBufferedWriter.java @@ -77,6 +77,11 @@ public void write(final char[] source, final int offset, final int length) { // Check arguments. Objects.requireNonNull(source, "source"); + + if (source.length == 0) { + return; + } + if (offset < 0 || offset >= source.length) { throw new IndexOutOfBoundsException("invalid offset: " + offset); } @@ -126,6 +131,11 @@ public void write(final String string, final int offset, final int length) { // Check arguments. Objects.requireNonNull(string, "string"); + + if (string.isEmpty()) { + return; + } + if (offset < 0 || offset >= string.length()) { throw new IndexOutOfBoundsException("invalid offset: " + offset); } @@ -168,6 +178,11 @@ public Writer append(final CharSequence seq, final int start, final int end) { return this; } + // Short-circuit on empty sequence + if (seq.length() == 0) { + return this; + } + // Check arguments. if (start < 0 || start >= seq.length()) { throw new IndexOutOfBoundsException("invalid start: " + start); diff --git a/src/changelog/.2.x.x/fix-TruncatingBufferedWriter-empty-input-handling.xml b/src/changelog/.2.x.x/fix-TruncatingBufferedWriter-empty-input-handling.xml new file mode 100644 index 00000000000..eaeae53f399 --- /dev/null +++ b/src/changelog/.2.x.x/fix-TruncatingBufferedWriter-empty-input-handling.xml @@ -0,0 +1,8 @@ + + + + Fix empty string handling for `TruncatingBufferedWriter` +