[#5824] Add Convert support for converting UUID to byte[] and vice versa

This commit is contained in:
lukaseder 2017-02-01 10:43:10 +01:00
parent ff07baf84c
commit 50d01b3d5a

View File

@ -49,6 +49,7 @@ import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
@ -484,8 +485,18 @@ public final class Convert {
// Regular checks
else if (fromClass == byte[].class) {
// [#5824] UUID's most significant bits in byte[] are first
if (toClass == UUID.class) {
ByteBuffer b = ByteBuffer.wrap((byte[]) from);
long mostSigBits = b.getLong();
long leastSigBits = b.getLong();
return (U) new UUID(mostSigBits, leastSigBits);
}
// [#5569] Binary data is expected to be in JVM's default encoding
return convert(new String((byte[]) from), toClass);
else {
return convert(new String((byte[]) from), toClass);
}
}
else if (fromClass.isArray()) {
@ -515,7 +526,17 @@ public final class Convert {
// [#5569] It should be possible, at least, to convert an empty string to an empty (var)binary.
else if (toClass == byte[].class) {
return (U) from.toString().getBytes();
// [#5824] UUID's most significant bits in byte[] are first
if (from instanceof UUID) {
ByteBuffer b = ByteBuffer.wrap(new byte[16]);
b.putLong(((UUID) from).getMostSignificantBits());
b.putLong(((UUID) from).getLeastSignificantBits());
return (U)b.array();
}
else {
return (U) from.toString().getBytes();
}
}
// Various number types are converted between each other via String