Created
September 26, 2012 13:47
-
-
Save agentgt/3788145 to your computer and use it in GitHub Desktop.
CrockfordIdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CrockfordIdGenerator { | |
public static String encodeBase32(UUID uuid) { | |
Long leastBits = uuid.getLeastSignificantBits(); | |
Long mostBits = uuid.getMostSignificantBits(); | |
byte[] bytes = ByteBuffer.allocate(16) | |
.putLong(mostBits) | |
.putLong(leastBits).array(); | |
return new String(encodeBase32(bytes)); | |
} | |
public static String compact(UUID uuid) { | |
return encodeBase32(uuid).replaceAll("=", ""); | |
} | |
private static final char[] C = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".toCharArray(); | |
public static char[] encodeBase32(byte[] b) { | |
// Douglas Crockford's Base32 Encoding | |
// see http://www.crockford.com/wrmg/base32.html | |
assert b != null; | |
assert b.length <= Integer.MAX_VALUE / 8 * 5 - 4; | |
char[] c = new char[(b.length + 4) / 5 * 8]; | |
int i = 0, j = 0; | |
long x; | |
while (i + 4 < b.length) { | |
x = (long) (b[i + 0] & 0xff) << 32 | (long) (b[i + 1] & 0xff) << 24 | (long) (b[i + 2] & 0xff) << 16 | |
| (long) (b[i + 3] & 0xff) << 8 | (long) (b[i + 4] & 0xff); | |
c[j + 0] = C[(int) (x >> 35) & 0x1f]; | |
c[j + 1] = C[(int) (x >> 30) & 0x1f]; | |
c[j + 2] = C[(int) (x >> 25) & 0x1f]; | |
c[j + 3] = C[(int) (x >> 20) & 0x1f]; | |
c[j + 4] = C[(int) (x >> 15) & 0x1f]; | |
c[j + 5] = C[(int) (x >> 10) & 0x1f]; | |
c[j + 6] = C[(int) (x >> 5) & 0x1f]; | |
c[j + 7] = C[(int) (x >> 0) & 0x1f]; | |
i += 5; | |
j += 8; | |
} | |
if (i + 4 == b.length) { | |
x = (long) (b[i + 0] & 0xff) << 32 | (long) (b[i + 1] & 0xff) << 24 | (long) (b[i + 2] & 0xff) << 16 | |
| (long) (b[i + 3] & 0xff) << 8; | |
c[j + 0] = C[(int) (x >> 35) & 0x1f]; | |
c[j + 1] = C[(int) (x >> 30) & 0x1f]; | |
c[j + 2] = C[(int) (x >> 25) & 0x1f]; | |
c[j + 3] = C[(int) (x >> 20) & 0x1f]; | |
c[j + 4] = C[(int) (x >> 15) & 0x1f]; | |
c[j + 5] = C[(int) (x >> 10) & 0x1f]; | |
c[j + 6] = C[(int) (x >> 5) & 0x1f]; | |
c[j + 7] = '='; | |
} | |
else if (i + 3 == b.length) { | |
x = (long) (b[i + 0] & 0xff) << 32 | (long) (b[i + 1] & 0xff) << 24 | (long) (b[i + 2] & 0xff) << 16; | |
c[j + 0] = C[(int) (x >> 35) & 0x1f]; | |
c[j + 1] = C[(int) (x >> 30) & 0x1f]; | |
c[j + 2] = C[(int) (x >> 25) & 0x1f]; | |
c[j + 3] = C[(int) (x >> 20) & 0x1f]; | |
c[j + 4] = C[(int) (x >> 15) & 0x1f]; | |
c[j + 5] = '='; | |
c[j + 6] = '='; | |
c[j + 7] = '='; | |
} | |
else if (i + 2 == b.length) { | |
x = (long) (b[i + 0] & 0xff) << 32 | (long) (b[i + 1] & 0xff) << 24; | |
c[j + 0] = C[(int) (x >> 35) & 0x1f]; | |
c[j + 1] = C[(int) (x >> 30) & 0x1f]; | |
c[j + 2] = C[(int) (x >> 25) & 0x1f]; | |
c[j + 3] = C[(int) (x >> 20) & 0x1f]; | |
c[j + 4] = '='; | |
c[j + 5] = '='; | |
c[j + 6] = '='; | |
c[j + 7] = '='; | |
} | |
else if (i + 1 == b.length) { | |
x = (long) (b[i + 0] & 0xff) << 32; | |
c[j + 0] = C[(int) (x >> 35) & 0x1f]; | |
c[j + 1] = C[(int) (x >> 30) & 0x1f]; | |
c[j + 2] = '='; | |
c[j + 3] = '='; | |
c[j + 4] = '='; | |
c[j + 5] = '='; | |
c[j + 6] = '='; | |
c[j + 7] = '='; | |
} | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment