<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tinguo002

     

    java生成uuid(轉(zhuǎn)載)

    public class UniqId {  
        
    private static char[] digits = '0''1''2''3''4''5''6''7',  
                
    '8''9''a''b''c''d''e''f' }
    ;  
      
        
    private static Map<Character, Integer> rDigits = new HashMap<Character, Integer>(  
                
    16);  
        
    static {  
            
    for (int i = 0; i < digits.length; ++i) {  
                rDigits.put(digits[i], i);  
            }
      
        }
      
      
        
    private static UniqId me = new UniqId();  
        
    private String hostAddr;  
        
    private Random random = new SecureRandom();  
        
    private MessageDigest mHasher;  
        
    private UniqTimer timer = new UniqTimer();  
      
        
    private ReentrantLock opLock = new ReentrantLock();  
      
        
    private UniqId() {  
            
    try {  
                InetAddress addr 
    = InetAddress.getLocalHost();  
      
                hostAddr 
    = addr.getHostAddress();  
            }
     catch (IOException e) {  
                hostAddr 
    = String.valueOf(System.currentTimeMillis());  
            }
      
      
            
    if (hostAddr == null || hostAddr.length() == 0  
                    
    || "127.0.0.1".equals(hostAddr)) {  
                hostAddr 
    = String.valueOf(System.currentTimeMillis());  
            }
      
      
            
    try {  
                mHasher 
    = MessageDigest.getInstance("MD5");  
            }
     catch (NoSuchAlgorithmException nex) {  
                mHasher 
    = null;  
            }
      
        }
      
      
        
    /** 
         * 獲取UniqID實(shí)例 
         *  
         * 
    @return UniqId 
         
    */
      
        
    public static UniqId getInstance() {  
            
    return me;  
        }
      
      
        
    /** 
         * 獲得不會(huì)重復(fù)的毫秒數(shù) 
         *  
         * 
    @return 
         
    */
      
        
    public long getUniqTime() {  
            
    return timer.getCurrentTime();  
        }
      
      
        
    /** 
         * 獲得UniqId 
         *  
         * 
    @return uniqTime-randomNum-hostAddr-threadId 
         
    */
      
        
    public String getUniqID() {  
            StringBuffer sb 
    = new StringBuffer();  
            
    long t = timer.getCurrentTime();  
      
            sb.append(t);  
      
            sb.append(
    "-");  
      
            sb.append(random.nextInt(
    8999+ 1000);  
      
            sb.append(
    "-");  
            sb.append(hostAddr);  
      
            sb.append(
    "-");  
            sb.append(Thread.currentThread().hashCode());  
      
            
    return sb.toString();  
        }
      
      
        
    /** 
         * 獲取MD5之后的uniqId string 
         *  
         * 
    @return uniqId md5 string 
         
    */
      
        
    public String getUniqIDHashString() {  
            
    return hashString(getUniqID());  
        }
      
      
        
    /** 
         * 獲取MD5之后的uniqId 
         *  
         * 
    @return byte[16] 
         
    */
      
        
    public byte[] getUniqIDHash() {  
            
    return hash(getUniqID());  
        }
      
      
        
    /** 
         * 對(duì)字符串進(jìn)行md5 
         *  
         * 
    @param str 
         * 
    @return md5 byte[16] 
         
    */
      
        
    public byte[] hash(String str) {  
            opLock.lock();  
            
    try {  
                
    byte[] bt = mHasher.digest(str.getBytes("UTF-8"));  
                
    if (null == bt || bt.length != 16{  
                    
    throw new IllegalArgumentException("md5 need");  
                }
      
                
    return bt;  
            }
     catch (UnsupportedEncodingException e) {  
                
    throw new RuntimeException("unsupported utf-8 encoding", e);  
            }
     finally {  
                opLock.unlock();  
            }
      
        }
      
      
        
    /** 
         * 對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行md5 
         *  
         * 
    @param str 
         * 
    @return md5 byte[16] 
         
    */
      
        
    public byte[] hash(byte[] data) {  
            opLock.lock();  
            
    try {  
                
    byte[] bt = mHasher.digest(data);  
                
    if (null == bt || bt.length != 16{  
                    
    throw new IllegalArgumentException("md5 need");  
                }
      
                
    return bt;  
            }
     finally {  
                opLock.unlock();  
            }
      
        }
      
      
        
    /** 
         * 對(duì)字符串進(jìn)行md5 string 
         *  
         * 
    @param str 
         * 
    @return md5 string 
         
    */
      
        
    public String hashString(String str) {  
            
    byte[] bt = hash(str);  
            
    return bytes2string(bt);  
        }
      
      
        
    /** 
         * 對(duì)字節(jié)流進(jìn)行md5 string 
         *  
         * 
    @param str 
         * 
    @return md5 string 
         
    */
      
        
    public String hashBytes(byte[] str) {  
            
    byte[] bt = hash(str);  
            
    return bytes2string(bt);  
        }
      
      
        
    /** 
         * 將一個(gè)字節(jié)數(shù)組轉(zhuǎn)化為可見(jiàn)的字符串 
         *  
         * 
    @param bt 
         * 
    @return 
         
    */
      
        
    public String bytes2string(byte[] bt) {  
            
    int l = bt.length;  
      
            
    char[] out = new char[l << 1];  
      
            
    for (int i = 0, j = 0; i < l; i++{  
                out[j
    ++= digits[(0xF0 & bt[i]) >>> 4];  
                out[j
    ++= digits[0x0F & bt[i]];  
            }
      
      
            
    return new String(out);  
        }
      
      
        
    /** 
         * 將字符串轉(zhuǎn)換為bytes 
         *  
         * 
    @param str 
         * 
    @return byte[] 
         
    */
      
        
    public byte[] string2bytes(String str) {  
            
    if (null == str) {  
                
    throw new NullPointerException("參數(shù)不能為空");  
            }
      
            
    if (str.length() != 32{  
                
    throw new IllegalArgumentException("字符串長(zhǎng)度必須是32");  
            }
      
            
    byte[] data = new byte[16];  
            
    char[] chs = str.toCharArray();  
            
    for (int i = 0; i < 16++i) {  
                
    int h = rDigits.get(chs[i * 2]).intValue();  
                
    int l = rDigits.get(chs[i * 2 + 1]).intValue();  
                data[i] 
    = (byte) ((h & 0x0F<< 4 | (l & 0x0F));  
            }
      
            
    return data;  
        }
      
      
        
    /** 
         * 實(shí)現(xiàn)不重復(fù)的時(shí)間 
         *  
         * 
    @author dogun 
         
    */
      
        
    private static class UniqTimer {  
            
    private AtomicLong lastTime = new AtomicLong(System.currentTimeMillis());  
      
            
    public long getCurrentTime() {  
                
    return this.lastTime.incrementAndGet();  
            }
      
        }
      
    }
      



    歡迎大家訪問(wèn)我的個(gè)人網(wǎng)站 萌萌的IT人

    posted on 2014-04-16 09:07 一堣而安 閱讀(565) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(1)

    隨筆分類(lèi)

    隨筆檔案

    收藏夾

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: www.亚洲精品| 亚洲一区二区三区香蕉| 亚洲中文字幕在线乱码| 亚洲国产精品成人精品小说| 国产成人亚洲精品91专区高清| baoyu116.永久免费视频| 女人张开腿等男人桶免费视频 | 亚洲视频免费观看| 毛片亚洲AV无码精品国产午夜| 免费日本一区二区| 国产精品酒店视频免费看| 亚洲av无码乱码国产精品fc2 | 污视频网站免费在线观看| 69av免费观看| 亚洲人成网站色在线入口| 亚洲男人天堂影院| 一级女人18片毛片免费视频| 四虎永久在线观看免费网站网址| 国产黄色一级毛片亚洲黄片大全| 国产91在线|亚洲| 久久午夜无码免费| 免费成人午夜视频| xxx毛茸茸的亚洲| 四虎国产精品永久免费网址| jjzz亚洲亚洲女人| 亚洲天堂男人影院| 四虎影视成人永久免费观看视频| 亚洲精品一级无码鲁丝片| 亚洲最大中文字幕无码网站| 久久久久免费精品国产小说| 亚洲日本韩国在线| 综合一区自拍亚洲综合图区| 99国产精品永久免费视频| 狠狠色伊人亚洲综合成人| 西西人体大胆免费视频| 成人最新午夜免费视频| 亚洲视频在线观看视频| 久久久久久久久久国产精品免费| gogo全球高清大胆亚洲| 99亚洲男女激情在线观看| 免费看韩国黄a片在线观看|