You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/03/22 17:11:43 UTC

logging-log4j2 git commit: LOG4J2-1291 provide fallback to legacy String.getBytes() in case encodeText() throws an Exception

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 9ada17cf1 -> ff3c78b95


LOG4J2-1291 provide fallback to legacy String.getBytes() in case encodeText() throws an Exception


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ff3c78b9
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ff3c78b9
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ff3c78b9

Branch: refs/heads/master
Commit: ff3c78b95315c58095851b75a5e12ec3b85ec242
Parents: 9ada17c
Author: rpopma <rp...@apache.org>
Authored: Wed Mar 23 01:11:42 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Wed Mar 23 01:11:42 2016 +0900

----------------------------------------------------------------------
 .../log4j/core/layout/TextEncoderHelper.java    | 30 ++++++++++++++++++++
 1 file changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ff3c78b9/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/TextEncoderHelper.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/TextEncoderHelper.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/TextEncoderHelper.java
index 0da19b5..698072d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/TextEncoderHelper.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/TextEncoderHelper.java
@@ -25,6 +25,8 @@ import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
 import java.util.Objects;
 
+import org.apache.logging.log4j.status.StatusLogger;
+
 /**
  * Helper class to encode text to binary data without allocating temporary objects.
  *
@@ -49,6 +51,20 @@ public class TextEncoderHelper {
     }
 
     public void encodeText(final StringBuilder text, final ByteBufferDestination destination) {
+        try {
+            encodeText0(text, destination);
+        } catch (final Exception ex) {
+            logEncodeTextException(ex, text, destination);
+            encodeTextFallBack(text, destination);
+        }
+    }
+
+    private void logEncodeTextException(final Exception ex, final StringBuilder text,
+            final ByteBufferDestination destination) {
+        StatusLogger.getLogger().error("Recovering from TextEncoderHelper.encodeText('{}') error", text, ex);
+    }
+
+    private void encodeText0(final StringBuilder text, final ByteBufferDestination destination) {
         charsetEncoder.reset();
         ByteBuffer byteBuf = destination.getByteBuffer();
         final CharBuffer charBuf = getCachedCharBuffer();
@@ -67,6 +83,20 @@ public class TextEncoderHelper {
         } while (!endOfInput);
     }
 
+    private void encodeTextFallBack(final StringBuilder text, final ByteBufferDestination destination) {
+        final byte[] bytes = text.toString().getBytes(charset);
+        ByteBuffer buffer = destination.getByteBuffer();
+        int offset = 0;
+        do {
+            final int length = Math.min(bytes.length - offset, buffer.remaining());
+            buffer.put(bytes, offset, length);
+            offset += length;
+            if (offset < bytes.length) {
+                buffer = destination.drain(buffer);
+            }
+        } while (offset < bytes.length);
+    }
+
     public void encodeText(final CharBuffer charBuf, final ByteBufferDestination destination) {
         charsetEncoder.reset();
         final ByteBuffer byteBuf = destination.getByteBuffer();