GUID和UUID
1.關於GUID
下面是一個生成GUID的Java程式碼:
package com.util;
import java.net.*;
import java.util.*;
import java.security.*;
public class RandomGUID extends Object {
public String valueBeforeMD5 = "";
public String valueAfterMD5 = "";
private static Random myRand;
private static SecureRandom mySecureRand;
private static String s_id;
static {
mySecureRand = new SecureRandom();
long secureInitializer = mySecureRand.nextLong();
myRand = new Random(secureInitializer);
try {
s_id = InetAddress.getLocalHost().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public RandomGUID() {
getRandomGUID(false);
}
public RandomGUID(boolean secure) {
getRandomGUID(secure);
}
private void getRandomGUID(boolean secure) {
MessageDigest md5 = null;
StringBuffer sbValueBeforeMD5 = new StringBuffer();
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
System.out.println("Error: " + e);
}
try {
long time = System.currentTimeMillis();
long rand = 0;
if (secure) {
rand = mySecureRand.nextLong();
} else {
rand = myRand.nextLong();
}
sbValueBeforeMD5.append(s_id);
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(time));
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(rand));
valueBeforeMD5 = sbValueBeforeMD5.toString();
md5.update(valueBeforeMD5.getBytes());
byte[] array = md5.digest();
StringBuffer sb = new StringBuffer();
for (int j = 0; j < array.length; ++j) {
int b = array[j] & 0xFF;
if (b < 0x10) sb.append('0');
sb.append(Integer.toHexString(b));
}
valueAfterMD5 = sb.toString();
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
public String toString() {
String raw = valueAfterMD5.toUpperCase();
StringBuffer sb = new StringBuffer();
sb.append(raw.substring(0, 8));
sb.append("-");
sb.append(raw.substring(8, 12));
sb.append("-");
sb.append(raw.substring(12, 16));
sb.append("-");
sb.append(raw.substring(16, 20));
sb.append("-");
sb.append(raw.substring(20));
return sb.toString();
}
public static String getInatance(){
RandomGUID myGUID = new RandomGUID();
return myGUID.toString();
}
public static void main(String args[]) {
for (int i=0; i< 100; i++) {
RandomGUID myGUID = new RandomGUID();
System.out.println("Seeding String=" + myGUID.valueBeforeMD5);
System.out.println("rawGUID=" + myGUID.valueAfterMD5);
System.out.println("RandomGUID=" + myGUID.toString());
System.out.println("GUID size=" + myGUID.toString().length());
}
}
}
使用方法:
public String getID(){
return RandomGUID.getInatance();
}
在需要的地方,呼叫getID()方法即可.
2.關於UUID
jdk5.0中引入的UUID
UUID(Universally Unique Identifier)全域性唯一識別符號,是指在一臺機器上生成的數字,它保證對在同一時空中的所有機器都是唯一的。按照開放軟體基金會(OSF)制定的標準計算,用到了乙太網卡地址、納秒級時間、晶片ID碼和許多可能的數字。由以下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關,如果你在生成一個UUID之後,過幾秒又生成一個UUID,則第一個部分不同,其餘相同),時鐘序列,全域性唯一的IEEE機器識別號(如果有網絡卡,從網絡卡獲得,沒有網絡卡以其他方式獲得),UUID的唯一缺陷在於生成的結果串會比較長。
在Java中生成UUID:
如果使用的JDK1.5的話,那麼生成UUID變成了一件簡單的事,以為JDK實現了UUID: java.util.UUID,直接呼叫即可.
UUID uuid = UUID.randomUUID();
第三方開源類庫(推薦使用):
最著名的是 JUG .特點上是: 純Java實現,開源,LGPL協議。採用了Native的方式產生真正的Uuid.而且提供了不同平臺的實現,包括:
Linux / x86
Windows (98, ME, NT, 2K, XP?) / x86
Solaris / Sparc
Mac OS X
FreeBSD / x86
import org.doomdark.uuid.UUID;
import org.doomdark.uuid.UUIDGenerator;
UUIDGenerator generator = UUIDGenerator.getInstance();
UUID uuid = generator.generateRandomBasedUUID();
在IO中,利用socket通訊,應該如何將UUID轉換成整型資料當成資料輸出流呢。
很簡單,想不到那可就麻煩咯~~~
out.writeLong(uuid.getMostSignificantBits());//傳送最高有效64位
out.writeLong(uuid.getLeastSignificantBits());//傳送最低有效64位
一共16個位元組