You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/12/25 18:44:37 UTC

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

Author: elecharny
Date: Sun Dec 25 09:44:33 2005
New Revision: 358994

URL: http://svn.apache.org/viewcvs?rev=358994&view=rev
Log:
- Added a EMPTY_BYTES constant
- Added an array HEX_VALUE to encode hex char to their int value
- Added a charToBytes() method
- Added an isEmpty(byte[]) method
- Added a trim(byte[]) method
- Added a listToString( List, String tabs) method
- Added a mapToString( List, String tabs) method

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=358994&r1=358993&r2=358994&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 Sun Dec 25 09:44:33 2005
@@ -112,6 +112,18 @@
         false, false, false, false, false, false, false, false, false
     };
 
+    /** <hex>    ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+    public static final byte[] HEX_VALUE =
+    {
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00 -> 0F
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10 -> 1F
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20 -> 2F
+         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, // 30 -> 3F ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
+        -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 40 -> 4F ( A, B, C, D, E, F )
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 50 -> 5F
+        -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1  // 60 -> 6F ( a, b, c, d, e, f )
+    };
+
     private static int CHAR_ONE_BYTE_MASK    = 0xFFFFFF80;
     
     private static int CHAR_TWO_BYTES_MASK   = 0xFFFFF800;
@@ -412,6 +424,41 @@
     }
     
     /**
+     * Return the Unicode char which is coded in the bytes at the given position. 
+     * @param bytes The byte[] represntation of an Unicode string. 
+     * @param pos The current position to start decoding the char
+     * @return The decoded char, or -1 if no char can be decoded
+     * 
+     * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+     */
+    public static byte[] charToBytes(char car)
+    {
+    	byte[] bytes = new byte[ countNbBytesPerChar( car ) ];
+    	
+    	if ( car <= 0x7F )
+    	{
+    		// Single byte char
+    		bytes[0] = (byte)car;
+    		return bytes;
+    	}
+    	else if ( car <= 0x7FF )
+    	{
+    		// two bytes char
+    		bytes[0] = (byte)(0x00C0 + ( ( car & 0x07C0 ) >> 6 ) );
+        	bytes[1] = (byte)(0x0080 + ( car & 0x3F ) );
+    	}
+    	else 
+    	{
+    		// Three bytes char
+    		bytes[0] = (byte)(0x00E0 + ( ( car & 0xF000 ) >> 12 ) );
+        	bytes[1] = (byte)(0x0080 + ( ( car & 0x0FC0 ) >> 6 ) );
+            bytes[2] = (byte)(0x0080 + ( car & 0x3F ) );
+    	}
+    	
+    	return bytes;
+    }
+    
+    /**
      * Count the number of chars included in the given byte[].  
      * @param bytes The byte array to decode
      * @return The number of char in the byte array
@@ -887,6 +934,11 @@
      * @since 2.0
      */
     public static final String EMPTY = "";
+    
+    /**
+     * The empty byte[]
+     */
+    public static final byte[] EMPTY_BYTES = new byte[]{};
 
     // Empty checks
     //-----------------------------------------------------------------------
@@ -913,6 +965,15 @@
     }
 
     /**
+     * Checks if a bytes array is empty or null. 
+     * @param bytes The bytes array to check, may be null
+     * @return <code>true</code> if the bytes array is empty or null
+     */
+    public static boolean isEmpty(byte[] bytes) {
+        return bytes == null || bytes.length == 0;
+    }
+
+    /**
      * <p>Checks if a String is not empty ("") and not null.</p>
      *
      * <pre>
@@ -972,6 +1033,49 @@
     }
 
     /**
+     * <p>Removes spaces (char &lt;= 32) from both start and
+     * ends of this bytes array, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start and end characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trim(null)          = null
+     * StringUtils.trim("")            = ""
+     * StringUtils.trim("     ")       = ""
+     * StringUtils.trim("abc")         = "abc"
+     * StringUtils.trim("    abc    ") = "abc"
+     * </pre>
+     *
+     * @param str  the String to be trimmed, may be null
+     * @return the trimmed string, <code>null</code> if null String input
+     */
+    public static byte[] trim(byte[] bytes) {
+        if ( isEmpty( bytes ) )
+        {
+            return EMPTY_BYTES;
+        }
+
+        int start = trimLeft( bytes, 0 );
+        int end = trimRight( bytes, bytes.length - 1 );
+        
+        int length = end - start + 1;
+        
+        if ( length != 0 )
+        {
+	        byte[] newBytes = new byte[ end - start + 1 ];
+	        
+	        System.arraycopy( bytes, start, newBytes, 0, length );
+	        
+	        return newBytes;
+        }
+        else
+        {
+        	return EMPTY_BYTES;
+        }
+    }
+
+    /**
      * <p>Removes spaces (char &lt;= 32) from start
      * of this String, handling <code>null</code> by returning
      * <code>null</code>.</p>
@@ -1323,6 +1427,32 @@
     }
     
     /**
+     * Utility method that return a String representation of a list
+     * @param list The list to transform to a string
+     * @return A csv string 
+     */
+    public static String listToString( List list, String tabs )
+    {
+    	if ( ( list == null ) || ( list.size() == 0 ) )
+    	{
+    		return "";
+    	}
+    	
+    	StringBuffer sb = new StringBuffer();
+    	
+    	Iterator iter = list.iterator();
+    	
+    	while ( iter.hasNext() )
+    	{
+  			sb.append( tabs );
+    		sb.append( iter.next() );
+  			sb.append( '\n' );
+    	}
+    	
+    	return sb.toString();
+    }
+        
+    /**
      * Utility method that return a String representation of a map. The elements
      * will be represented as "key = value"
      * @param map The map to transform to a string
@@ -1354,6 +1484,36 @@
     		Object key = iter.next();
     		sb.append( key );
     		sb.append( " = '" ).append( map.get( key ) ).append( "'" );
+    	}
+    	
+    	return sb.toString();
+    }
+
+    /**
+     * Utility method that return a String representation of a map. The elements
+     * will be represented as "key = value"
+     * @param map The map to transform to a string
+     * @return A csv string 
+     */
+    public static String mapToString( Map map, String tabs )
+    {
+    	if ( ( map == null ) || ( map.size() == 0 ) )
+    	{
+    		return "";
+    	}
+    	
+    	StringBuffer sb = new StringBuffer();
+    	
+    	Iterator iter = map.keySet().iterator();
+    	
+    	while ( iter.hasNext() )
+    	{
+    		Object key = iter.next();
+    		sb.append( tabs );
+    		sb.append( key );
+    		Object value = map.get( key );
+    		
+    		sb.append( " = '" ).append( value.toString() ).append( "'\n" );
     	}
     	
     	return sb.toString();