CODEC-295 - Minor improvement (#67)
diff --git a/src/main/java/org/apache/commons/codec/binary/BinaryCodec.java b/src/main/java/org/apache/commons/codec/binary/BinaryCodec.java
index 590dd3e..c7f383e 100644
--- a/src/main/java/org/apache/commons/codec/binary/BinaryCodec.java
+++ b/src/main/java/org/apache/commons/codec/binary/BinaryCodec.java
@@ -80,13 +80,14 @@
if (isEmpty(ascii)) {
return EMPTY_BYTE_ARRAY;
}
+ final int asciiLength = ascii.length;
// get length/8 times bytes with 3 bit shifts to the right of the length
- final byte[] l_raw = new byte[ascii.length >> 3];
+ final byte[] l_raw = new byte[asciiLength >> 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
- for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
+ for (int ii = 0, jj = asciiLength - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if (ascii[jj - bits] == '1') {
l_raw[ii] |= BITS[bits];
@@ -112,13 +113,14 @@
if (ascii == null || ascii.length == 0) {
return EMPTY_BYTE_ARRAY;
}
+ final int asciiLength = ascii.length;
// get length/8 times bytes with 3 bit shifts to the right of the length
- final byte[] l_raw = new byte[ascii.length >> 3];
+ final byte[] l_raw = new byte[asciiLength >> 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
- for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
+ for (int ii = 0, jj = asciiLength - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if (ascii[jj - bits] == '1') {
l_raw[ii] |= BITS[bits];
@@ -152,13 +154,14 @@
if (isEmpty(raw)) {
return EMPTY_BYTE_ARRAY;
}
+ final int rawLength = raw.length;
// get 8 times the bytes with 3 bit shifts to the left of the length
- final byte[] l_ascii = new byte[raw.length << 3];
+ final byte[] l_ascii = new byte[rawLength << 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
- for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
+ for (int ii = 0, jj = l_ascii.length - 1; ii < rawLength; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if ((raw[ii] & BITS[bits]) == 0) {
l_ascii[jj - bits] = '0';
@@ -182,13 +185,14 @@
if (isEmpty(raw)) {
return EMPTY_CHAR_ARRAY;
}
+ final int rawLength = raw.length;
// get 8 times the bytes with 3 bit shifts to the left of the length
- final char[] l_ascii = new char[raw.length << 3];
+ final char[] l_ascii = new char[rawLength << 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
- for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
+ for (int ii = 0, jj = l_ascii.length - 1; ii < rawLength; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if ((raw[ii] & BITS[bits]) == 0) {
l_ascii[jj - bits] = '0';
diff --git a/src/main/java/org/apache/commons/codec/binary/Hex.java b/src/main/java/org/apache/commons/codec/binary/Hex.java
index 2294956..7075ab9 100644
--- a/src/main/java/org/apache/commons/codec/binary/Hex.java
+++ b/src/main/java/org/apache/commons/codec/binary/Hex.java
@@ -166,9 +166,9 @@
* @since 1.4
*/
protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
- final int l = data.length;
- final char[] out = new char[l << 1];
- encodeHex(data, 0, data.length, toDigits, out, 0);
+ final int dataLength = data.length;
+ final char[] out = new char[dataLength << 1];
+ encodeHex(data, 0, dataLength, toDigits, out, 0);
return out;
}
@@ -519,7 +519,7 @@
*/
@Override
public Object encode(final Object object) throws EncoderException {
- byte[] byteArray;
+ final byte[] byteArray;
if (object instanceof String) {
byteArray = ((String) object).getBytes(this.getCharset());
} else if (object instanceof ByteBuffer) {
diff --git a/src/main/java/org/apache/commons/codec/cli/Digest.java b/src/main/java/org/apache/commons/codec/cli/Digest.java
index 4e553cf..9815034 100644
--- a/src/main/java/org/apache/commons/codec/cli/Digest.java
+++ b/src/main/java/org/apache/commons/codec/cli/Digest.java
@@ -61,16 +61,17 @@
if (args == null) {
throw new IllegalArgumentException("args");
}
- if (args.length == 0) {
+ final int argsLength = args.length;
+ if (argsLength == 0) {
throw new IllegalArgumentException(
String.format("Usage: java %s [algorithm] [FILE|DIRECTORY|string] ...", Digest.class.getName()));
}
this.args = args;
algorithm = args[0];
- if (args.length <= 1) {
+ if (argsLength <= 1) {
inputs = null;
} else {
- inputs = new String[args.length -1];
+ inputs = new String[argsLength - 1];
System.arraycopy(args, 1, inputs, 0, inputs.length);
}
}
diff --git a/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java b/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java
index f607b88..82011a2 100644
--- a/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java
+++ b/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java
@@ -283,7 +283,7 @@
final int keyLen = keyBytes.length;
// Extract the real salt from the given string which can be a complete hash string.
- String saltString;
+ final String saltString;
if (salt == null) {
saltString = B64.getRandomSalt(8, random);
} else {
diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java b/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
index 6b471df..0c28cff 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
@@ -1078,8 +1078,8 @@
}
// Combine unprocessed bytes with new bytes.
- int newOffset;
- int newLength;
+ final int newOffset;
+ final int newLength;
if (unprocessedLength > 0) {
int k = -1;
switch (unprocessedLength) {
diff --git a/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java b/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
index 7be4958..b77e4f3 100644
--- a/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
+++ b/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
@@ -220,7 +220,8 @@
final byte key[] = new byte[8];
Arrays.fill(key, (byte) 0);
- for (int i = 0; i < key.length && i < original.length; i++) {
+ final int originalLength = original.length;
+ for (int i = 0; i < key.length && i < originalLength; i++) {
final int iChar = original[i];
key[i] = (byte) (iChar << 1);
}
diff --git a/src/main/java/org/apache/commons/codec/language/Metaphone.java b/src/main/java/org/apache/commons/codec/language/Metaphone.java
index 84a8575..5ee6466 100644
--- a/src/main/java/org/apache/commons/codec/language/Metaphone.java
+++ b/src/main/java/org/apache/commons/codec/language/Metaphone.java
@@ -89,7 +89,7 @@
*/
public String metaphone(final String txt) {
boolean hard = false;
- int txtLength;
+ final int txtLength;
if (txt == null || (txtLength = txt.length()) == 0) {
return "";
}
diff --git a/src/main/java/org/apache/commons/codec/language/bm/Rule.java b/src/main/java/org/apache/commons/codec/language/bm/Rule.java
index 644614a..d55ce8c 100644
--- a/src/main/java/org/apache/commons/codec/language/bm/Rule.java
+++ b/src/main/java/org/apache/commons/codec/language/bm/Rule.java
@@ -84,8 +84,10 @@
public static final Comparator<Phoneme> COMPARATOR = new Comparator<Phoneme>() {
@Override
public int compare(final Phoneme o1, final Phoneme o2) {
- for (int i = 0; i < o1.phonemeText.length(); i++) {
- if (i >= o2.phonemeText.length()) {
+ final int o1Length = o1.phonemeText.length();
+ final int o2Length = o2.phonemeText.length();
+ for (int i = 0; i < o1Length; i++) {
+ if (i >= o2Length) {
return +1;
}
final int c = o1.phonemeText.charAt(i) - o2.phonemeText.charAt(i);
@@ -94,7 +96,7 @@
}
}
- if (o1.phonemeText.length() < o2.phonemeText.length()) {
+ if (o1Length < o2Length) {
return -1;
}
@@ -205,6 +207,9 @@
private static final String HASH_INCLUDE = "#include";
+ private static final int HASH_INCLUDE_LENGTH = HASH_INCLUDE.length();
+
+
private static final Map<NameType, Map<RuleType, Map<String, Map<String, List<Rule>>>>> RULES =
new EnumMap<>(NameType.class);
@@ -262,10 +267,13 @@
}
private static boolean endsWith(final CharSequence input, final CharSequence suffix) {
- if (suffix.length() > input.length()) {
+ final int suffixLength = suffix.length();
+ final int inputLength = input.length();
+
+ if (suffixLength > inputLength) {
return false;
}
- for (int i = input.length() - 1, j = suffix.length() - 1; j >= 0; i--, j--) {
+ for (int i = inputLength - 1, j = suffixLength - 1; j >= 0; i--, j--) {
if (input.charAt(i) != suffix.charAt(j)) {
return false;
}
@@ -419,7 +427,7 @@
if (line.startsWith(HASH_INCLUDE)) {
// include statement
- final String incl = line.substring(HASH_INCLUDE.length()).trim();
+ final String incl = line.substring(HASH_INCLUDE_LENGTH).trim();
if (incl.contains(" ")) {
throw new IllegalArgumentException("Malformed import statement '" + rawLine + "' in " +
location);
@@ -492,7 +500,7 @@
if (!boxes) {
if (startsWith && endsWith) {
// exact match
- if (content.length() == 0) {
+ if (content.isEmpty()) {
// empty
return new RPattern() {
@Override
@@ -507,7 +515,7 @@
return input.equals(content);
}
};
- } else if ((startsWith || endsWith) && content.length() == 0) {
+ } else if ((startsWith || endsWith) && content.isEmpty()) {
// matches every string
return ALL_STRINGS_RMATCHER;
} else if (startsWith) {
diff --git a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
index e18b298..5641dc8 100644
--- a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
@@ -282,12 +282,13 @@
printable = PRINTABLE_CHARS;
}
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ final int bytesLength = bytes.length;
if (strict) {
int pos = 1;
// encode up to buffer.length - 3, the last three octets will be treated
// separately for simplification of note #3
- for (int i = 0; i < bytes.length - 3; i++) {
+ for (int i = 0; i < bytesLength - 3; i++) {
final int b = getUnsignedOctet(i, bytes);
if (pos < SAFE_LENGTH) {
// up to this length it is safe to add any byte, encoded or not
@@ -306,7 +307,7 @@
// rule #3: whitespace at the end of a line *must* be encoded
// if we would do a soft break line after this octet, encode whitespace
- int b = getUnsignedOctet(bytes.length - 3, bytes);
+ int b = getUnsignedOctet(bytesLength - 3, bytes);
boolean encode = !printable.get(b) || (isWhitespace(b) && pos > SAFE_LENGTH - 5);
pos += encodeByte(b, encode, buffer);
@@ -318,10 +319,10 @@
buffer.write(CR);
buffer.write(LF);
}
- for (int i = bytes.length - 2; i < bytes.length; i++) {
+ for (int i = bytesLength - 2; i < bytesLength; i++) {
b = getUnsignedOctet(i, bytes);
// rule #3: trailing whitespace shall be encoded
- encode = !printable.get(b) || (i > bytes.length - 2 && isWhitespace(b));
+ encode = !printable.get(b) || (i > bytesLength - 2 && isWhitespace(b));
encodeByte(b, encode, buffer);
}
} else {
diff --git a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
index 90564fe..16bbcf4 100644
--- a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
+++ b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
@@ -55,7 +55,7 @@
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.
* <p>
* This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes
- * {@link #doEncoding(byte [])} method of a concrete class to perform the specific encoding.
+ * {@link #doEncoding(byte[])} method of a concrete class to perform the specific encoding.
*
* @param text
* a string to encode
@@ -85,7 +85,7 @@
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.
* <p>
* This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes
- * {@link #doEncoding(byte [])} method of a concrete class to perform the specific encoding.
+ * {@link #doEncoding(byte[])} method of a concrete class to perform the specific encoding.
*
* @param text
* a string to encode
@@ -111,7 +111,7 @@
* Applies an RFC 1522 compliant decoding scheme to the given string of text.
* <p>
* This method processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
- * {@link #doEncoding(byte [])} method of a concrete class to perform the specific decoding.
+ * {@link #doDecoding(byte[])} method of a concrete class to perform the specific decoding.
*
* @param text
* a string to decode
diff --git a/src/test/java/org/apache/commons/codec/binary/Base16Test.java b/src/test/java/org/apache/commons/codec/binary/Base16Test.java
index f60709d..baae38f 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base16Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base16Test.java
@@ -85,7 +85,7 @@
private void testBase16InBuffer(final int startPasSize, final int endPadSize) {
final String content = "Hello World";
- String encodedContent;
+ final String encodedContent;
final byte[] bytesUtf8 = StringUtils.getBytesUtf8(content);
byte[] buffer = ArrayUtils.addAll(bytesUtf8, new byte[endPadSize]);
buffer = ArrayUtils.addAll(new byte[startPasSize], buffer);
diff --git a/src/test/java/org/apache/commons/codec/binary/Base32Test.java b/src/test/java/org/apache/commons/codec/binary/Base32Test.java
index 068f5f8..475bb2f 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base32Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base32Test.java
@@ -27,7 +27,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
-import junit.framework.Assert;
import org.apache.commons.codec.CodecPolicy;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.lang3.ArrayUtils;
@@ -206,7 +205,7 @@
public void testBase32BinarySamples() throws Exception {
final Base32 codec = new Base32();
for (final Object[] element : BASE32_BINARY_TEST_CASES) {
- String expected;
+ final String expected;
if(element.length > 2) {
expected = (String)element[2];
} else {
diff --git a/src/test/java/org/apache/commons/codec/binary/Base64Test.java b/src/test/java/org/apache/commons/codec/binary/Base64Test.java
index a6e834b..e4be6d0 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base64Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base64Test.java
@@ -151,7 +151,7 @@
private void testBase64InBuffer(final int startPasSize, final int endPadSize) {
final String content = "Hello World";
- String encodedContent;
+ final String encodedContent;
final byte[] bytesUtf8 = StringUtils.getBytesUtf8(content);
byte[] buffer = ArrayUtils.addAll(bytesUtf8, new byte[endPadSize]);
buffer = ArrayUtils.addAll(new byte[startPasSize], buffer);
diff --git a/src/test/java/org/apache/commons/codec/binary/HexTest.java b/src/test/java/org/apache/commons/codec/binary/HexTest.java
index 7f0fc1c..bab8754 100644
--- a/src/test/java/org/apache/commons/codec/binary/HexTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/HexTest.java
@@ -29,7 +29,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
-import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
diff --git a/src/test/java/org/apache/commons/codec/cli/DigestTest.java b/src/test/java/org/apache/commons/codec/cli/DigestTest.java
new file mode 100644
index 0000000..e5d279b
--- /dev/null
+++ b/src/test/java/org/apache/commons/codec/cli/DigestTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.cli;
+
+import org.junit.Test;
+
+import java.io.IOException;
+
+
+/**
+ * Tests {@link Digest}.
+ *
+ * @since 1.17
+ */
+public class DigestTest {
+
+ /**
+ * Tests if empty arguments are handled correctly.
+ *
+ * @throws IllegalArgumentException for some failure scenarios.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testEmptyArguments() throws IOException {
+ Digest.main(new String[0]);
+ }
+ /**
+ * Tests if null arguments are handled correctly.
+ *
+ * @throws IllegalArgumentException for some failure scenarios.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testNullArguments() throws IOException {
+ Digest.main(null);
+ }
+}
diff --git a/src/test/java/org/apache/commons/codec/language/bm/PhoneticEngineRegressionTest.java b/src/test/java/org/apache/commons/codec/language/bm/PhoneticEngineRegressionTest.java
index f04fe4d..cb1a7ed 100644
--- a/src/test/java/org/apache/commons/codec/language/bm/PhoneticEngineRegressionTest.java
+++ b/src/test/java/org/apache/commons/codec/language/bm/PhoneticEngineRegressionTest.java
@@ -205,8 +205,8 @@
* regressions in Commons-Codec.
*/
private static String encode(final Map<String, String> args, final boolean concat, final String input) {
- Languages.LanguageSet languageSet;
- PhoneticEngine engine;
+ final Languages.LanguageSet languageSet;
+ final PhoneticEngine engine;
// PhoneticEngine = NameType + RuleType + concat
// we use common-codec's defaults: GENERIC + APPROX + true