package org.ovirt.engine.core.utils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.compat.Guid;

public class GuidUtils {

    public static byte[] toByteArray(UUID uuid) {
        String guidStr = uuid.toString();

        // Going to split the GUID to hexadecimal sequences.
        // Each sequence like that contains hexadecimal numbers that should be
        // converted to bytes.
        // As GUID may vary in size , the bytes are written to a byte array
        // output stream
        // which is kept in
        String[] guidParts = guidStr.split("-");
        if (guidParts == null || guidParts.length == 0) {
            return null;
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        for (String guidPart : guidParts) {
            writeGUIDPartToStream(guidPart, baos);

        }

        return baos.toByteArray();
    }

    private static void writeGUIDPartToStream(String guidPart, ByteArrayOutputStream baos) {
        // GuidPart is composed from an even number of characters.
        // Each two characters are a hexadecimal number

        char[] dst = new char[guidPart.length()];
        guidPart.getChars(0, guidPart.length(), dst, 0);
        for (int counter = 0; counter < (guidPart.length()) / 2; counter++) {
            // Build a string from two characters
            StringBuilder numberStrSB = new StringBuilder(2);
            numberStrSB.append(dst[counter * 2]);
            numberStrSB.append(dst[(counter * 2) + 1]);
            // Convert the string to byte and add write it to the stream
            int number = Integer.parseInt(numberStrSB.toString(), 16);
            baos.write(number);
        }
    }

    private static final String SEPARATOR = ",";

    /**
     * Gets a string containing multiple <code>Guid</code> values separated by a comma and returns an ArrayList of
     * <code>Guid</code>. If the String is null/empty returns an empty array.
     *
     * @param str
     *            - String which contains list of <code>Guid</code>.
     * @return - Array of <code>Guid</code> type.
     */
    public static ArrayList<Guid> getGuidListFromString(String str) {
        if (StringUtils.isEmpty(str)) {
            return new ArrayList<Guid>();
        }
        return getGuidListFromStringArray(Arrays.asList(str.split(SEPARATOR)));
    }

    /**
     * Gets a List of strings containing multiple <code>Guid</code> values and returns an ArrayList of <code>Guid</code>
     * . If the list is null/empty returns an empty ArrayList.
     *
     * @param strings
     *            - Array of Strings which contains <code>Guid</code> values.
     * @return - Array of <code>Guid</code> type.
     */
    public static ArrayList<Guid> getGuidListFromStringArray(List<String> strings) {
        ArrayList<Guid> guidList = new ArrayList<Guid>();
        if (strings != null && !strings.isEmpty()) {
            for (String guidString : strings) {
                guidList.add(Guid.createGuidFromStringDefaultEmpty(guidString));
            }
        }
        return guidList;
    }
}

@@ -35,46 +35,8 @@
 s;%0A%0A
-import java.io.ByteArrayOutputStream;%0A
 impo
@@ -306,648 +306,190 @@
     
-String guidStr = uuid.toString();%0A%0A        // Going to split the GUID to hexadecimal sequences.%0A        // Each sequence like that contains hexadecimal numbers that should be%0A        // converted to bytes.%0A        // As GUID may vary in size , the bytes are written to a byte array%0A        // output stream%0A        // which is kept in%0A        String%5B%5D guidParts = guidStr.split(%22-%22);%0A        if (guidParts == null %7C%7C guidParts.length == 0) %7B%0A            return null;%0A        %7D%0A%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A%0A        for (String guidPart : guidParts) %7B%0A            writeGUIDPartToStream(guidPart, baos);%0A
+byte%5B%5D data = new byte%5B16%5D;%0A        long msb = uuid.getMostSignificantBits();%0A        for (int i = 7; i %3E= 0; i--) %7B%0A            data%5Bi%5D = (byte) (msb & 0xff);%0A            msb %3E%3E= 8;
 %0A   
@@ -495,25 +495,24 @@
       %7D%0A
-%0A
         
 return b
@@ -507,847 +507,187 @@
     
-return baos.toByteArray();%0A    %7D%0A%0A    private static void writeGUIDPartToStream(String guidPart, ByteArrayOutputStream baos) %7B%0A        // GuidPart is composed from an even number of characters.%0A        // Each two characters are a hexadecimal number%0A%0A        char%5B%5D dst = new char%5BguidPart.length()%5D;%0A        guidPart.getChars(0, guidPart.length(), dst, 0);%0A        for (int counter = 0; counter %3C (guidPart.length()) / 2; counter++) %7B%0A            // Build a string from two characters%0A            StringBuilder numberStrSB = new StringBuilder(2);%0A            numberStrSB.append(dst%5Bcounter * 2%5D);%0A            numberStrSB.append(dst%5B(counter * 2) + 1%5D);%0A            // Convert the string to byte and add write it to the stream%0A            int number = Integer.parseInt(numberStrSB.toString(), 16);%0A            baos.write(number);%0A        %7D
+long lsb = uuid.getLeastSignificantBits();%0A        for (int i = 15; i %3E= 8; i--) %7B%0A            data%5Bi%5D = (byte) (lsb & 0xff);%0A            lsb %3E%3E= 8;%0A        %7D%0A        return data;
 %0A   

Improve performance of GuidUtils.toByteArray