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
This commit is contained in:
Henri Yandell 2013-10-17 07:30:57 +00:00
parent e01f2a4a19
commit cc6a92b1a5
3 changed files with 43 additions and 0 deletions

View File

@ -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>

View File

@ -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);
}
}

View File

@ -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)));
}
}