Skip to content

Commit

Permalink
ARTEMIS-2390 Small improvement on UUID Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
clebertsuconic committed Jun 27, 2019
1 parent c66d62e commit 2a84a6f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
Expand Up @@ -163,6 +163,30 @@ public static int bytesToInt(byte[] b) {
| ((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++) {
Expand Down
Expand Up @@ -107,10 +107,15 @@ public UUID(final int type, final byte[] data) {
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;
}
Expand Down
Expand Up @@ -18,7 +18,6 @@

import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -111,15 +110,7 @@ public UUID generateTimeBasedUUID(final byte[] byteAddr) {
}

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() {
Expand Down
Expand Up @@ -318,4 +318,32 @@ public void testIntToByte() {
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);
}


}

0 comments on commit 2a84a6f

Please sign in to comment.