Identities增加生成随机Base62编码的字符串

This commit is contained in:
Calvin
2012-03-26 22:45:30 +08:00
parent 15abadd71d
commit ae02aab15e
4 changed files with 30 additions and 53 deletions
@@ -13,20 +13,17 @@ import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.Validate;
/**
* 封装各种格式的编码解码工具类.
*
* 1.Commons-Codec的 hex/base64 编码
* 2.自行编写的,将long进行base62编码以缩短其长度
* 3.Commons-Lang的xml/html/csv escape
* 4.JDK提供的URLEncoder
*
* @author calvin
*/
public class Encodes {
private static final String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final String DEFAULT_URL_ENCODING = "UTF-8";
@@ -72,41 +69,6 @@ public class Encodes {
return Base64.decodeBase64(input);
}
/**
* Base62(0_9A_Za_z)编码数字, long->String.
*/
public static String encodeNumberToBase62(long num) {
return alphabetEncode(num, 62);
}
/**
* Base62(0_9A_Za_z)解码数字, String->long.
*/
public static long decodeBase62ToNumber(String str) {
return alphabetDecode(str, 62);
}
private static String alphabetEncode(long num, int base) {
num = Math.abs(num);
StringBuilder sb = new StringBuilder();
for (; num > 0; num /= base) {
sb.append(ALPHABET.charAt((int) (num % base)));
}
return sb.toString();
}
private static long alphabetDecode(String str, int base) {
Validate.notBlank(str);
long result = 0;
for (int i = 0; i < str.length(); i++) {
result += ALPHABET.indexOf(str.charAt(i)) * Math.pow(base, i);
}
return result;
}
/**
* Html 转码.
*/
@@ -17,6 +17,9 @@ public class Identities {
private static SecureRandom random = new SecureRandom();
private static final char[] ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
.toCharArray();
private Identities() {
}
@@ -38,13 +41,20 @@ public class Identities {
* 使用SecureRandom随机生成Long.
*/
public static long randomLong() {
return random.nextLong();
return Math.abs(random.nextLong());
}
/**
* 基于Base62编码的SecureRandom随机生成Long.
* 基于Base62编码的SecureRandom随机生成bytes.
*/
public static String randomBase62() {
return Encodes.encodeNumberToBase62(random.nextLong());
public static String randomBase62(int length) {
byte[] randomBytes = new byte[length];
random.nextBytes(randomBytes);
char[] chars = new char[length];
for (int i = 0; i < length; i++) {
chars[i] = ALPHABET[((randomBytes[i] & 0xFF) % ALPHABET.length)];
}
return new String(chars);
}
}
@@ -1,9 +1,8 @@
package org.springside.modules.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springside.modules.utils.Encodes;
public class EncodesTest {
@@ -28,15 +27,6 @@ public class EncodesTest {
assertEquals(input, new String(Encodes.decodeBase64(result)));
}
@Test
public void base62Encode() {
long num = 63;
String result = Encodes.encodeNumberToBase62(num);
assertEquals("11", result);
assertEquals(num, Encodes.decodeBase62ToNumber(result));
}
@Test
public void urlEncode() {
String input = "http://locahost/?q=中文&t=1";
@@ -0,0 +1,15 @@
package org.springside.modules.utils;
import org.junit.Test;
public class IdentitiesTest {
@Test
public void demo() {
System.out.println("uuid: " + Identities.uuid());
System.out.println("uuid2:" + Identities.uuid2());
System.out.println("randomLong: " + Identities.randomLong());
System.out.println("randomBase62:" + Identities.randomBase62(6));
}
}