Create utility to convert UUID to bytes and back.

Description

The Java UUID class stores its bytes internally as two long values, but there is no easy way to go back and forth between an array of bytes and a UUID. Some have recommended using a ByteBuffer, and others have recommended a DataOutputStream, but there seems little point in all that overhead just to some simple byte operations.

Look at the Java code of DataOutputStream.writeLong(). It just uses a few big shifts and casts. Simply repeat this for two longs, and the job should be done. Do the reverse shifting with some OR operations to go the other way.

These should be placed in UUIDs.toBytes() and UUIDs.fromBytes() methods, with variations to use an existing array if given.

Environment

None

Assignee

Garret Wilson

Reporter

Garret Wilson

Labels

None

Components

Priority

Major