This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch scheduled-for-deletion/LOG4J2-930
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit b66c93ab6d43ebd315b1655ccb319be2519edbbe
Author: rpopma <[email protected]>
AuthorDate: Mon Jan 12 17:25:37 2015 +0900

    added helper method #getBytes
---
 .../apache/logging/log4j/core/util/Charsets.java   | 32 ++++++++++++++++------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java
index 2ad66b36af..07f1087423 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java
@@ -16,13 +16,14 @@
  */
 package org.apache.logging.log4j.core.util;
 
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
- * Charset utilities. Contains the standard character sets guaranteed to be 
available on all implementations of the
- * Java platform. Parts adapted from JDK 1.7 (in particular, the {@code 
java.nio.charset.StandardCharsets} class).
+ * Charset utilities. Contains the standard character sets guaranteed to be 
available on all implementations of the Java
+ * platform. Parts adapted from JDK 1.7 (in particular, the {@code 
java.nio.charset.StandardCharsets} class).
  *
  * @see java.nio.charset.Charset
  */
@@ -62,8 +63,7 @@ public final class Charsets {
      * Returns a Charset, if possible the Charset for the specified {@code 
charsetName}, otherwise (if the specified
      * {@code charsetName} is {@code null} or not supported) this method 
returns the platform default Charset.
      *
-     * @param charsetName
-     *            name of the preferred charset or {@code null}
+     * @param charsetName name of the preferred charset or {@code null}
      * @return a Charset, not null.
      */
     public static Charset getSupportedCharset(final String charsetName) {
@@ -74,10 +74,8 @@ public final class Charsets {
      * Returns a Charset, if possible the Charset for the specified {@code 
charsetName}, otherwise (if the specified
      * {@code charsetName} is {@code null} or not supported) this method 
returns the platform default Charset.
      *
-     * @param charsetName
-     *            name of the preferred charset or {@code null}
-     * @param defaultCharset
-     *            returned if {@code charsetName} is null or is not supported.
+     * @param charsetName name of the preferred charset or {@code null}
+     * @param defaultCharset returned if {@code charsetName} is null or is not 
supported.
      * @return a Charset, never null.
      */
     public static Charset getSupportedCharset(final String charsetName, final 
Charset defaultCharset) {
@@ -95,6 +93,24 @@ public final class Charsets {
         return charset;
     }
 
+    /**
+     * Returns a sequence of bytes that encodes the specified String, using 
the named Charset.
+     * 
+     * @param txt the string to encode
+     * @param charset the {@code Charset} to use
+     * @return the encoded String
+     */
+    public static byte[] getBytes(final String txt, final Charset charset) {
+        try {
+            // PERFORMANCE-SENSITIVE CODE (called for every log event by most 
layouts).
+            // String.getBytes(Charset) creates a new StringEncoder instance
+            // every call and is slightly slower than String.getBytes(String)
+            return txt.getBytes(charset.name());
+        } catch (UnsupportedEncodingException e) {
+            return txt.getBytes(charset);
+        }
+    }
+
     private Charsets() {
     }
 

Reply via email to