存储UUID为base64字符串 [英] Storing UUID as base64 String
问题描述
我一直在尝试使用UUID数据库键。我想占用的字节尽可能最少的,同时仍保持UUID重新presentation人类可读的。
我认为我已经得到它归结为使用Base64并删除一些尾随==,似乎是不必要的存储为我的目的22个字节。是否有使用这种方法什么破绽?
基本上我的测试code不一堆转换,以获得UUID下降到22字节的字符串,然后将其转换回一个UUID。
进口java.io.IOException异常;
进口java.util.UUID中;公共类UUIDTest { 公共静态无效的主要(字串[] args){
UUID UUID = UUID.randomUUID();
的System.out.println(UUID字符串:+根据UUID.toString());
的System.out.println(字节数:+根据UUID.toString()的getBytes()的长度。);
的System.out.println(); 字节[] = uuidArr asByteArray(UUID);
System.out.print(UUID字节数组:);
对于(BYTE B:uuidArr){
System.out.print(B +);
}
的System.out.println();
的System.out.println(字节数:+ uuidArr.length);
的System.out.println();
尝试{
//将一个字节数组为base64字符串
字符串s =新sun.misc.BASE64En codeR()带code(uuidArr)。
的System.out.println(UUID Base64编码字符串:+ S);
的System.out.println(字节数:+ s.getBytes()长);
的System.out.println();
串修整= s.split(=)[0];
的System.out.println(UUID的Base64字符串修剪:+修剪);
的System.out.println(字节数:+ trimmed.getBytes()的长度。);
的System.out.println(); // base64转换字符串为一个字节数组
字节[] = backArr新sun.misc.BASE64De codeR()德codeBuffer(修剪)。
System.out.print(回到UUID字节数组:);
对于(BYTE B:backArr){
System.out.print(B +);
}
的System.out.println();
的System.out.println(字节数:+ backArr.length); 字节[] = fixedArr新的字节[16];
的for(int i = 0; I< 16;我++){
fixedArr [I] = backArr [I]
}
的System.out.println();
System.out.print(固定UUID字节数组:);
对于(BYTE B:fixedArr){
System.out.print(B +);
}
的System.out.println();
的System.out.println(字节数:+ fixedArr.length); 的System.out.println();
UUID newUUID = toUUID(fixedArr);
的System.out.println(UUID字符串:+ newUUID.toString());
的System.out.println(字节数:+ newUUID.toString()的getBytes()的长度。);
的System.out.println(); 的System.out.println(等于start UUID?+ newUUID.equals(UUID));
如果(!newUUID.equals(UUID)){
System.exit(0);
}
}赶上(IOException异常五){
} }
公共静态的byte [] asByteArray(UUID UUID){ 长MSB = uuid.getMostSignificantBits();
长LSB = uuid.getLeastSignificantBits();
字节[]缓冲区=新的字节[16]; 的for(int i = 0; I< 8;我++){
缓冲液[I] =(字节)(MSB>>→8 *(7 - ⅰ));
}
的for(int i = 8; I< 16;我++){
缓冲液[I] =(字节)(LSB>>→8 *(7 - ⅰ));
} 返回缓冲区; } 公共静态UUID toUUID(字节[]的字节数组){ 长MSB = 0;
长LSB = 0;
的for(int i = 0; I< 8;我++)
MSB =(MSB<< 8)| (字节数组包括[1] - 安培; 0xFF的);
的for(int i = 8; I< 16;我++)
LSB =(LSB<< 8)| (字节数组包括[1] - 安培; 0xFF的);
UUID结果=新的UUID(MSB,LSB); 返回结果;
}}
输出:
UUID字符串:cdaed56d-8712-414d-b346-01905d0026fe
字节数:36UUID字节数组:-51 -82 -43 109 -121 18 65 77 70 -77 -112 1 0 93 38 -2
字节数:16UUID Base64编码字符串:za7VbYcSQU2zRgGQXQAm / g的==
字节数:24UUID的Base64字符串修剪:za7VbYcSQU2zRgGQXQAm / G
字节数:22返回UUID字节数组:-51 -82 -43 109 -121 18 65 77 70 -77 -112 1 0 93 38 -2 0 38
字节数:18固定UUID字节数组:-51 -82 -43 109 -121 18 65 77 70 -77 -112 1 0 93 38 -2
字节数:16UUID字符串:cdaed56d-8712-414d-b346-01905d0026fe
字节数:36等于start UUID?真正
您可以安全地滴在此应用程序的填充==。如果要取消code基64文本回字节,大多数图书馆希望它在那里,但因为你只是使用生成的字符串作为重点,这不是一个问题。
我喜欢的Base-64由于其有限的字符集看起来不那么像废话,但也有 BASE-85 。它使用多个字符,codeS 4个字节为5个字符,这样你可以让你的文字到20个字符。
I have been experimenting with using UUIDs as database keys. I want to take up the least amount of bytes as possible, while still keeping the UUID representation human readable.
I think that I have gotten it down to 22 bytes using base64 and removing some trailing "==" that seem to be unnecessary to store for my purposes. Are there any flaws with this approach?
Basically my test code does a bunch of conversions to get the UUID down to a 22 byte String, then converts it back into a UUID.
import java.io.IOException;
import java.util.UUID;
public class UUIDTest {
public static void main(String[] args){
UUID uuid = UUID.randomUUID();
System.out.println("UUID String: " + uuid.toString());
System.out.println("Number of Bytes: " + uuid.toString().getBytes().length);
System.out.println();
byte[] uuidArr = asByteArray(uuid);
System.out.print("UUID Byte Array: ");
for(byte b: uuidArr){
System.out.print(b +" ");
}
System.out.println();
System.out.println("Number of Bytes: " + uuidArr.length);
System.out.println();
try {
// Convert a byte array to base64 string
String s = new sun.misc.BASE64Encoder().encode(uuidArr);
System.out.println("UUID Base64 String: " +s);
System.out.println("Number of Bytes: " + s.getBytes().length);
System.out.println();
String trimmed = s.split("=")[0];
System.out.println("UUID Base64 String Trimmed: " +trimmed);
System.out.println("Number of Bytes: " + trimmed.getBytes().length);
System.out.println();
// Convert base64 string to a byte array
byte[] backArr = new sun.misc.BASE64Decoder().decodeBuffer(trimmed);
System.out.print("Back to UUID Byte Array: ");
for(byte b: backArr){
System.out.print(b +" ");
}
System.out.println();
System.out.println("Number of Bytes: " + backArr.length);
byte[] fixedArr = new byte[16];
for(int i= 0; i<16; i++){
fixedArr[i] = backArr[i];
}
System.out.println();
System.out.print("Fixed UUID Byte Array: ");
for(byte b: fixedArr){
System.out.print(b +" ");
}
System.out.println();
System.out.println("Number of Bytes: " + fixedArr.length);
System.out.println();
UUID newUUID = toUUID(fixedArr);
System.out.println("UUID String: " + newUUID.toString());
System.out.println("Number of Bytes: " + newUUID.toString().getBytes().length);
System.out.println();
System.out.println("Equal to Start UUID? "+newUUID.equals(uuid));
if(!newUUID.equals(uuid)){
System.exit(0);
}
} catch (IOException e) {
}
}
public static byte[] asByteArray(UUID uuid) {
long msb = uuid.getMostSignificantBits();
long lsb = uuid.getLeastSignificantBits();
byte[] buffer = new byte[16];
for (int i = 0; i < 8; i++) {
buffer[i] = (byte) (msb >>> 8 * (7 - i));
}
for (int i = 8; i < 16; i++) {
buffer[i] = (byte) (lsb >>> 8 * (7 - i));
}
return buffer;
}
public static UUID toUUID(byte[] byteArray) {
long msb = 0;
long lsb = 0;
for (int i = 0; i < 8; i++)
msb = (msb << 8) | (byteArray[i] & 0xff);
for (int i = 8; i < 16; i++)
lsb = (lsb << 8) | (byteArray[i] & 0xff);
UUID result = new UUID(msb, lsb);
return result;
}
}
output:
UUID String: cdaed56d-8712-414d-b346-01905d0026fe
Number of Bytes: 36
UUID Byte Array: -51 -82 -43 109 -121 18 65 77 -77 70 1 -112 93 0 38 -2
Number of Bytes: 16
UUID Base64 String: za7VbYcSQU2zRgGQXQAm/g==
Number of Bytes: 24
UUID Base64 String Trimmed: za7VbYcSQU2zRgGQXQAm/g
Number of Bytes: 22
Back to UUID Byte Array: -51 -82 -43 109 -121 18 65 77 -77 70 1 -112 93 0 38 -2 0 38
Number of Bytes: 18
Fixed UUID Byte Array: -51 -82 -43 109 -121 18 65 77 -77 70 1 -112 93 0 38 -2
Number of Bytes: 16
UUID String: cdaed56d-8712-414d-b346-01905d0026fe
Number of Bytes: 36
Equal to Start UUID? true
You can safely drop the padding "==" in this application. If you were to decode the base-64 text back to bytes, most libraries would expect it to be there, but since you are just using the resulting string as a key, it's not a problem.
I like Base-64 because its limited character set looks less like gibberish, but there's also Base-85. It uses more characters and codes 4 bytes as 5 characters, so you could get your text down to 20 characters.
这篇关于存储UUID为base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!