From cc6a92b1a5bb1a9a1d38d4bce67644dbb1b2b349 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 17 Oct 2013 07:30:57 +0000 Subject: [PATCH] Deprecating StringUtils.toString(byte[],String) in favour of a new CharSet based method, as reported by Aaron Digulla in LANG-795 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1532995 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/StringUtils.java | 20 +++++++++++++++++ .../apache/commons/lang3/StringUtilsTest.java | 22 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index eceb7d08f..42acb312e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + StringUtils.toString(byte[], String) deprecated in favour of a new StringUtils.toString(byte[], CharSet) RandomStringUtils.random javadoc was incorrectly promising letters and numbers would, as opposed to may, appear BooleanUtils.xor(boolean...) produces wrong results StringUtils.normalizeSpace now handles non-breaking spaces (Unicode 00A0) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 7f0825d44..183892f9e 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.text.Normalizer; import java.util.ArrayList; import java.util.Arrays; @@ -7347,10 +7348,29 @@ public class StringUtils { * If the named charset is not supported * @throws NullPointerException * if the input is null + * @deprecated use {@link StringUtils#toEncodedString(byte[], Charset)} instead to String constants in your code * @since 3.1 */ public static String toString(final byte[] bytes, final String charsetName) throws UnsupportedEncodingException { return charsetName == null ? new String(bytes) : new String(bytes, charsetName); } + /** + * Converts a byte[] to a String using the specified character encoding. + * + * @param bytes + * the byte array to read from + * @param charsetName + * the encoding to use, if null then use the platform default + * @return a new String + * @throws UnsupportedEncodingException + * If the named charset is not supported + * @throws NullPointerException + * if the input is null + * @since 3.2 + */ + public static String toEncodedString(byte[] bytes, Charset charset) throws UnsupportedEncodingException { + return charset == null ? new String(bytes) : new String(bytes, charset); + } + } diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index ad8a198d7..a120d504e 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -30,6 +30,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.CharBuffer; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; @@ -2329,4 +2330,25 @@ public class StringUtilsTest { assertEquals("prependIfMissingIgnoreCase(XYZabc,xyz,mno)", "XYZabc", StringUtils.prependIfMissingIgnoreCase("XYZabc","xyz","mno")); assertEquals("prependIfMissingIgnoreCase(MNOabc,xyz,mno)", "MNOabc", StringUtils.prependIfMissingIgnoreCase("MNOabc","xyz","mno")); } + + /** + * Tests {@link StringUtils#toString(byte[], Charset)} + * + * @throws UnsupportedEncodingException + * @see StringUtils#toString(byte[], Charset) + */ + @Test + public void testToEncodedString() throws UnsupportedEncodingException { + final String expectedString = "The quick brown fox jumped over the lazy dog."; + String encoding = SystemUtils.FILE_ENCODING; + byte[] expectedBytes = expectedString.getBytes(encoding); + // sanity check start + assertArrayEquals(expectedBytes, expectedString.getBytes()); + // sanity check end + assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.defaultCharset())); + assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding))); + encoding = "UTF-16"; + expectedBytes = expectedString.getBytes(encoding); + assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding))); + } }