directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r917168 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/util/StringTools.java test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
Date Sun, 28 Feb 2010 11:53:40 GMT
Author: seelmann
Date: Sun Feb 28 11:53:40 2010
New Revision: 917168

URL: http://svn.apache.org/viewvc?rev=917168&view=rev
Log:
Added uuidToBytes utility method

Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java?rev=917168&r1=917167&r2=917168&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
Sun Feb 28 11:53:40 2010
@@ -39,6 +39,7 @@
 import org.apache.directory.shared.asn1.codec.binary.Hex;
 import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
 import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
+import org.apache.directory.shared.ldap.schema.syntaxCheckers.UuidSyntaxChecker;
 
 
 /**
@@ -3752,4 +3753,44 @@
         return sb.toString().toLowerCase();
     }
 
+
+    /**
+     * converts the string representation of an UUID to bytes
+     *  
+     * @param string the string representation of an UUID
+     * @return the bytes, null if the the syntax is not valid
+     */
+    public static byte[] uuidToBytes( String string )
+    {
+        if ( !new UuidSyntaxChecker().isValidSyntax( string ) )
+        {
+            return null;
+        }
+
+        char[] chars = string.toCharArray();
+        byte[] bytes = new byte[16];
+        bytes[0] = getHexValue( chars[0], chars[1] );
+        bytes[1] = getHexValue( chars[2], chars[3] );
+        bytes[2] = getHexValue( chars[4], chars[5] );
+        bytes[3] = getHexValue( chars[6], chars[7] );
+
+        bytes[4] = getHexValue( chars[9], chars[10] );
+        bytes[5] = getHexValue( chars[11], chars[12] );
+
+        bytes[6] = getHexValue( chars[14], chars[15] );
+        bytes[7] = getHexValue( chars[16], chars[17] );
+
+        bytes[8] = getHexValue( chars[19], chars[20] );
+        bytes[9] = getHexValue( chars[21], chars[22] );
+
+        bytes[10] = getHexValue( chars[24], chars[25] );
+        bytes[11] = getHexValue( chars[26], chars[27] );
+        bytes[12] = getHexValue( chars[28], chars[29] );
+        bytes[13] = getHexValue( chars[30], chars[31] );
+        bytes[14] = getHexValue( chars[32], chars[33] );
+        bytes[15] = getHexValue( chars[34], chars[35] );
+
+        return bytes;
+    }
+
 }

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java?rev=917168&r1=917167&r2=917168&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
Sun Feb 28 11:53:40 2010
@@ -24,6 +24,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 import java.util.regex.Pattern;
 
 import javax.naming.NamingException;
@@ -401,4 +402,15 @@
         assertEquals( "  a", StringTools.trimRight( "  a" ) );
         assertEquals( "  a", StringTools.trimRight( "  a  " ) );
     }
+
+
+    @Test
+    public void testConvertUUID()
+    {
+        UUID uuid = UUID.randomUUID();
+        byte[] bytes = StringTools.uuidToBytes( uuid.toString() );
+        String string = StringTools.uuidToString( bytes );
+        assertEquals( uuid.toString(), string );
+    }
+
 }



Mime
View raw message