Converts a Long to a byte array with the proper UID width @param uid The UID to convert @param width The width of the UID in bytes @return The UID as a byte array @throws IllegalStateException if the UID is larger than the width would allow @since 2.1
(final long uid, final short width)
| 1475 | * @since 2.1 |
| 1476 | */ |
| 1477 | public static byte[] longToUID(final long uid, final short width) { |
| 1478 | // Verify that we're going to drop bytes that are 0. |
| 1479 | final byte[] padded = Bytes.fromLong(uid); |
| 1480 | for (int i = 0; i < padded.length - width; i++) { |
| 1481 | if (padded[i] != 0) { |
| 1482 | final String message = "UID " + Long.toString(uid) + |
| 1483 | " was too large for " + width + " bytes"; |
| 1484 | LOG.error("OMG " + message); |
| 1485 | throw new IllegalStateException(message); |
| 1486 | } |
| 1487 | } |
| 1488 | // Shrink the ID on the requested number of bytes. |
| 1489 | return Arrays.copyOfRange(padded, padded.length - width, padded.length); |
| 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * Appends the given UID to the given string buffer, followed by "\\E". |