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个字节
本文来源:http://blog.csdn.net/feijianxia/archive/2007/08/27/1760583.aspx