]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/UuidBinaryUtils.java
1 package org
.argeo
.api
.uuid
;
3 import java
.util
.Objects
;
6 /** Static utilities around conversion of {@link UUID} from/to bytes. */
7 public class UuidBinaryUtils
{
9 * Singleton constructor, should only be extended to provide additional static
12 protected UuidBinaryUtils() {
16 * Convert bytes to an UUID, starting to read the array at this offset.
18 public static UUID
fromBytes(byte[] data
, int offset
) {
19 Objects
.requireNonNull(data
, "Byte array cannot be null");
22 for (int i
= offset
; i
< 8 + offset
; i
++)
23 msb
= (msb
<< 8) | (data
[i
] & 0xff);
24 for (int i
= 8 + offset
; i
< 16 + offset
; i
++)
25 lsb
= (lsb
<< 8) | (data
[i
] & 0xff);
26 return new UUID(msb
, lsb
);
33 * Convert bytes to an UUID. Byte array must not be null and be exactly of
36 public static UUID
fromBytes(byte[] data
) {
37 Objects
.requireNonNull(data
, "Byte array must not be null");
38 if (data
.length
!= 16)
39 throw new IllegalArgumentException("Byte array as length " + data
.length
);
40 return fromBytes(data
, 0);
44 protected static long longFromBytes(byte[] data
) {
46 for (int i
= 0; i
< data
.length
; i
++)
47 msb
= (msb
<< 8) | (data
[i
] & 0xff);
51 /** Convert this UUID to a byte array of length 16. */
52 public static byte[] toBytes(UUID uuid
) {
53 Objects
.requireNonNull(uuid
, "UUID cannot be null");
54 long msb
= uuid
.getMostSignificantBits();
55 long lsb
= uuid
.getLeastSignificantBits();
56 return toBytes(msb
, lsb
);
59 /** Copies this {@link UUID} as bytes, using 16 bytes. */
60 public static void copyBytes(UUID uuid
, byte[] arr
, int offset
) {
61 Objects
.requireNonNull(uuid
, "UUID cannot be null");
62 long msb
= uuid
.getMostSignificantBits();
63 long lsb
= uuid
.getLeastSignificantBits();
64 copyBytes(msb
, lsb
, arr
, offset
);
67 /** Convert two longs to a byte array with length 16. */
68 protected static byte[] toBytes(long long1
, long long2
) {
69 byte[] result
= new byte[16];
70 for (int i
= 0; i
< 8; i
++)
71 result
[i
] = (byte) ((long1
>> ((7 - i
) * 8)) & 0xff);
72 for (int i
= 8; i
< 16; i
++)
73 result
[i
] = (byte) ((long2
>> ((15 - i
) * 8)) & 0xff);
77 /** Copy these two longs to this byte array, using 16 bytes. */
78 protected static void copyBytes(long long1
, long long2
, byte[] arr
, int offset
) {
79 assert arr
.length
>= 16 + offset
;
80 for (int i
= offset
; i
< 8 + offset
; i
++)
81 arr
[i
] = (byte) ((long1
>> ((7 - i
) * 8)) & 0xff);
82 for (int i
= 8 + offset
; i
< 16 + offset
; i
++)
83 arr
[i
] = (byte) ((long2
>> ((15 - i
) * 8)) & 0xff);
86 final protected static char[] hexArray
= "0123456789abcdef".toCharArray();
88 /** Converts a byte array to an hex String. */
89 public static String
toHexString(byte[] bytes
) {
90 char[] hexChars
= new char[bytes
.length
* 2];
91 for (int j
= 0; j
< bytes
.length
; j
++) {
92 int v
= bytes
[j
] & 0xFF;
93 hexChars
[j
* 2] = hexArray
[v
>>> 4];
94 hexChars
[j
* 2 + 1] = hexArray
[v
& 0x0F];
96 return new String(hexChars
);
103 /** To a 32 characters hex string without '-'. */
104 public static String
toCompact(UUID uuid
) {
105 return toHexString(toBytes(uuid
));
109 * Converts an UUID hex representation without '-' to an {@link UUID}.
111 public static UUID
fromCompact(String compact
) {
112 return UUID
.fromString(UuidBinaryUtils
.compactToStd(compact
));
116 * Converts an UUID hex representation without '-' to the standard form (with
119 public static String
compactToStd(String compact
) {
120 if (compact
.length() != 32)
121 throw new IllegalArgumentException(
122 "Compact UUID '" + compact
+ "' has length " + compact
.length() + " and not 32.");
123 StringBuilder sb
= new StringBuilder(36);
124 for (int i
= 0; i
< 32; i
++) {
125 if (i
== 8 || i
== 12 || i
== 16 || i
== 20)
127 sb
.append(compact
.charAt(i
));
129 String std
= sb
.toString();
130 assert std
.length() == 36;
131 assert UUID
.fromString(std
).toString().equals(std
);
136 * Converts an UUID to a binary string (list of 0 and 1), with a separator to
137 * make it more readable.
139 public static String
toBinaryString(UUID uuid
, int charsPerSegment
, char separator
) {
140 Objects
.requireNonNull(uuid
, "UUID cannot be null");
141 String binaryString
= UuidBinaryUtils
.toBinaryString(uuid
);
142 StringBuilder sb
= new StringBuilder(128 + (128 / charsPerSegment
));
143 for (int i
= 0; i
< binaryString
.length(); i
++) {
144 if (i
!= 0 && i
% charsPerSegment
== 0)
145 sb
.append(separator
);
146 sb
.append(binaryString
.charAt(i
));
148 return sb
.toString();
151 /** Converts an UUID to a binary string (list of 0 and 1). */
152 public static String
toBinaryString(UUID uuid
) {
153 Objects
.requireNonNull(uuid
, "UUID cannot be null");
154 String most
= zeroTo64Chars(Long
.toBinaryString(uuid
.getMostSignificantBits()));
155 String least
= zeroTo64Chars(Long
.toBinaryString(uuid
.getLeastSignificantBits()));
156 String binaryString
= most
+ least
;
157 assert binaryString
.length() == 128;
162 * LOW-LEVEL UTILITIES
164 protected static String
zeroTo64Chars(String str
) {
165 assert str
.length() <= 64;
166 if (str
.length() < 64) {
167 StringBuilder sb
= new StringBuilder(64);
168 for (int i
= 0; i
< 64 - str
.length(); i
++)
171 return sb
.toString();