From 2a84a6fd22dbfde96ac475be47aee30a3020ed77 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Thu, 27 Jun 2019 10:26:30 -0400 Subject: [PATCH] ARTEMIS-2390 Small improvement on UUID Conversion --- .../activemq/artemis/utils/ByteUtil.java | 24 ++++++++++++++++ .../apache/activemq/artemis/utils/UUID.java | 7 ++++- .../activemq/artemis/utils/UUIDGenerator.java | 11 +------- .../activemq/artemis/utils/ByteUtilTest.java | 28 +++++++++++++++++++ 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/ByteUtil.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/ByteUtil.java index 88c6d49583..f34ce1a2cd 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/ByteUtil.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/ByteUtil.java @@ -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++) { diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUID.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUID.java index c3f99a423b..7d8e984c0d 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUID.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUID.java @@ -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; } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java index 3d4f10ecab..64475810cd 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java @@ -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() { diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/ByteUtilTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/ByteUtilTest.java index f13e153d0e..929ccf8947 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/ByteUtilTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/ByteUtilTest.java @@ -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); + } + + }