BAEL-6716 - Storing UUID as Base64 String in Java

This commit is contained in:
ICKostiantyn.Ivanov 2024-01-28 10:05:13 +01:00
parent d5eaf1d6cc
commit 489e932e38
3 changed files with 130 additions and 0 deletions

View File

@ -29,6 +29,16 @@
<artifactId>tsid-creator</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,61 @@
package com.baeldung.uuid;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
import org.apache.commons.lang3.Conversion;
import org.junit.jupiter.api.Test;
public class DecodeUUIDStringFromBase64Test {
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
@Test
public void shouldDecodeUUIDUsingByteArrayAndBase64Decoder() {
byte[] decodedBytes = Base64.getDecoder()
.decode("UUrxjPeTX8xsDDoxQOfGgw");
UUID uuid = convertToUUID(decodedBytes);
assertEquals(originalUUID, uuid);
}
@Test
public void shouldDecodeUUIDUsingByteBufferAndBase64UrlDecoder() {
byte[] decodedBytes = Base64.getUrlDecoder()
.decode("zF-T94zxSlGDxudAMToMbA");
ByteBuffer byteBuffer = ByteBuffer.wrap(decodedBytes);
long mostSignificantBits = byteBuffer.getLong();
long leastSignificantBits = byteBuffer.getLong();
UUID uuid = new UUID(mostSignificantBits, leastSignificantBits);
assertEquals(originalUUID, uuid);
}
@Test
public void shouldDecodeUUIDUsingApacheUtils() {
byte[] decodedBytes = decodeBase64("UUrxjPeTX8xsDDoxQOfGgw");
UUID uuid = Conversion.byteArrayToUuid(decodedBytes, 0);
assertEquals(originalUUID, uuid);
}
private UUID convertToUUID(byte[] src) {
long mostSignificantBits = convertBytesToLong(src, 0);
long leastSignificantBits = convertBytesToLong(src, 8);
return new UUID(mostSignificantBits, leastSignificantBits);
}
private long convertBytesToLong(byte[] uuidBytes, int start) {
long result = 0;
for(int i = 0; i < 8; i++) {
int shift = i * 8;
long bits = (255L & (long)uuidBytes[i + start]) << shift;
long mask = 255L << shift;
result = result & ~mask | bits;
}
return result;
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.uuid;
import static org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
import org.apache.commons.lang3.Conversion;
import org.junit.jupiter.api.Test;
public class EncodeUUIDToBase64StringTest {
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
@Test
public void shouldEncodeUUIDUsingByteArrayAndBase64Encoder() {
byte[] uuidBytes = convertToByteArray(originalUUID);
String encodedUUID = Base64.getEncoder().withoutPadding()
.encodeToString(uuidBytes);
assertEquals("UUrxjPeTX8xsDDoxQOfGgw", encodedUUID);
}
@Test
public void shouldEncodeUUIDUsingByteBufferAndBase64UrlEncoder() {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(originalUUID.getMostSignificantBits());
byteBuffer.putLong(originalUUID.getLeastSignificantBits());
String encodedUUID = Base64.getUrlEncoder().withoutPadding()
.encodeToString(byteBuffer.array());
assertEquals("zF-T94zxSlGDxudAMToMbA", encodedUUID);
}
@Test
public void shouldEncodeUUIDUsingApacheUtils() {
byte[] bytes = Conversion.uuidToByteArray(originalUUID, new byte[16], 0, 16);
String encodedUUID = encodeBase64URLSafeString(bytes);
assertEquals("UUrxjPeTX8xsDDoxQOfGgw", encodedUUID);
}
private byte[] convertToByteArray(UUID uuid) {
byte[] result = new byte[16];
long mostSignificantBits = uuid.getMostSignificantBits();
fillByteArray(0, 8, result, mostSignificantBits);
long leastSignificantBits = uuid.getLeastSignificantBits();
fillByteArray(8, 16, result, leastSignificantBits);
return result;
}
private static void fillByteArray(int start, int end, byte[] result, long bits) {
for (int i = start; i < end; i++) {
int shift = i * 8;
result[i] = (byte) ((int) (255L & bits >> shift));
}
}
}