Skip to content

Commit acfc476

Browse files
authored
Merge pull request #10 from patrickfav/feat-9-uuid-handling
Add UUID Handling Feature
2 parents 2324441 + bdc5816 commit acfc476

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* add `encodeCharsetToBytes()` feature #7
66
* add new `from(char[] charArray, Charset charset)` constructor with improved logic #8
7+
* add constructor/converter from/to UUID #9
78

89
## v0.5.0
910

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ to blindly paste code snippets from
3232

3333
It's main features include:
3434

35-
* **Creation** from a wide variety of sources: multiple arrays, integers, [streams](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html), random, strings, files, ...
35+
* **Creation** from a wide variety of sources: multiple arrays, integers, [streams](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html), random, strings, files, uuid, ...
3636
* **Transformation** with many built-in: append, [xor](https://en.wikipedia.org/wiki/Exclusive_or), [and](https://en.wikipedia.org/wiki/Logical_conjunction), [hash](https://en.wikipedia.org/wiki/Cryptographic_hash_function), [shifts](https://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts), shuffle, reverse, [checksum](https://en.wikipedia.org/wiki/Checksum), ...
3737
* **Validators** with the ability to arbitrarily combine multiple ones with logical expressions
3838
* **Parsing and Encoding** in most common binary-to-text-encodings: [hex](https://en.wikipedia.org/wiki/Hexadecimal), [base36](https://en.wikipedia.org/wiki/Base36), [base64](https://en.wikipedia.org/wiki/Base64), ...
@@ -164,6 +164,7 @@ Bytes.from(myBitSet); //java.util.BitSet myBitSet = ...
164164
Bytes.from(bigInteger); //java.math.BigInteger
165165
Bytes.from(file); //reads bytes from any java.io.File
166166
Bytes.from(dataInput, 16); //reads bytes from any java.io.DataInput
167+
Bytes.from(UUID.randomUUID()); //read 16 bytes from UUID
167168
```
168169

169170
For parsing binary-text-encoded strings, see below.

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.*;
24+
import java.io.ByteArrayInputStream;
25+
import java.io.DataInput;
26+
import java.io.File;
27+
import java.io.InputStream;
28+
import java.io.Serializable;
2529
import java.math.BigInteger;
2630
import java.nio.ByteBuffer;
2731
import java.nio.ByteOrder;
@@ -31,7 +35,14 @@
3135
import java.nio.charset.StandardCharsets;
3236
import java.security.SecureRandom;
3337
import java.text.Normalizer;
34-
import java.util.*;
38+
import java.util.Arrays;
39+
import java.util.BitSet;
40+
import java.util.Collection;
41+
import java.util.Iterator;
42+
import java.util.List;
43+
import java.util.Objects;
44+
import java.util.Random;
45+
import java.util.UUID;
3546

3647
/**
3748
* Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it:
@@ -471,6 +482,18 @@ public static Bytes from(char[] charArray, Charset charset) {
471482
return from(bytes);
472483
}
473484

485+
/**
486+
* Convert UUID to a newly generated 16 byte long array representation. Puts the 8 byte most significant bits and
487+
* 8 byte least significant bits into an byte array.
488+
*
489+
* @param uuid to convert to array
490+
* @return new instance
491+
*/
492+
public static Bytes from(UUID uuid) {
493+
Objects.requireNonNull(uuid);
494+
return wrap(Util.getBytesFromUUID(uuid).array());
495+
}
496+
474497
/**
475498
* Parses a big endian binary string (e.g. <code>10010001</code>)
476499
*
@@ -1498,6 +1521,21 @@ public BigInteger toBigInteger() {
14981521
}
14991522
}
15001523

1524+
/**
1525+
* Creates a {@link UUID} instance of the internal byte array. This requires the internal array to be exactly 16 bytes. Takes the first
1526+
* 8 byte as mostSigBits and the last 8 byte as leastSigBits. There is no validation of version/type, just passes the raw bytes
1527+
* to a {@link UUID} constructor.
1528+
*
1529+
* @return newly created UUID
1530+
*/
1531+
public UUID toUUID() {
1532+
if (length() != 16) {
1533+
throw new IllegalStateException("creating UUID requires internal array to be exactly 16 bytes, was " + length());
1534+
}
1535+
ByteBuffer byteBuffer = buffer();
1536+
return new UUID(byteBuffer.getLong(), byteBuffer.getLong());
1537+
}
1538+
15011539
/**
15021540
* If the underlying byte array is exactly 1 byte / 8 bit long, returns signed two-complement
15031541
* representation for a Java byte value.

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,23 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.*;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.DataInput;
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.io.InputStream;
2529
import java.nio.ByteBuffer;
2630
import java.nio.file.Files;
27-
import java.util.*;
31+
import java.util.ArrayList;
32+
import java.util.Collection;
33+
import java.util.HashMap;
34+
import java.util.Iterator;
35+
import java.util.List;
36+
import java.util.Map;
37+
import java.util.NoSuchElementException;
38+
import java.util.Objects;
39+
import java.util.Random;
40+
import java.util.UUID;
2841

2942
/**
3043
* Common Util methods to convert or modify byte arrays
@@ -446,6 +459,20 @@ static boolean constantTimeEquals(byte[] obj, byte[] anotherArray) {
446459
return result == 0;
447460
}
448461

462+
/**
463+
* Convert UUID to a newly generated 16 byte long array representation. Puts the 8 byte most significant bits and
464+
* 8 byte least significant bits into an byte array.
465+
*
466+
* @param uuid to convert to array
467+
* @return buffer containing the 16 bytes
468+
*/
469+
static ByteBuffer getBytesFromUUID(UUID uuid) {
470+
ByteBuffer bb = ByteBuffer.allocate(16);
471+
bb.putLong(uuid.getMostSignificantBits());
472+
bb.putLong(uuid.getLeastSignificantBits());
473+
return bb;
474+
}
475+
449476
/*
450477
=================================================================================================
451478
Copyright 2011 Twitter, Inc.

src/test/java/at/favre/lib/bytes/BytesConstructorTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Arrays;
3939
import java.util.BitSet;
4040
import java.util.LinkedList;
41+
import java.util.UUID;
4142

4243
import static org.junit.Assert.*;
4344

@@ -423,4 +424,23 @@ public void fromObjectArray() {
423424
Bytes b = Bytes.from(objectArray);
424425
assertArrayEquals(new byte[]{0x01, 0x02, 0x03, 0x04}, b.array());
425426
}
427+
428+
@Test
429+
public void fromUUID() {
430+
testUUID(UUID.fromString("00000000-0000-0000-0000-000000000000"));
431+
testUUID(UUID.fromString("123e4567-e89b-42d3-a456-556642440000"));
432+
testUUID(UUID.fromString("e8e3db08-dc39-48ea-a3db-08dc3958eafb"));
433+
testUUID(UUID.randomUUID());
434+
}
435+
436+
private void testUUID(UUID uuid) {
437+
Bytes b = Bytes.from(uuid);
438+
assertEquals(16, b.length());
439+
assertEquals(uuid, b.toUUID());
440+
}
441+
442+
@Test(expected = NullPointerException.class)
443+
public void fromUUIDNullArgument() {
444+
Bytes.from((UUID) null);
445+
}
426446
}

src/test/java/at/favre/lib/bytes/BytesToConvertOtherTypesTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,19 @@ public void toDouble() {
219219
} catch (IllegalStateException ignored) {
220220
}
221221
}
222-
}
222+
223+
@Test(expected = IllegalStateException.class)
224+
public void testToUUIDToLong() {
225+
Bytes.random(17).toUUID();
226+
}
227+
228+
@Test(expected = IllegalStateException.class)
229+
public void testToUUIDToShort() {
230+
Bytes.random(15).toUUID();
231+
}
232+
233+
@Test(expected = IllegalStateException.class)
234+
public void testToUUIDEmpty() {
235+
Bytes.allocate(0).toUUID();
236+
}
237+
}

0 commit comments

Comments
 (0)