commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1183484 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/StringUtils.java site/changes/changes.xml test/java/org/apache/commons/lang3/StringUtilsTest.java
Date Fri, 14 Oct 2011 20:12:50 GMT
Author: ggregory
Date: Fri Oct 14 20:12:49 2011
New Revision: 1183484

URL: http://svn.apache.org/viewvc?rev=1183484&view=rev
Log: (empty)

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/site/changes/changes.xml
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java

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=1183484&r1=1183483&r2=1183484&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 Fri
Oct 14 20:12:49 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.lang3;
 
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -6560,4 +6561,22 @@ public class StringUtils {
         return false;
     }
 
+    /**
+     * 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.1
+     */
+    public static String toString(byte[] bytes, String charsetName) throws UnsupportedEncodingException
{
+        return charsetName == null ? new String(bytes) : new String(bytes, charsetName);
+    }
+
 }

Modified: commons/proper/lang/trunk/src/site/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/site/changes/changes.xml?rev=1183484&r1=1183483&r2=1183484&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/site/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/site/changes/changes.xml Fri Oct 14 20:12:49 2011
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.0.2" date="unreleased" description="September release">     

+    <action type="add" issue="LANG-760">Add API StringUtils.toString(byte[] intput,
String charsetName)</action>
     <action type="add" issue="LANG-756">Add APIs ClassUtils.isPrimitiveWrapper(Class&lt;?&gt;)
and isPrimitiveOrWrapper(Class&lt;?&gt;)</action>
     <action type="update" issue="LANG-752">Fix createLong() so it behaves like createInteger()</action>
     <action type="update" issue="LANG-751">Include the actual type in the Validate.isInstance
and isAssignableFrom exception messages</action>

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=1183484&r1=1183483&r2=1183484&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
Fri Oct 14 20:12:49 2011
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertSam
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -2092,4 +2093,25 @@ public class StringUtilsTest {
             }
         }
     }
+
+    /**
+     * Tests {@link StringUtils#toString(byte[], String)}
+     * 
+     * @throws UnsupportedEncodingException
+     * @see StringUtils#toString(byte[], String)
+     */
+    @Test
+    public void testToString() 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.toString(expectedBytes, null));
+        assertEquals(expectedString, StringUtils.toString(expectedBytes, encoding));
+        encoding = "UTF-16";
+        expectedBytes = expectedString.getBytes(encoding);
+        assertEquals(expectedString, StringUtils.toString(expectedBytes, encoding));
+    }
 }



Mime
View raw message