?? uidgenerator.java
字號:
/*** * jwma Java WebMail * Copyright (c) 2000-2003 jwma team * * jwma is free software; you can distribute and use this source * under the terms of the BSD-style license received along with * the distribution. ***/package com.struts2.framework.util;import java.rmi.server.UID;import java.net.URL;import java.io.DataInputStream;import java.util.Random;/** * Utility class exposing a method that will return * a unique identifier. * * @author Dieter Wimberger * @version 0.9.7 07/02/2003 */public class UIDGenerator { //class attributes private static Random m_Random; private static int m_ReseedCounter = 0; //create seeded random static { m_Random = new Random(); seedRandom(); } /** * Returns a UID (unique identifier) as <tt>String</tt>. * The identifier represents the MD5 hashed combination * of a <tt>java.rmi.server.UID</tt> instance, a random padding of * <tt>RANDOM_PADDING</tt> length, it's identity hashcode and * <tt>System.currentTimeMillis()</tt>. * * @return the UID as <tt>String</tt>. */ public static final synchronized String getUID() { byte[] buffer = new byte[RANDOM_PADDING]; String u = new UID().toString(); int i = System.identityHashCode(u); long d = System.currentTimeMillis(); //create random padding m_Random.nextBytes(buffer); u = u + new String(buffer); if (m_ReseedCounter == RANDOM_RESEED) { seedRandom(); m_ReseedCounter = 0; } else { m_ReseedCounter++; } return MD5.hash(u + i + d); }//getUID /** * If the <tt>HotBits</tt> Server is available, <tt>Random</tt> * will be seeded with a real random long. * <p> * <a href="http://www.fourmilab.ch/hotbits/">HotBits</a> is located * at Fermilab, Switzerland. * */ public static final void seedRandom() { try { URL url = new URL(HOTBITS_URL); DataInputStream din = new DataInputStream(url.openStream()); m_Random.setSeed(din.readLong()); din.close(); } catch (Exception ex) { //use what is available m_Random.setSeed(System.currentTimeMillis()); } }//seedRandom public static final void main(String[] args) { int i = 0; long start = System.currentTimeMillis(); while (i < 1000) { System.out.println(getUID()); i++; } long stop = System.currentTimeMillis(); System.out.println("Time =" + (stop - start) + "[ms]"); }//main public static final int RANDOM_PADDING = 256; public static final int RANDOM_SEED_LENGTH = 6; public static final int RANDOM_RESEED = 1000; public static final String HOTBITS_URL = "http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=" + RANDOM_SEED_LENGTH + "&fmt=bin";}//UIDGenerator
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -