[Minor] cleaned up compiler warnings in BaseEncoding

This commit is contained in:
Robert von Burg 2013-11-01 18:48:57 +01:00
parent 9c547af5bb
commit 994de0241a
1 changed files with 49 additions and 29 deletions

View File

@ -21,6 +21,8 @@
*/ */
package ch.eitchnet.utils.helper; package ch.eitchnet.utils.helper;
import java.text.MessageFormat;
/** /**
* <p> * <p>
* This class implements the encoding and decoding of RFC 4648 <a>https://tools.ietf.org/html/rfc4648</a>. * This class implements the encoding and decoding of RFC 4648 <a>https://tools.ietf.org/html/rfc4648</a>.
@ -368,8 +370,10 @@ public class BaseEncoding {
public static byte[] toBase64(byte[] alphabet, byte[] bytes) { public static byte[] toBase64(byte[] alphabet, byte[] bytes) {
if (bytes.length == 0) if (bytes.length == 0)
return new byte[0]; return new byte[0];
if (alphabet.length != 64) if (alphabet.length != 64) {
throw new RuntimeException("Alphabet does not have expected size 64 but is " + alphabet.length); String msg = MessageFormat.format("Alphabet does not have expected size 64 but is {0}", alphabet.length); //$NON-NLS-1$
throw new RuntimeException(msg);
}
// 6 bits input for every 8 bits (1 byte) output // 6 bits input for every 8 bits (1 byte) output
// least common multiple of 6 bits input and 8 bits output = 24 // least common multiple of 6 bits input and 8 bits output = 24
@ -476,8 +480,10 @@ public class BaseEncoding {
public static byte[] toBase32(byte[] alphabet, byte[] bytes) { public static byte[] toBase32(byte[] alphabet, byte[] bytes) {
if (bytes.length == 0) if (bytes.length == 0)
return new byte[0]; return new byte[0];
if (alphabet.length != 32) if (alphabet.length != 32) {
throw new RuntimeException("Alphabet does not have expected size 32 but is " + alphabet.length); String msg = MessageFormat.format("Alphabet does not have expected size 32 but is {0}", alphabet.length); //$NON-NLS-1$
throw new RuntimeException(msg);
}
// 5 bits input for every 8 bits (1 byte) output // 5 bits input for every 8 bits (1 byte) output
// least common multiple of 5 bits input and 8 bits output = 40 // least common multiple of 5 bits input and 8 bits output = 40
@ -598,8 +604,10 @@ public class BaseEncoding {
public static byte[] toBase16(byte[] alphabet, byte[] bytes) { public static byte[] toBase16(byte[] alphabet, byte[] bytes) {
if (bytes.length == 0) if (bytes.length == 0)
return new byte[0]; return new byte[0];
if (alphabet.length != 16) if (alphabet.length != 16) {
throw new RuntimeException("Alphabet does not have expected size 16 but is " + alphabet.length); String msg = MessageFormat.format("Alphabet does not have expected size 16 but is {0}", alphabet.length); //$NON-NLS-1$
throw new RuntimeException(msg);
}
// calculate output text length // calculate output text length
int nrOfInputBytes = bytes.length; int nrOfInputBytes = bytes.length;
@ -651,15 +659,19 @@ public class BaseEncoding {
if (inputLength == 0) if (inputLength == 0)
return new byte[0]; return new byte[0];
if ((inputLength % 4) != 0) { if ((inputLength % 4) != 0) {
throw new RuntimeException("The input bytes to be decoded must be multiples of 4, but is multiple of " String msg = MessageFormat.format(
+ (inputLength % 4)); "The input bytes to be decoded must be multiples of 4, but is multiple of {0}", //$NON-NLS-1$
(inputLength % 4));
throw new RuntimeException(msg);
} }
if (alphabet.length != 128) if (alphabet.length != 128) {
throw new RuntimeException("Alphabet does not have expected size 128 but is " + alphabet.length); String msg = MessageFormat.format("Alphabet does not have expected size 128 but is {0}", alphabet.length); //$NON-NLS-1$
throw new RuntimeException(msg);
}
if (!isEncodedByAlphabet(alphabet, bytes, PADDING_64)) if (!isEncodedByAlphabet(alphabet, bytes, PADDING_64))
throw new RuntimeException("The data contains illegal values which are not mapped by the given alphabet!"); throw new RuntimeException("The data contains illegal values which are not mapped by the given alphabet!"); //$NON-NLS-1$
// find how much padding we have // find how much padding we have
int nrOfBytesPadding = 0; int nrOfBytesPadding = 0;
@ -786,15 +798,18 @@ public class BaseEncoding {
if (inputLength == 0) if (inputLength == 0)
return new byte[0]; return new byte[0];
if ((inputLength % 8) != 0) { if ((inputLength % 8) != 0) {
throw new RuntimeException("The input bytes to be decoded must be multiples of 8, but is multiple of " String msg = "The input bytes to be decoded must be multiples of 8, but is multiple of {0}"; //$NON-NLS-1$
+ (inputLength % 8)); msg = MessageFormat.format(msg, (inputLength % 8));
throw new RuntimeException(msg);
} }
if (alphabet.length != 128) if (alphabet.length != 128) {
throw new RuntimeException("Alphabet does not have expected size 128 but is " + alphabet.length); String msg = MessageFormat.format("Alphabet does not have expected size 128 but is {0}", alphabet.length); //$NON-NLS-1$
throw new RuntimeException(msg);
}
if (!isEncodedByAlphabet(alphabet, bytes, PADDING_32)) if (!isEncodedByAlphabet(alphabet, bytes, PADDING_32))
throw new RuntimeException("The data contains illegal values which are not mapped by the given alphabet!"); throw new RuntimeException("The data contains illegal values which are not mapped by the given alphabet!"); //$NON-NLS-1$
// find how much padding we have // find how much padding we have
int nrOfBytesPadding = 0; int nrOfBytesPadding = 0;
@ -945,15 +960,18 @@ public class BaseEncoding {
if (bytes.length == 0) if (bytes.length == 0)
return new byte[0]; return new byte[0];
if ((bytes.length % 2) != 0) { if ((bytes.length % 2) != 0) {
throw new RuntimeException("The input bytes to be decoded must be multiples of 4, but is multiple of " String msg = "The input bytes to be decoded must be multiples of 4, but is multiple of {0}"; //$NON-NLS-1$
+ (bytes.length % 4)); msg = MessageFormat.format(msg, (bytes.length % 4));
throw new RuntimeException(msg);
} }
if (alphabet.length != 128) if (alphabet.length != 128) {
throw new RuntimeException("Alphabet does not have expected size 128 but is " + alphabet.length); String msg = MessageFormat.format("Alphabet does not have expected size 128 but is {0}", alphabet.length); //$NON-NLS-1$
throw new RuntimeException(msg);
}
if (!isEncodedByAlphabet(alphabet, bytes, 0)) if (!isEncodedByAlphabet(alphabet, bytes, 0))
throw new RuntimeException("The data contains illegal values which are not mapped by the given alphabet!"); throw new RuntimeException("The data contains illegal values which are not mapped by the given alphabet!"); //$NON-NLS-1$
int dataLength = bytes.length / 2; int dataLength = bytes.length / 2;
@ -963,25 +981,27 @@ public class BaseEncoding {
byte b1 = bytes[i++]; byte b1 = bytes[i++];
byte b2 = bytes[i++]; byte b2 = bytes[i++];
String msgOutOfRange = "Value at index {0} is not in range of alphabet (0-127){1}"; //$NON-NLS-1$
if (b1 < 0) { if (b1 < 0) {
throw new IllegalArgumentException("Value at index " + (i - 2) + " is not in range of alphabet (0-127)" msgOutOfRange = MessageFormat.format(msgOutOfRange, (i - 2), b1);
+ b1); throw new IllegalArgumentException(msgOutOfRange);
} }
if (b2 < 0) { if (b2 < 0) {
throw new IllegalArgumentException("Value at index " + (i - 1) + " is not in range of alphabet (0-127)" msgOutOfRange = MessageFormat.format(msgOutOfRange, (i - 1), b2);
+ b2); throw new IllegalArgumentException(msgOutOfRange);
} }
byte c1 = alphabet[b1]; byte c1 = alphabet[b1];
byte c2 = alphabet[b2]; byte c2 = alphabet[b2];
String msgIllegalValue = "Value at index {0} is referencing illegal value in alphabet: {1}"; //$NON-NLS-1$
if (c1 == -1) { if (c1 == -1) {
throw new IllegalArgumentException("Value at index " + (i - 2) msgIllegalValue = MessageFormat.format(msgIllegalValue, (i - 2), b1);
+ " is referencing illegal value in alphabet: " + b1); throw new IllegalArgumentException(msgIllegalValue);
} }
if (c2 == -1) { if (c2 == -1) {
throw new IllegalArgumentException("Value at index " + (i - 2) msgIllegalValue = MessageFormat.format(msgIllegalValue, (i - 2), b2);
+ " is referencing illegal value in alphabet: " + b2); throw new IllegalArgumentException(msgIllegalValue);
} }
int dataIndex = (i / 2) - 1; int dataIndex = (i / 2) - 1;