Java UUID to Byte Array uuidToBytes(UUID uuid)

Here you can find the source of uuidToBytes(UUID uuid)

Description

Converts an UUID to byte array.

License

Open Source License

Parameter

Parameter Description
uuid UUID value.

Return

Encoded into byte array .

Declaration

public static byte[] uuidToBytes(UUID uuid) 

Method Source Code


//package com.java2s;
/* /*from   w w w .j  a v a 2 s  .  c om*/
 Copyright (C) GridGain Systems. All Rights Reserved.
     
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
     
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.nio.*;
import java.util.*;

public class Main {
    /**
     * Converts an UUID to byte array.
     *
     * @param uuid UUID value.
     * @return Encoded into byte array {@link UUID}.
     */
    public static byte[] uuidToBytes(UUID uuid) {
        byte[] bytes = new byte[(Long.SIZE >> 3) * 2];

        uuidToBytes(uuid, bytes, 0);

        return bytes;
    }

    /**
     * Converts {@code UUID} type to byte array and stores it in specified byte array.
     *
     * @param uuid UUID to convert.
     * @param bytes Array of bytes.
     * @param off Offset in {@code bytes} array.
     * @return Number of bytes overwritten in {@code bytes} array.
     */
    public static int uuidToBytes(UUID uuid, byte[] bytes, int off) {

        ByteBuffer buf = ByteBuffer.wrap(bytes, off, 16);

        buf.order(ByteOrder.BIG_ENDIAN);

        if (uuid != null) {
            buf.putLong(uuid.getMostSignificantBits());
            buf.putLong(uuid.getLeastSignificantBits());
        } else {
            buf.putLong(0);
            buf.putLong(0);
        }

        return 16;
    }
}

Related

  1. uuidToBase58(UUID uuid)
  2. uuidToByteArray(UUID uuid)
  3. uuidToBytes(UUID id)
  4. uuidToBytes(UUID uuid)
  5. uuidToBytes(UUID uuid)
  6. uuidToBytesNullOk(UUID uuid)