]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/UuidBinaryUtils.java
Kerberos does not try to use shared state
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / UuidBinaryUtils.java
1 package org.argeo.api.uuid;
2
3 import java.util.Objects;
4 import java.util.UUID;
5
6 /** Static utilities around conversion of {@link UUID} from/to bytes. */
7 public class UuidBinaryUtils {
8 /**
9 * Singleton constructor, should only be extended to provide additional static
10 * utilities.
11 */
12 protected UuidBinaryUtils() {
13 }
14
15 /**
16 * Convert bytes to an UUID, starting to read the array at this offset.
17 */
18 public static UUID fromBytes(byte[] data, int offset) {
19 Objects.requireNonNull(data, "Byte array cannot be null");
20 long msb = 0;
21 long lsb = 0;
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);
27 }
28
29 /*
30 * UTILITIES
31 */
32 /**
33 * Convert bytes to an UUID. Byte array must not be null and be exactly of
34 * length 16.
35 */
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);
41 }
42
43 @Deprecated
44 protected static long longFromBytes(byte[] data) {
45 long msb = 0;
46 for (int i = 0; i < data.length; i++)
47 msb = (msb << 8) | (data[i] & 0xff);
48 return msb;
49 }
50
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);
57 }
58
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);
65 }
66
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);
74 return result;
75 }
76
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);
84 }
85
86 final protected static char[] hexArray = "0123456789abcdef".toCharArray();
87
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];
95 }
96 return new String(hexChars);
97 }
98
99 /*
100 * COMPACT STRING
101 */
102
103 /** To a 32 characters hex string without '-'. */
104 public static String toCompact(UUID uuid) {
105 return toHexString(toBytes(uuid));
106 }
107
108 /**
109 * Converts an UUID hex representation without '-' to an {@link UUID}.
110 */
111 public static UUID fromCompact(String compact) {
112 return UUID.fromString(UuidBinaryUtils.compactToStd(compact));
113 }
114
115 /**
116 * Converts an UUID hex representation without '-' to the standard form (with
117 * '-').
118 */
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)
126 sb.append('-');
127 sb.append(compact.charAt(i));
128 }
129 String std = sb.toString();
130 assert std.length() == 36;
131 assert UUID.fromString(std).toString().equals(std);
132 return std;
133 }
134
135 /**
136 * Converts an UUID to a binary string (list of 0 and 1), with a separator to
137 * make it more readable.
138 */
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));
147 }
148 return sb.toString();
149 }
150
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;
158 return binaryString;
159 }
160
161 /*
162 * LOW-LEVEL UTILITIES
163 */
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++)
169 sb.append('0');
170 sb.append(str);
171 return sb.toString();
172 } else
173 return str;
174 }
175
176 }