tinguo002

           

          java生成uuid(轉載)

          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實例 
               *  
               * 
          @return UniqId 
               
          */
            
              
          public static UniqId getInstance() {  
                  
          return me;  
              }
            
            
              
          /** 
               * 獲得不會重復的毫秒數 
               *  
               * 
          @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());  
              }
            
            
              
          /** 
               * 對字符串進行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();  
                  }
            
              }
            
            
              
          /** 
               * 對二進制數據進行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();  
                  }
            
              }
            
            
              
          /** 
               * 對字符串進行md5 string 
               *  
               * 
          @param str 
               * 
          @return md5 string 
               
          */
            
              
          public String hashString(String str) {  
                  
          byte[] bt = hash(str);  
                  
          return bytes2string(bt);  
              }
            
            
              
          /** 
               * 對字節流進行md5 string 
               *  
               * 
          @param str 
               * 
          @return md5 string 
               
          */
            
              
          public String hashBytes(byte[] str) {  
                  
          byte[] bt = hash(str);  
                  
          return bytes2string(bt);  
              }
            
            
              
          /** 
               * 將一個字節數組轉化為可見的字符串 
               *  
               * 
          @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);  
              }
            
            
              
          /** 
               * 將字符串轉換為bytes 
               *  
               * 
          @param str 
               * 
          @return byte[] 
               
          */
            
              
          public byte[] string2bytes(String str) {  
                  
          if (null == str) {  
                      
          throw new NullPointerException("參數不能為空");  
                  }
            
                  
          if (str.length() != 32{  
                      
          throw new IllegalArgumentException("字符串長度必須是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;  
              }
            
            
              
          /** 
               * 實現不重復的時間 
               *  
               * 
          @author dogun 
               
          */
            
              
          private static class UniqTimer {  
                  
          private AtomicLong lastTime = new AtomicLong(System.currentTimeMillis());  
            
                  
          public long getCurrentTime() {  
                      
          return this.lastTime.incrementAndGet();  
                  }
            
              }
            
          }
            



          歡迎大家訪問我的個人網站 萌萌的IT人

          posted on 2014-04-16 09:07 一堣而安 閱讀(569) 評論(0)  編輯  收藏 所屬分類: java

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 伊春市| 鄂托克旗| 贵溪市| 乌兰察布市| 昆山市| 盘锦市| 琼结县| 同心县| 瑞安市| 南城县| 靖边县| 乌苏市| 永泰县| 横峰县| 嘉善县| 绍兴县| 商都县| 威海市| 梁平县| 政和县| 扶绥县| 金阳县| 黑水县| 通许县| 安福县| 苏尼特左旗| 台南县| 大方县| 阿拉善左旗| 威海市| 景泰县| 乐至县| 肥乡县| 嘉荫县| 昭觉县| 嘉黎县| 衡水市| 洪湖市| 洪雅县| 昌黎县| 桓台县|