You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/11/10 10:46:27 UTC

svn commit: r332254 - /directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java

Author: trustin
Date: Thu Nov 10 01:46:25 2005
New Revision: 332254

URL: http://svn.apache.org/viewcvs?rev=332254&view=rev
Log:
Prevented unexpected NullPointerExceptions in StringUtils

Modified:
    directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java

Modified: directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java?rev=332254&r1=332253&r2=332254&view=diff
==============================================================================
--- directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java (original)
+++ directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java Thu Nov 10 01:46:25 2005
@@ -145,6 +145,11 @@
      */
     public static String dumpBytes( byte[] buffer )
     {
+        if( buffer == null )
+        {
+            return "";
+        }
+
         StringBuffer sb = new StringBuffer();
         
         for (int i = 0; i < buffer.length ; i++)
@@ -178,6 +183,11 @@
      */
     public static int countBytesPerChar(byte[] bytes, int pos)
     {
+        if( bytes == null )
+        {
+            return -1;
+        }
+
         if ((bytes[pos] & UTF8_MULTI_BYTES_MASK) == 0)
         {
             return 1;
@@ -254,6 +264,11 @@
      */
     public static int countBytes(char[] chars)
     {
+        if( chars == null )
+        {
+            return 0;
+        }
+
         int nbBytes = 0;
         int currentPos = 0;
         
@@ -280,6 +295,11 @@
      */
     public static char bytesToChar(byte[] bytes, int pos)
     {
+        if( bytes == null )
+        {
+            return ( char ) -1;
+        }
+
     	if ((bytes[pos] & UTF8_MULTI_BYTES_MASK) == 0)
 		{
     		return (char)bytes[pos];
@@ -395,6 +415,11 @@
      */
     public static int countChars(byte[] bytes)
     {
+        if( bytes == null )
+        {
+            return 0;
+        }
+
         int nbChars = 0;
         int currentPos = 0;
         
@@ -923,7 +948,7 @@
     public static String trim(String str) {
         if ( isEmpty( str ) )
         {
-            return str;
+            return "";
         }
         
         char[] array = str.toCharArray();
@@ -964,7 +989,7 @@
     public static String trimLeft( String str ) {
         if ( isEmpty( str ) )
         {
-            return str;
+            return "";
         }
         
         char[] array = str.toCharArray();
@@ -1065,7 +1090,7 @@
     public static String trimRight( String str ) {
         if ( isEmpty( str ) )
         {
-            return str;
+            return "";
         }
         
         char[] array = str.toCharArray();
@@ -1223,6 +1248,11 @@
      */
     public static String toUtf8( byte[] bytes )
     {
+        if( bytes == null )
+        {
+            return "";
+        }
+
         try
         {
             return new String( bytes, "UTF-8" );
@@ -1240,6 +1270,11 @@
      */
     public static byte[] getBytesUtf8( String string )
     {
+        if( string == null )
+        {
+            return new byte[0];
+        }
+
         try
         {
             return string.getBytes( "UTF-8" );