directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r327752 - /directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java
Date Sun, 23 Oct 2005 06:18:54 GMT
Author: elecharny
Date: Sat Oct 22 23:18:49 2005
New Revision: 327752

URL: http://svn.apache.org/viewcvs?rev=327752&view=rev
Log:
Added two methods :
 - toUtf8 : creates a String from a byte array, assuming that this byte array is a valid UTF-8
String
 - getBytesUtf8 : returns a byte arry from a String

Those methods are usefull to avoid a bunch of try... catch (UnsupportedEncodingException)

Modified:
    directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java

Modified: directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java?rev=327752&r1=327751&r2=327752&view=diff
==============================================================================
--- directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java (original)
+++ directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java Sat
Oct 22 23:18:49 2005
@@ -1216,4 +1216,38 @@
     public static boolean equals(String str1, String str2) {
         return str1 == null ? str2 == null : str1.equals(str2);
     }
+    
+    /**
+     * Return an UTF-8 encoded String
+     * @param bytes The byte array to be transformed to a String
+     * @return A String. 
+     */
+    public static String toUtf8( byte[] bytes )
+    {
+        try
+        {
+            return new String( bytes, "UTF-8" );
+        }
+        catch ( UnsupportedEncodingException uee )
+        {
+            return "";
+        }
+    }
+
+    /**
+     * Return an UTF-8 encoded String
+     * @param bytes The byte array to be transformed to a String
+     * @return A String. 
+     */
+    public static byte[] getBytesUtf8( String string )
+    {
+        try
+        {
+            return string.getBytes( "UTF-8" );
+        }
+        catch ( UnsupportedEncodingException uee )
+        {
+            return new byte[]{};
+        }
+    }
 }



Mime
View raw message