ARTEMIS-2390 Small improvement on UUID Conversion

This commit is contained in:
Clebert Suconic 2019-06-27 10:26:30 -04:00
parent c66d62e4b0
commit 2a84a6fd22
4 changed files with 59 additions and 11 deletions

View File

@ -163,6 +163,30 @@ public class ByteUtil {
| ((int) b[0] & 0xff) << 24;
}
public static byte[] longToBytes(long value) {
byte[] output = new byte[8];
longToBytes(value, output, 0);
return output;
}
public static void longToBytes(long x, byte[] output, int offset) {
output[offset] = (byte)(x >> 56);
output[offset + 1] = (byte)(x >> 48);
output[offset + 2] = (byte)(x >> 40);
output[offset + 3] = (byte)(x >> 32);
output[offset + 4] = (byte)(x >> 24);
output[offset + 5] = (byte)(x >> 16);
output[offset + 6] = (byte)(x >> 8);
output[offset + 7] = (byte)(x);
}
public static byte[] doubleLongToBytes(long value1, long value2) {
byte[] output = new byte[16];
longToBytes(value1, output, 0);
longToBytes(value2, output, 8);
return output;
}
public static byte[] hexToBytes(String hexStr) {
byte[] bytes = new byte[hexStr.length() / 2];
for (int i = 0; i < bytes.length; i++) {

View File

@ -107,10 +107,15 @@ public final class UUID {
mId[UUID.INDEX_VARIATION] |= (byte) 0x80;
}
public UUID(final byte[] data) {
private UUID(final byte[] data) {
mId = data;
}
/** This is for conversions between two types of UUID */
public UUID(java.util.UUID uuid) {
this(ByteUtil.doubleLongToBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()));
}
public byte[] asBytes() {
return mId;
}

View File

@ -18,7 +18,6 @@ package org.apache.activemq.artemis.utils;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
@ -111,15 +110,7 @@ public final class UUIDGenerator {
}
public UUID fromJavaUUID(java.util.UUID uuid) {
long msb = uuid.getMostSignificantBits();
long lsb = uuid.getLeastSignificantBits();
ByteBuffer buffer = ByteBuffer.allocate(16);
buffer.putLong(msb);
buffer.putLong(lsb);
byte[] contents = buffer.array();
return new UUID(contents);
return new UUID(uuid);
}
public byte[] generateDummyAddress() {

View File

@ -318,4 +318,32 @@ public class ByteUtilTest {
assertEquals(randomInt, ByteUtil.bytesToInt(actual));
}
}
@Test
public void testLongToBytes() {
ByteBuffer buffer = ByteBuffer.allocate(8);
long randomLong = RandomUtil.randomLong();
buffer.putLong(randomLong);
byte[] longArrayAssert = buffer.array();
byte[] convertedArray = ByteUtil.longToBytes(randomLong);
assertArrayEquals(longArrayAssert, convertedArray);
}
@Test
public void testDoubleLongToBytes() {
long randomLong1 = RandomUtil.randomLong();
long randomLong2 = RandomUtil.randomLong();
ByteBuffer buffer = ByteBuffer.allocate(16);
buffer.putLong(randomLong1);
buffer.putLong(randomLong2);
byte[] assertContent = buffer.array();
byte[] convertedContent = ByteUtil.doubleLongToBytes(randomLong1, randomLong2);
assertArrayEquals(assertContent, convertedContent);
}
}