Merge pull request #15758 from sIvanovKonstantyn/master
BAEL-6716 - Storing UUID as Base64 String in Java
This commit is contained in:
commit
aefb1e6cbc
|
@ -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>
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
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 DecodeUUIDStringFromBase64UnitTest {
|
||||
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
|
||||
|
||||
@Test
|
||||
public void shouldDecodeUUIDUsingByteArrayAndBase64Decoder() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] decodedBytes = Base64.getDecoder()
|
||||
.decode(expectedEncodedString);
|
||||
UUID uuid = convertToUUID(decodedBytes);
|
||||
assertEquals(originalUUID, uuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeUUIDUsingByteBufferAndBase64UrlDecoder() {
|
||||
String expectedEncodedString = "zF-T94zxSlGDxudAMToMbA";
|
||||
byte[] decodedBytes = Base64.getUrlDecoder()
|
||||
.decode(expectedEncodedString);
|
||||
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() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] decodedBytes = decodeBase64(expectedEncodedString);
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
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 EncodeUUIDToBase64StringUnitTest {
|
||||
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
|
||||
|
||||
@Test
|
||||
public void shouldEncodeUUIDUsingByteArrayAndBase64Encoder() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] uuidBytes = convertToByteArray(originalUUID);
|
||||
String encodedUUID = Base64.getEncoder().withoutPadding()
|
||||
.encodeToString(uuidBytes);
|
||||
assertEquals(expectedEncodedString, encodedUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeUUIDUsingByteBufferAndBase64UrlEncoder() {
|
||||
String expectedEncodedString = "zF-T94zxSlGDxudAMToMbA";
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
|
||||
byteBuffer.putLong(originalUUID.getMostSignificantBits());
|
||||
byteBuffer.putLong(originalUUID.getLeastSignificantBits());
|
||||
String encodedUUID = Base64.getUrlEncoder().withoutPadding()
|
||||
.encodeToString(byteBuffer.array());
|
||||
assertEquals(expectedEncodedString, encodedUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeUUIDUsingApacheUtils() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] bytes = Conversion.uuidToByteArray(originalUUID, new byte[16], 0, 16);
|
||||
String encodedUUID = encodeBase64URLSafeString(bytes);
|
||||
assertEquals(expectedEncodedString, 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));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue