Skip to content

Commit

Permalink
Remove unnecessary cast.
Browse files Browse the repository at this point in the history
  • Loading branch information
keiji committed Oct 18, 2021
1 parent ce7d9f5 commit 962680c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/main/java/dev/keiji/util/Base16.java
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down Expand Up @@ -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);
}
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/dev/keiji/util/Base32.java
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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];
Expand All @@ -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);

Expand All @@ -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])
);
}

Expand Down
24 changes: 12 additions & 12 deletions src/main/java/dev/keiji/util/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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];
Expand All @@ -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);

Expand All @@ -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])
);
}

Expand Down

0 comments on commit 962680c

Please sign in to comment.