1 16 17 23 24 package org.apache.axis.components.uuid; 25 26 import java.math.BigInteger ; 27 import java.security.SecureRandom ; 28 import java.util.Random ; 29 30 39 public class SimpleUUIDGen implements UUIDGen { 40 private static final BigInteger countStart = new BigInteger ("-12219292800000"); private static final int clock_sequence = (new Random ()).nextInt(16384); 42 private static final byte ZERO = (byte) 48; private static final byte ONE = (byte) 49; private static Random secureRandom = null; 45 46 static { 47 try { 62 secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN"); 63 } catch (Exception e) { 64 secureRandom = new Random (); 65 } 66 } 67 68 77 private static final String leftZeroPadString(String bitString, int len) { 78 if (bitString.length() < len) { 79 int nbExtraZeros = len - bitString.length(); 80 StringBuffer extraZeros = new StringBuffer (); 81 for (int i = 0; i < nbExtraZeros; i++) { 82 extraZeros.append("0"); 83 } 84 extraZeros.append(bitString); 85 bitString = extraZeros.toString(); 86 } 87 return bitString; 88 } 89 90 99 public String nextUUID() { 100 103 BigInteger count; 105 106 BigInteger current = BigInteger.valueOf(System.currentTimeMillis()); 108 109 BigInteger countMillis = current.subtract(countStart); 111 112 count = countMillis.multiply(BigInteger.valueOf(10000)); 114 byte[] bits = leftZeroPadString(count.toString(2), 60).getBytes(); 115 116 byte[] time_low = new byte[32]; 118 for (int i = 0; i < 32; i++) 119 time_low[i] = bits[bits.length - i - 1]; 120 121 byte[] time_mid = new byte[16]; 123 for (int i = 0; i < 16; i++) 124 time_mid[i] = bits[bits.length - 32 - i - 1]; 125 126 byte[] time_hi_and_version = new byte[16]; 128 for (int i = 0; i < 12; i++) 129 time_hi_and_version[i] = bits[bits.length - 48 - i - 1]; 130 131 time_hi_and_version[12] = ONE; 132 time_hi_and_version[13] = ZERO; 133 time_hi_and_version[14] = ZERO; 134 time_hi_and_version[15] = ZERO; 135 136 BigInteger clockSequence = BigInteger.valueOf(clock_sequence); 138 byte[] clock_bits = leftZeroPadString(clockSequence.toString(2), 14).getBytes(); 139 byte[] clock_seq_low = new byte[8]; 140 for (int i = 0; i < 8; i++) { 141 clock_seq_low[i] = clock_bits[clock_bits.length - i - 1]; 142 } 143 144 byte[] clock_seq_hi_and_reserved = new byte[8]; 146 for (int i = 0; i < 6; i++) 147 clock_seq_hi_and_reserved[i] = clock_bits[clock_bits.length - 8 - i - 1]; 148 149 clock_seq_hi_and_reserved[6] = ZERO; 150 clock_seq_hi_and_reserved[7] = ONE; 151 152 String timeLow = Long.toHexString((new BigInteger (new String (reverseArray(time_low)), 2)).longValue()); 153 timeLow = leftZeroPadString(timeLow, 8); 154 155 String timeMid = Long.toHexString((new BigInteger (new String (reverseArray(time_mid)), 2)).longValue()); 156 timeMid = leftZeroPadString(timeMid, 4); 157 158 String timeHiAndVersion = Long.toHexString((new BigInteger (new String (reverseArray(time_hi_and_version)), 2)).longValue()); 159 timeHiAndVersion = leftZeroPadString(timeHiAndVersion, 4); 160 161 String clockSeqHiAndReserved = Long.toHexString((new BigInteger (new String (reverseArray(clock_seq_hi_and_reserved)), 2)).longValue()); 162 clockSeqHiAndReserved = leftZeroPadString(clockSeqHiAndReserved, 2); 163 164 String clockSeqLow = Long.toHexString((new BigInteger (new String (reverseArray(clock_seq_low)), 2)).longValue()); 165 clockSeqLow = leftZeroPadString(clockSeqLow, 2); 166 167 long nodeValue = secureRandom.nextLong(); 168 nodeValue = Math.abs(nodeValue); 169 while (nodeValue > 140737488355328L) { 170 nodeValue = secureRandom.nextLong(); 171 nodeValue = Math.abs(nodeValue); 172 } 173 174 BigInteger nodeInt = BigInteger.valueOf(nodeValue); 175 176 byte[] node_bits = leftZeroPadString(nodeInt.toString(2), 47).getBytes(); 177 byte[] node = new byte[48]; 178 for (int i = 0; i < 47; i++) 179 node[i] = node_bits[node_bits.length - i - 1]; 180 181 node[47] = ONE; 182 String theNode = Long.toHexString((new BigInteger (new String (reverseArray(node)), 2)).longValue()); 183 theNode = leftZeroPadString(theNode, 12); 184 185 StringBuffer result = new StringBuffer (timeLow); 186 result.append("-"); 187 result.append(timeMid); 188 result.append("-"); 189 result.append(timeHiAndVersion); 190 result.append("-"); 191 result.append(clockSeqHiAndReserved); 192 result.append(clockSeqLow); 193 result.append("-"); 194 result.append(theNode); 195 return result.toString().toUpperCase(); 196 } 197 198 private static byte[] reverseArray(byte[] bits) { 199 byte[] result = new byte[bits.length]; 200 for (int i = 0; i < result.length; i++) 201 result[i] = bits[result.length - 1 - i]; 202 203 return result; 204 } 205 } 206 | Popular Tags |