You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2016/04/14 10:37:39 UTC
[2/6] logging-log4j2 git commit: LOG4J2-1274 TextEncoderHelper cleanup
LOG4J2-1274 TextEncoderHelper cleanup
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/0a06f8f0
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/0a06f8f0
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/0a06f8f0
Branch: refs/heads/LOG4J2-1365
Commit: 0a06f8f0dea40cce08d987da3cc6e340835060d2
Parents: 23cd33f
Author: rpopma <rp...@apache.org>
Authored: Thu Apr 14 01:23:18 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Apr 14 01:23:18 2016 +0900
----------------------------------------------------------------------
.../log4j/core/layout/TextEncoderHelper.java | 55 ++++++++------------
1 file changed, 21 insertions(+), 34 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0a06f8f0/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 136e2e6..09a49d8 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
@@ -22,9 +22,6 @@ import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
-import java.util.Objects;
-
-import org.apache.logging.log4j.status.StatusLogger;
/**
* Helper class to encode text to binary data without allocating temporary objects.
@@ -34,27 +31,34 @@ import org.apache.logging.log4j.status.StatusLogger;
public class TextEncoderHelper {
static final int DEFAULT_CHAR_BUFFER_SIZE = 2048;
- private final Charset charset;
-// private final CharBuffer cachedCharBuffer;
-// private final CharsetEncoder charsetEncoder;
-
- public TextEncoderHelper(final Charset charset) {
- this(charset, DEFAULT_CHAR_BUFFER_SIZE);
+ private TextEncoderHelper() {
}
- public TextEncoderHelper(final Charset charset, final int bufferSize) {
- this.charset = Objects.requireNonNull(charset, "charset");
- }
-
- private void logEncodeTextException(final Exception ex, final StringBuilder text,
- final ByteBufferDestination destination) {
- StatusLogger.getLogger().error("Recovering from TextEncoderHelper.encodeText('{}') error", text, ex);
+ static void encodeTextFallBack(final Charset charset, final StringBuilder text,
+ final ByteBufferDestination destination) {
+ final byte[] bytes = text.toString().getBytes(charset);
+ synchronized (destination) {
+ 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);
+ }
}
static void encodeTextWithCopy(final CharsetEncoder charsetEncoder, final CharBuffer charBuf, final ByteBuffer temp,
final StringBuilder text, final ByteBufferDestination destination) {
+
encodeText(charsetEncoder, charBuf, temp, text, destination);
+ copyDataToDestination(temp, destination);
+ }
+ private static void copyDataToDestination(final ByteBuffer temp, final ByteBufferDestination destination) {
synchronized (destination) {
ByteBuffer destinationBuffer = destination.getByteBuffer();
if (destinationBuffer != temp) { // still need to write to the destination
@@ -87,28 +91,11 @@ public class TextEncoderHelper {
} while (!endOfInput);
}
- static void encodeTextFallBack(final Charset charset, final StringBuilder text,
- final ByteBufferDestination destination) {
- final byte[] bytes = text.toString().getBytes(charset);
- synchronized (destination) {
- 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);
- }
- }
-
/**
* For testing purposes only.
*/
@Deprecated
- public void encodeText(final CharsetEncoder charsetEncoder, final CharBuffer charBuf,
+ public static void encodeText(final CharsetEncoder charsetEncoder, final CharBuffer charBuf,
final ByteBufferDestination destination) {
synchronized (destination) {
charsetEncoder.reset();