Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* Copyright (c) 2015-2016 Microsoft Corporation. This software is licensed under the MIT License.
 * See the license file delivered with this project for further information.
 */

import java.nio.ByteBuffer;

import java.util.UUID;

public class Main {
    private static final int UUID_LENGTH_IN_BYTES = 16;

    /**
     * Converts the given byte array, which is expected to contain the UUID, into a UUID instance.
     *
     * @param byteArray The byte array containing the UUID.
     * @return A newly created UUID instance or null in case of a failure.
     */
    public static UUID byteArrayToUuid(byte[] byteArray) {
        if (byteArray != null && byteArray.length >= UUID_LENGTH_IN_BYTES) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
            long mostSignificantBits = byteBuffer.getLong();
            long leastSignificantBits = byteBuffer.getLong();
            return new UUID(mostSignificantBits, leastSignificantBits);
        }

        return null;
    }
}