将UUID作为Base64字符串存储

94

我一直在尝试将UUID作为数据库键使用。我希望尽可能少地占用字节,同时保持UUID表示可读性。

我使用了base64将其减少到22个字节,并删除了一些似乎对于我的用途没有必要存储的尾随“==”。这种方法是否有任何缺陷?

基本上,我的测试代码进行了一系列的转换,将UUID缩小到22个字节的字符串,然后将其转换回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;
    }

}

输出:

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

一个角度来看,UUID 是 128 个随机位,每个 base64 项有 6 个位,所以是 128/6=21.3,因此你说需要 22 个 base64 位置来存储相同的数据是正确的。 - Stijn Sanders
1
你之前的问题似乎本质上是一样的:https://dev59.com/gUfRa4cB1Zd3GeqP70pX - erickson
1
我不确定你的代码在 asByteBuffer 的第二个 for 循环中是否正确。你从 7 中减去了 i,但是 i 的迭代范围是从 8 到 16,这意味着它将会以负数进行移位。如果我没记错的话,<<< 会循环,但它似乎仍然不正确。 - Jon Tirsen
1
我认为更容易的方法是使用ByteBuffer将这两个long转换为字节数组,就像这个问题中所示:https://dev59.com/Wmw15IYBdhLWcg3wA3GT - Jon Tirsen
“人类可读性”有什么意义?看看mysql/mariadb函数uuid_short()的作用。 - theking2
URLs和分享它们 - mainstringargs
11个回答

1

很惊讶没有人提到 commons-lang3 的 uuidToByteArray(…)

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

And then the code will be

import org.apache.commons.lang3.Conversion;
import java.util.*;


public static byte[] asByteArray(UUID uuid) {
    return Conversion.uuidToByteArray(uuid, new byte[16], 0, 16);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接