UUID 压缩为22位

public class Generator {

    private static char[] BASE64 = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789".toCharArray();

    public static String generateUUID() {
        UUID uuid = UUID.randomUUID();
        char[] chs = new char[22];
        long most = uuid.getMostSignificantBits();
        long least = uuid.getLeastSignificantBits();
        int high = (int)((most >> 13) ^ (least >> 31)) & 0x3c;
        int k = chs.length - 1;
        for(int i = 0; i < 10; i++, least >>>= 6) {
            chs[k--] = BASE64[(int)(least & 0x3f)];
        }
        chs[k--] = BASE64[(int)((least & 0x3f) | (most & 0x30))];
        most >>>= 2;
        for(int i = 0; i < 10; i++, most >>>= 6) {
            chs[k--] = BASE64[(int)(most & 0x3f)];
        }
        chs[k--] = BASE64[(int)(high | most)];
        return new String(chs);
    }
}

 

posted @ 2015-09-15 12:10  真大闸蟹  阅读(1556)  评论(0编辑  收藏  举报