Author: bayard Date: Thu Oct 17 07:30:57 2013 New Revision: 1532995 URL: http://svn.apache.org/r1532995 Log: Deprecating StringUtils.toString(byte[],String) in favour of a new CharSet based method, as reported by Aaron Digulla in LANG-795
Modified: commons/proper/lang/trunk/src/changes/changes.xml commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java Modified: commons/proper/lang/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1532995&r1=1532994&r2=1532995&view=diff ============================================================================== --- commons/proper/lang/trunk/src/changes/changes.xml (original) +++ commons/proper/lang/trunk/src/changes/changes.xml Thu Oct 17 07:30:57 2013 @@ -22,6 +22,7 @@ <body> <release version="3.2" date="TBA" description="Next release"> + <action issue="LANG-795" type="add" due-to="Aaron Digulla">StringUtils.toString(byte[], String) deprecated in favour of a new StringUtils.toString(byte[], CharSet)</action> <action issue="LANG-902" type="fix" due-to="Andrzej Winnicki">RandomStringUtils.random javadoc was incorrectly promising letters and numbers would, as opposed to may, appear</action> <action issue="LANG-921" type="fix" dev="britter">BooleanUtils.xor(boolean...) produces wrong results</action> <action issue="LANG-910" type="update" due-to="Timur Yarosh">StringUtils.normalizeSpace now handles non-breaking spaces (Unicode 00A0)</action> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1532995&r1=1532994&r2=1532995&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Thu Oct 17 07:30:57 2013 @@ -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 <code>byte[]</code> 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); + } + } Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1532995&r1=1532994&r2=1532995&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java Thu Oct 17 07:30:57 2013 @@ -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))); + } }