Java Convert via ByteBuffer toGuidBytes(UUID theUuid)

Here you can find the source of toGuidBytes(UUID theUuid)

Description

to Guid Bytes

License

Open Source License

Declaration

public static byte[] toGuidBytes(UUID theUuid) 

Method Source Code

//package com.java2s;
//  License: https://servicestack.net/bsd-license.txt

import java.nio.ByteBuffer;

import java.util.UUID;

public class Main {
    public static byte[] toGuidBytes(UUID theUuid) {
        ByteBuffer first8 = ByteBuffer.allocate(8);
        first8.putLong(theUuid.getMostSignificantBits());
        first8.rewind();/*from   w w  w . j a  va  2s .c om*/

        byte[] first4 = new byte[4];
        first8.get(first4);
        reverse(first4);

        byte[] second2 = new byte[2];
        first8.get(second2);
        reverse(second2);

        byte[] third2 = new byte[2];
        first8.get(third2);
        reverse(third2);

        ByteBuffer converted16 = ByteBuffer.allocate(16).put(first4).put(second2).put(third2);

        ByteBuffer last8 = ByteBuffer.allocate(8);
        last8.putLong(theUuid.getLeastSignificantBits());
        last8.rewind();

        converted16.put(last8);

        return converted16.array();
    }

    public static void reverse(byte[] bytes) {
        if (bytes == null)
            return;

        int i = 0;
        int j = bytes.length - 1;
        byte hold;
        while (j > i) {
            hold = bytes[j];
            bytes[j] = bytes[i];
            bytes[i] = hold;
            j--;
            i++;
        }
    }
}

Related

  1. toFiles(URL[] urls)
  2. toFloat(byte[] bytes)
  3. toFloat(final byte[] b)
  4. toFloatArray(int[] intArray)
  5. toFloatBytes(List datas)
  6. toGuidString(UUID uuid)
  7. toHex(byte[] bytes)
  8. toHexBytes(byte[] bytes)
  9. toHexString(Number n)