forked from TrueCloudLab/frostfs-sdk-java
parent
7518893388
commit
ab8a574d0d
14 changed files with 139 additions and 25 deletions
35
models/src/main/java/info/frostfs/sdk/UuidExtension.java
Normal file
35
models/src/main/java/info/frostfs/sdk/UuidExtension.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class UuidExtension {
|
||||
private static final int UUID_BYTE_ARRAY_LENGTH = 16;
|
||||
|
||||
private UuidExtension() {
|
||||
}
|
||||
|
||||
public static UUID asUuid(byte[] bytes) {
|
||||
if (isNull(bytes) || bytes.length != UUID_BYTE_ARRAY_LENGTH) {
|
||||
throw new IllegalArgumentException("Uuid byte array length must be " + UUID_BYTE_ARRAY_LENGTH);
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bytes);
|
||||
long firstLong = bb.getLong();
|
||||
long secondLong = bb.getLong();
|
||||
return new UUID(firstLong, secondLong);
|
||||
}
|
||||
|
||||
public static byte[] asBytes(UUID uuid) {
|
||||
if (isNull(uuid)) {
|
||||
throw new IllegalArgumentException("Uuid is not present");
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(UUID_BYTE_ARRAY_LENGTH);
|
||||
bb.putLong(uuid.getMostSignificantBits());
|
||||
bb.putLong(uuid.getLeastSignificantBits());
|
||||
return bb.array();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue