diff --git a/src/main/java/dev/keiji/util/Base16.java b/src/main/java/dev/keiji/util/Base16.java index c8953f4..cb6b0a3 100644 --- a/src/main/java/dev/keiji/util/Base16.java +++ b/src/main/java/dev/keiji/util/Base16.java @@ -36,7 +36,7 @@ private Base16() { private static final int PLAIN_DATA_BLOCK_SIZE = 1; private static final int ENCODED_DATA_BLOCK_SIZE = 2; - private static final char[] TABLE_ENCODE = { + private static final byte[] TABLE_ENCODE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', }; @@ -103,8 +103,8 @@ private static void encode( while (byteArrayInputStream.read(plainDataBlock, 0, PLAIN_DATA_BLOCK_SIZE) > 0) { int value = byteToInt(plainDataBlock[0]); - encodedDataBlock[0] = (byte) TABLE_ENCODE[getIndex(value, 4)]; - encodedDataBlock[1] = (byte) TABLE_ENCODE[getIndex(value, 0)]; + encodedDataBlock[0] = TABLE_ENCODE[getIndex(value, 4)]; + encodedDataBlock[1] = TABLE_ENCODE[getIndex(value, 0)]; byteArrayOutputStream.write(encodedDataBlock, 0, ENCODED_DATA_BLOCK_SIZE); } diff --git a/src/main/java/dev/keiji/util/Base32.java b/src/main/java/dev/keiji/util/Base32.java index 0e5859c..50033c8 100644 --- a/src/main/java/dev/keiji/util/Base32.java +++ b/src/main/java/dev/keiji/util/Base32.java @@ -38,14 +38,14 @@ private Base32() { private static final char PAD = '='; - private static final char[] TABLE_ENCODE = { + private static final byte[] TABLE_ENCODE = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', }; - private static final char[] TABLE_ENCODE_EXTENDED_HEX = { + private static final byte[] TABLE_ENCODE_EXTENDED_HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', @@ -111,7 +111,7 @@ private static class Encoder { private static final int BIT_WIDTH = 5; private static final long BIT_MASK = 0x1F; // = 00011111 - public static String encode(byte[] input, char[] tableEncode) { + public static String encode(byte[] input, byte[] tableEncode) { if (input == null) { throw new IllegalArgumentException("Input data must not be null."); } @@ -131,7 +131,7 @@ public static String encode(byte[] input, char[] tableEncode) { private static void encode( ByteArrayInputStream byteArrayInputStream, ByteArrayOutputStream byteArrayOutputStream, - char[] tableEncode + byte[] tableEncode ) { byte[] plainDataBlock = new byte[PLAIN_DATA_BLOCK_SIZE]; byte[] encodedDataBlock = new byte[ENCODED_DATA_BLOCK_SIZE]; @@ -144,14 +144,14 @@ private static void encode( long value = getLongFromBlock(plainDataBlock); - encodedDataBlock[0] = (byte) tableEncode[getIndex(value, 35)]; - encodedDataBlock[1] = (byte) tableEncode[getIndex(value, 30)]; - encodedDataBlock[2] = (byte) tableEncode[getIndex(value, 25)]; - encodedDataBlock[3] = (byte) tableEncode[getIndex(value, 20)]; - encodedDataBlock[4] = (byte) tableEncode[getIndex(value, 15)]; - encodedDataBlock[5] = (byte) tableEncode[getIndex(value, 10)]; - encodedDataBlock[6] = (byte) tableEncode[getIndex(value, 5)]; - encodedDataBlock[7] = (byte) tableEncode[getIndex(value, 0)]; + encodedDataBlock[0] = tableEncode[getIndex(value, 35)]; + encodedDataBlock[1] = tableEncode[getIndex(value, 30)]; + encodedDataBlock[2] = tableEncode[getIndex(value, 25)]; + encodedDataBlock[3] = tableEncode[getIndex(value, 20)]; + encodedDataBlock[4] = tableEncode[getIndex(value, 15)]; + encodedDataBlock[5] = tableEncode[getIndex(value, 10)]; + encodedDataBlock[6] = tableEncode[getIndex(value, 5)]; + encodedDataBlock[7] = tableEncode[getIndex(value, 0)]; byteArrayOutputStream.write(encodedDataBlock, 0, ENCODED_DATA_BLOCK_SIZE - padSize); @@ -168,13 +168,13 @@ private static byte getIndex(long value, int shift) { return (byte) ((value & BIT_MASK << shift) >>> shift); } - private static long getLongFromBlock(byte[] bucket) { + private static long getLongFromBlock(byte[] block) { return ( - byteToLong(bucket[0]) << 32) - + (byteToLong(bucket[1]) << 24) - + (byteToLong(bucket[2]) << 16) - + (byteToLong(bucket[3]) << 8) - + (byteToLong(bucket[4]) + byteToLong(block[0]) << 32) + + (byteToLong(block[1]) << 24) + + (byteToLong(block[2]) << 16) + + (byteToLong(block[3]) << 8) + + (byteToLong(block[4]) ); } diff --git a/src/main/java/dev/keiji/util/Base64.java b/src/main/java/dev/keiji/util/Base64.java index 7a9a389..13e7d48 100644 --- a/src/main/java/dev/keiji/util/Base64.java +++ b/src/main/java/dev/keiji/util/Base64.java @@ -40,7 +40,7 @@ private Base64() { private static final char PAD = '='; - private static final char[] TABLE_ENCODE = { + private static final byte[] TABLE_ENCODE = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', @@ -51,7 +51,7 @@ private Base64() { '4', '5', '6', '7', '8', '9', '+', '/', }; - private static final char[] TABLE_ENCODE_URL_SAFE = { + private static final byte[] TABLE_ENCODE_URL_SAFE = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', @@ -120,7 +120,7 @@ public static byte[] decodeUrlSafe(String input) { private static class Encoder { private static final int BIT_MASK = 0x3F; // = 00111111 - public static String encode(byte[] input, char[] tableEncode) { + public static String encode(byte[] input, byte[] tableEncode) { if (input == null) { throw new IllegalArgumentException("Input data must not be null."); } @@ -140,7 +140,7 @@ public static String encode(byte[] input, char[] tableEncode) { private static void encode( ByteArrayInputStream byteArrayInputStream, ByteArrayOutputStream byteArrayOutputStream, - char[] tableEncode + byte[] tableEncode ) { byte[] plainDataBlock = new byte[PLAIN_DATA_BLOCK_SIZE]; byte[] encodedDataBlock = new byte[ENCODED_DATA_BLOCK_SIZE]; @@ -157,10 +157,10 @@ private static void encode( int value = getIntFromBlock(plainDataBlock); - encodedDataBlock[0] = (byte) tableEncode[getIndex(value, 18)]; - encodedDataBlock[1] = (byte) tableEncode[getIndex(value, 12)]; - encodedDataBlock[2] = (byte) tableEncode[getIndex(value, 6)]; - encodedDataBlock[3] = (byte) tableEncode[getIndex(value, 0)]; + encodedDataBlock[0] = tableEncode[getIndex(value, 18)]; + encodedDataBlock[1] = tableEncode[getIndex(value, 12)]; + encodedDataBlock[2] = tableEncode[getIndex(value, 6)]; + encodedDataBlock[3] = tableEncode[getIndex(value, 0)]; byteArrayOutputStream.write(encodedDataBlock, 0, ENCODED_DATA_BLOCK_SIZE - padSize); @@ -177,11 +177,11 @@ private static byte getIndex(long value, int shift) { return (byte) ((value & BIT_MASK << shift) >>> shift); } - private static int getIntFromBlock(byte[] bucket) { + private static int getIntFromBlock(byte[] block) { return ( - byteToInt(bucket[0]) << 16) - + (byteToInt(bucket[1]) << 8) - + (byteToInt(bucket[2]) + byteToInt(block[0]) << 16) + + (byteToInt(block[1]) << 8) + + (byteToInt(block[2]) ); }