1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 @file:JvmName("UUIDUtil")
17 @file:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) // used in generated code
18 
19 package androidx.room.util
20 
21 import androidx.annotation.RestrictTo
22 import java.nio.ByteBuffer
23 import java.util.UUID
24 
25 /** UUID / byte[] two-way conversion utility for Room */
26 
27 /**
28  * Converts a 16-bytes array BLOB into a UUID pojo
29  *
30  * @param bytes byte array stored in database as BLOB
31  * @return a UUID object created based on the provided byte array
32  */
convertByteToUUIDnull33 fun convertByteToUUID(bytes: ByteArray): UUID {
34     val buffer = ByteBuffer.wrap(bytes)
35     val firstLong = buffer.long
36     val secondLong = buffer.long
37     return UUID(firstLong, secondLong)
38 }
39 
40 /**
41  * Converts a UUID pojo into a 16-bytes array to store into database as BLOB
42  *
43  * @param uuid the UUID pojo
44  * @return a byte array to store into database
45  */
convertUUIDToBytenull46 fun convertUUIDToByte(uuid: UUID): ByteArray {
47     val bytes = ByteArray(16)
48     val buffer = ByteBuffer.wrap(bytes)
49     buffer.putLong(uuid.mostSignificantBits)
50     buffer.putLong(uuid.leastSignificantBits)
51     return buffer.array()
52 }
53