public final class

UUIDUtil

extends java.lang.Object

 java.lang.Object

↳androidx.room.util.UUIDUtil

Gradle dependencies

compile group: 'androidx.room', name: 'room-runtime', version: '2.5.0-alpha01'

  • groupId: androidx.room
  • artifactId: room-runtime
  • version: 2.5.0-alpha01

Artifact androidx.room:room-runtime:2.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.room:room-runtime android.arch.persistence.room:runtime

Overview

UUID / byte[] two-way conversion utility for Room

Summary

Methods
public static java.util.UUIDconvertByteToUUID(byte[] bytes[])

Converts a 16-bytes array BLOB into a UUID pojo

public static byte[]convertUUIDToByte(java.util.UUID uuid)

Converts a UUID pojo into a 16-bytes array to store into database as BLOB

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static java.util.UUID convertByteToUUID(byte[] bytes[])

Converts a 16-bytes array BLOB into a UUID pojo

Parameters:

bytes: byte array stored in database as BLOB

Returns:

a UUID object created based on the provided byte array

public static byte[] convertUUIDToByte(java.util.UUID uuid)

Converts a UUID pojo into a 16-bytes array to store into database as BLOB

Parameters:

uuid: the UUID pojo

Returns:

a byte array to store into database

Source

/*
 * Copyright 2021 The Android Open Source Project
 *
 * 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.
 */

package androidx.room.util;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

import java.nio.ByteBuffer;
import java.util.UUID;

/**
 * UUID / byte[] two-way conversion utility for Room
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public final class UUIDUtil {

    // private constructor to prevent instantiation
    private UUIDUtil() {}

    /**
     * Converts a 16-bytes array BLOB into a UUID pojo
     *
     * @param bytes byte array stored in database as BLOB
     * @return a UUID object created based on the provided byte array
     */
    @NonNull
    public static UUID convertByteToUUID(@NonNull byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        long firstLong = buffer.getLong();
        long secondLong = buffer.getLong();
        return new UUID(firstLong, secondLong);
    }

    /**
     * Converts a UUID pojo into a 16-bytes array to store into database as BLOB
     *
     * @param uuid the UUID pojo
     * @return a byte array to store into database
     */
    @NonNull
    public static byte[] convertUUIDToByte(@NonNull UUID uuid) {
        byte[] bytes = new byte[16];
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        buffer.putLong(uuid.getMostSignificantBits());
        buffer.putLong(uuid.getLeastSignificantBits());
        return buffer.array();
    }
}