Skip to main content
summaryrefslogtreecommitdiffstats
blob: 82a1f8e45a1a50cb71a29dc37c65ad46c0dc5a58 (plain) (blame)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util;

import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.SecureRandom;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * Generates 16 byte UUID values and can encode them to Strings, decode from Strings respectively.
 * 
 * @author Eike Stepper
 * @since 3.2
 */
public final class UUIDGenerator
{
  public static final int NODE_ADDRESS_BYTES = 6;

  public static final UUIDGenerator DEFAULT = new UUIDGenerator();

  public UUIDGenerator(byte[] nodeAddress)
  {
    Random random = new SecureRandom();

    clockSequence = (short)random.nextInt(16384);
    updateClockSequence();

    if (nodeAddress == null)
    {
      try
      {
        nodeAddress = getHardwareAddress();
      }
      catch (Throwable ex)
      {
        //$FALL-THROUGH$
      }

      if (nodeAddress == null || nodeAddress.length != NODE_ADDRESS_BYTES)
      {
        // Generate a 48 bit node identifier;
        // This is an alternative to the IEEE 802 host address, which is not available in Java.
        nodeAddress = new byte[NODE_ADDRESS_BYTES];
        random.nextBytes(nodeAddress);
      }
    }

    setNodeAddress(nodeAddress);
  }

  public UUIDGenerator()
  {
    this(null);
  }

  public synchronized String generate()
  {
    updateCurrentTime();
    encode(uuid, buffer);
    return new String(buffer);
  }

  public synchronized void generate(byte[] uuid)
  {
    updateCurrentTime();

    for (int i = 0; i < 16; i++)
    {
      uuid[i] = this.uuid[i];
    }
  }

  public String encode(byte[] uuid)
  {
    char[] buffer = createBuffer();
    encode(uuid, buffer);
    return new String(buffer);
  }

  public byte[] decode(String string)
  {
    byte[] uuid = createUUID();

    char c1;
    char c2;
    char c3;
    char c4;

    int i1;
    int i2;
    int i3;
    int i4;

    for (int i = 0; i < 5; ++i)
    {
      c1 = string.charAt(4 * i + 1);
      c2 = string.charAt(4 * i + 2);
      c3 = string.charAt(4 * i + 3);
      c4 = string.charAt(4 * i + 4);

      i1 = BASE64_INDEX[c1 - BASE64_INDEX_OFFSET];
      i2 = BASE64_INDEX[c2 - BASE64_INDEX_OFFSET];
      i3 = BASE64_INDEX[c3 - BASE64_INDEX_OFFSET];
      i4 = BASE64_INDEX[c4 - BASE64_INDEX_OFFSET];

      uuid[3 * i] = (byte)(i1 << 2 | i2 >>> 4);
      uuid[3 * i + 1] = (byte)((i2 & 0xF) << 4 | i3 >>> 2);
      uuid[3 * i + 2] = (byte)((i3 & 0x3) << 6 | i4);
    }

    // Handle the last chars at the end.
    //
    c1 = string.charAt(21);
    c2 = string.charAt(22);

    i1 = BASE64_INDEX[c1 - BASE64_INDEX_OFFSET];
    i2 = BASE64_INDEX[c2 - BASE64_INDEX_OFFSET];

    uuid[15] = (byte)(i1 << 2 | i2 >>> 4);

    return uuid;
  }

  private static final char[] BASE64_DIGITS = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
      'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
      'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
      '6', '7', '8', '9', '-', '_' };

  private static final byte[] BASE64_INDEX;

  private static final int BASE64_INDEX_OFFSET;

  /**
   * An adjustment to convert the Java epoch of Jan 1, 1970 00:00:00 to the epoch required by the IETF specification,
   * Oct 15, 1582 00:00:00.
   */
  private static final long EPOCH_ADJUSTMENT = new GregorianCalendar(1970, 0, 1, 0, 0, 0).getTime().getTime()
      - new GregorianCalendar(1582, 9, 15, 0, 0, 0).getTime().getTime();

  private long lastTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT;

  private short clockSequence;

  private short timeAdjustment;

  private int sleepTime = 1;

  private final char[] buffer = createBuffer();

  /**
   * A cached array of bytes representing the UUID. The second 8 bytes will be kept the same unless the clock sequence
   * has changed.
   */
  private final byte[] uuid = createUUID();

  static
  {
    byte[] index = new byte[256];
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;

    for (byte i = 0; i < BASE64_DIGITS.length; i++)
    {
      char digit = BASE64_DIGITS[i];
      if (digit < min)
      {
        min = digit;
      }

      if (digit > max)
      {
        max = digit;
      }

      index[digit] = i;
    }

    int length = max - min + 1;
    BASE64_INDEX = new byte[length];
    BASE64_INDEX_OFFSET = min;
    System.arraycopy(index, BASE64_INDEX_OFFSET, BASE64_INDEX, 0, length);
  }

  private byte[] getHardwareAddress() throws Throwable
  {
    // getHardwareAddress is a JRE 1.6 method and must be called reflectiviely
    Method method = ReflectUtil.getMethod(NetworkInterface.class, "getHardwareAddress");

    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements())
    {
      NetworkInterface networkInterface = networkInterfaces.nextElement();

      try
      {
        byte[] nodeAddress = (byte[])ReflectUtil.invokeMethod(method, networkInterface);
        if (nodeAddress != null && nodeAddress.length == NODE_ADDRESS_BYTES)
        {
          return nodeAddress;
        }
      }
      catch (Throwable ex)
      {
        //$FALL-THROUGH$
      }
    }

    throw new SocketException("Hardware address could not be determined");
  }

  private void setNodeAddress(byte[] nodeAddress)
  {
    // Set the most significant bit of the first octet to 1 so as to distinguish it from IEEE node addresses
    //
    nodeAddress[0] |= (byte)0x80;

    // The node identifier is already in network byte order,
    // so there is no need to do any byte order reversing.
    //
    for (int i = 0; i < NODE_ADDRESS_BYTES; ++i)
    {
      uuid[i + 10] = nodeAddress[i];
    }
  }

  /**
   * Updates the clock sequence portion of the UUID. The clock sequence portion may seem odd, but in the specification,
   * the high order byte comes before the low order byte. The variant is multiplexed into the high order octet of
   * clockseq_hi.
   */
  private void updateClockSequence()
  {
    // clockseq_hi
    uuid[8] = (byte)(clockSequence >> 8 & 0x3F | 0x80);
    // clockseq_low
    uuid[9] = (byte)(clockSequence & 0xFF);
  }

  /**
   * Updates the UUID with the current time, compensating for the fact that the clock resolution may be less than 100
   * ns. The byte array will have its first eight bytes populated with the time in the correct sequence of bytes, as per
   * the specification.
   */
  private void updateCurrentTime()
  {
    // Get the current time in milliseconds since the epoch
    // and adjust it to match the epoch required by the specification.
    //
    long currentTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT;

    if (lastTime > currentTime)
    {
      // The system clock has been rewound so the clock sequence must be incremented
      // to ensure that a duplicate UUID is not generated.
      //
      ++clockSequence;

      if (16384 == clockSequence)
      {
        clockSequence = 0;
      }

      updateClockSequence();
    }
    else if (lastTime == currentTime)
    {
      // The system time hasn't changed so add some increment of 100s of nanoseconds to guarantee uniqueness.
      //
      ++timeAdjustment;

      if (timeAdjustment > 9999)
      {
        // Wait so that the clock can catch up and the time adjustment won't overflow.
        try
        {
          Thread.sleep(sleepTime);
        }
        catch (InterruptedException exception)
        {
          // We just woke up.
        }

        timeAdjustment = 0;
        currentTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT;

        while (lastTime == currentTime)
        {
          try
          {
            ++sleepTime;
            Thread.sleep(1);
          }
          catch (InterruptedException exception)
          {
            // We just woke up.
          }
          currentTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT;
        }
      }
    }
    else
    {
      timeAdjustment = 0;
    }

    lastTime = currentTime;

    // Since the granularity of time in Java is only milliseconds,
    // add an adjustment so that the time is represented in 100s of nanoseconds.
    // The version number (1) is multiplexed into the most significant hex digit.
    //
    currentTime *= 10000;
    currentTime += timeAdjustment;
    currentTime |= 0x1000000000000000L;

    // Place the time into the byte array in network byte order.
    //
    for (int i = 0; i < 4; ++i)
    {
      // time_low
      //
      uuid[i] = (byte)(currentTime >> 8 * (3 - i) & 0xFFL);
    }

    for (int i = 0; i < 2; ++i)
    {
      // time_mid
      //
      uuid[i + 4] = (byte)(currentTime >> 8 * (1 - i) + 32 & 0xFFL);
    }

    for (int i = 0; i < 2; ++i)
    {
      // time_hi
      //
      uuid[i + 6] = (byte)(currentTime >> 8 * (1 - i) + 48 & 0xFFL);
    }
  }

  private void encode(byte[] uuid, char[] buffer)
  {
    for (int i = 0; i < 5; ++i)
    {
      buffer[4 * i + 1] = BASE64_DIGITS[uuid[i * 3] >> 2 & 0x3F];
      buffer[4 * i + 2] = BASE64_DIGITS[uuid[i * 3] << 4 & 0x30 | uuid[i * 3 + 1] >> 4 & 0xF];
      buffer[4 * i + 3] = BASE64_DIGITS[uuid[i * 3 + 1] << 2 & 0x3C | uuid[i * 3 + 2] >> 6 & 0x3];
      buffer[4 * i + 4] = BASE64_DIGITS[uuid[i * 3 + 2] & 0x3F];
    }

    // Handle the last byte at the end.
    //
    buffer[21] = BASE64_DIGITS[uuid[15] >> 2 & 0x3F];
    buffer[22] = BASE64_DIGITS[uuid[15] << 4 & 0x30];
  }

  private byte[] createUUID()
  {
    return new byte[16];
  }

  private char[] createBuffer()
  {
    char[] buffer = new char[23];
    buffer[0] = '_';
    return buffer;
  }
}

Back to the top