UUID.java
8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Decompiled with CFR 0_118.
*/
package com.day.util;
import java.io.*;
import java.net.InetAddress;
import java.security.MessageDigest;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
public class UUID
implements Serializable {
private final byte[] bytes;
private int hashCode;
private final int version;
public static final char[] hexDigits = "0123456789abcdef".toCharArray();
private static byte[] internetAddress = null;
private static File uuidFile = null;
private static final int UUIDsPerTick = 128;
private static long lastTime = new Date().getTime();
private static final long gregorianOffset = - new GregorianCalendar(1582, 9, 15).getTime().getTime();
private static int uuidsThisTick = 128;
private static short prevClock = 0;
private static long prevTime = 0;
private static byte[] prevNode = null;
private static long nextSave = new Date().getTime();
private static Random randomGenerator = new Random(new Date().getTime());
public UUID(String string) {
if (string.length() > 36) {
string = string.substring(0, 36);
}
this.bytes = new byte[16];
int j = 0;
for (int i = 0; i < 36; i += 2) {
this.bytes[j++] = (byte)Integer.parseInt(string.substring(i, i + 2), 16);
if (i != 6 && i != 11 && i != 16 && i != 21) continue;
++i;
}
this.version = this.bytes[7] >> 4 & 15;
}
public UUID(byte[] bytes) {
this.bytes = new byte[16];
System.arraycopy(bytes, 0, this.bytes, 0, Math.min(16, bytes.length));
this.version = bytes[7] >> 4 & 15;
}
public UUID(long[] longs) {
int i;
long hi = longs[0];
long lo = longs[1];
this.bytes = new byte[16];
for (i = 7; i >= 0; --i) {
this.bytes[i] = (byte)(hi & 255);
hi >>= 8;
}
for (i = 7; i >= 0; --i) {
this.bytes[8 + i] = (byte)(lo & 255);
lo >>= 8;
}
this.version = this.bytes[7] >> 4 & 15;
}
public UUID(String name, String namespace) {
this.version = 3;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(namespace.getBytes());
md5.update(name.getBytes());
this.bytes = md5.digest();
this.bytes[7] = (byte)(this.bytes[7] & 15 | (byte)(this.version << 4));
this.bytes[8] = (byte)(this.bytes[8] & 63 | 128);
return;
}
catch (Exception exc) {
throw new InternalError("MD5 not available");
}
}
public UUID() {
this.version = 4;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);
out.writeLong(System.currentTimeMillis());
out.writeInt(Thread.currentThread().hashCode());
out.write(internetAddress);
out.writeInt(UUID.random());
out.flush();
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteOut.toByteArray());
this.bytes = md5.digest();
this.bytes[7] = (byte)(this.bytes[7] & 15 | (byte)(this.version << 4));
this.bytes[8] = (byte)(this.bytes[8] & 63 | 128);
return;
}
catch (Exception exc) {
throw new InternalError("MD5 not available");
}
}
public UUID(byte[] node, long time, short clock) {
int i;
this.version = 1;
this.bytes = new byte[16];
long t = time;
for (i = 0; i < 8; ++i) {
this.bytes[i] = (byte)(t & 255);
t >>= 8;
}
byte[] arrby = this.bytes;
arrby[7] = (byte)(arrby[7] | (byte)(this.version << 4));
this.bytes[8] = (byte)(clock >> 8 & 63 | 128);
this.bytes[9] = (byte)(clock & 255);
for (i = 0; i < 6; ++i) {
this.bytes[10 + i] = node[i];
}
}
public int getVersion() {
return this.version;
}
public boolean equals(Object toUUID) {
if (this == toUUID) {
return true;
}
if (toUUID instanceof UUID) {
UUID uuid = (UUID)toUUID;
if (this.bytes == uuid.bytes) {
return true;
}
for (int i = 0; i < 16; ++i) {
if (this.bytes[i] == uuid.bytes[i]) continue;
return false;
}
return true;
}
return false;
}
public String toString() {
char[] chars = new char[36];
int j = 0;
for (int i = 0; i < 16; ++i) {
chars[j++] = hexDigits[this.bytes[i] >> 4 & 15];
chars[j++] = hexDigits[this.bytes[i] & 15];
if (i != 3 && i != 5 && i != 7 && i != 9) continue;
chars[j++] = 45;
}
return new String(chars);
}
public byte[] getBytes() {
return this.bytes;
}
public int hashCode() {
int h = this.hashCode;
if (h == 0) {
for (int i = 0; i < this.bytes.length; ++i) {
h = 31 * h + this.bytes[i];
}
this.hashCode = h;
}
return this.hashCode;
}
public static void init(File uuidStateFile) {
try {
internetAddress = InetAddress.getLocalHost().getAddress();
}
catch (Exception exc) {
throw new InternalError("Unable to get host address: " + exc);
}
uuidFile = uuidStateFile;
UUID.loadState();
}
public static synchronized UUID create() {
long time = UUID.getCurrentTime();
short clock = prevClock;
byte[] node = prevNode;
if (prevTime > time) {
clock = (short)(clock + 1);
}
UUID uuid = new UUID(node, time, clock);
UUID.saveState(time, clock, node);
return uuid;
}
private static byte[] computeNodeAddress() {
MessageDigest md5;
byte[] address = new byte[6];
int thread = Thread.currentThread().hashCode();
long time = System.currentTimeMillis();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);
try {
if (internetAddress != null) {
out.write(internetAddress);
}
out.write(thread);
out.writeLong(time);
out.close();
}
catch (IOException exc) {
// empty catch block
}
byte[] rand = byteOut.toByteArray();
try {
md5 = MessageDigest.getInstance("MD5");
}
catch (Exception exc) {
throw new InternalError(exc.toString());
}
md5.update(rand);
byte[] temp = md5.digest();
for (int i = 0; i < 6; ++i) {
address[i] = temp[i + 5];
}
address[0] = (byte)(address[0] | -128);
return address;
}
private static synchronized long getCurrentTime() {
long now = 0;
boolean waitForTick = true;
while (waitForTick) {
now = (new Date().getTime() + gregorianOffset) * 10;
if (lastTime < now) {
uuidsThisTick = 0;
waitForTick = false;
continue;
}
if (uuidsThisTick >= 128) continue;
++uuidsThisTick;
waitForTick = false;
}
lastTime = now += (long)uuidsThisTick;
return now;
}
private static byte[] getIEEEAddress() {
byte[] address = new byte[6];
return address;
}
private static int random() {
return randomGenerator.nextInt();
}
private static void loadState() {
try {
ObjectInputStream s = new ObjectInputStream(new FileInputStream(uuidFile));
prevTime = s.readLong();
prevClock = s.readShort();
prevNode = new byte[6];
s.readFully(prevNode);
s.close();
}
catch (Exception exc) {
prevNode = UUID.computeNodeAddress();
prevTime = 0;
prevClock = (short)UUID.random();
}
}
private static void saveState(long time, short clock, byte[] node) {
prevTime = time;
prevClock = clock;
prevNode = node;
if (prevTime > nextSave) {
try {
ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(uuidFile));
s.writeLong(prevTime);
s.writeShort(prevClock);
s.write(prevNode);
s.close();
nextSave = prevTime + 100000;
}
catch (Exception exc) {
// empty catch block
}
}
}
}