From 50f4f72afd0c9b978bea40b2282424f4be8ce09d Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 7 Dec 2020 15:54:43 +0100 Subject: [PATCH] CODEC-295 - Minor improvement --- .../commons/codec/binary/BinaryCodec.java | 20 +++++--- .../org/apache/commons/codec/binary/Hex.java | 8 +-- .../org/apache/commons/codec/cli/Digest.java | 7 +-- .../apache/commons/codec/digest/Md5Crypt.java | 2 +- .../commons/codec/digest/MurmurHash3.java | 4 +- .../commons/codec/digest/UnixCrypt.java | 3 +- .../commons/codec/language/Metaphone.java | 2 +- .../commons/codec/language/bm/Rule.java | 24 ++++++--- .../codec/net/QuotedPrintableCodec.java | 9 ++-- .../commons/codec/net/RFC1522Codec.java | 6 +-- .../commons/codec/binary/Base16Test.java | 2 +- .../commons/codec/binary/Base32Test.java | 3 +- .../commons/codec/binary/Base64Test.java | 2 +- .../apache/commons/codec/binary/HexTest.java | 1 - .../apache/commons/codec/cli/DigestTest.java | 50 +++++++++++++++++++ .../bm/PhoneticEngineRegressionTest.java | 4 +- 16 files changed, 105 insertions(+), 42 deletions(-) create mode 100644 src/test/java/org/apache/commons/codec/cli/DigestTest.java 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 590dd3e579..c7f383e2d3 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 @@ public static byte[] fromAscii(final byte[] ascii) { 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 @@ public static byte[] fromAscii(final char[] ascii) { 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 @@ public static byte[] toAsciiBytes(final byte[] raw) { 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 @@ public static char[] toAsciiChars(final byte[] raw) { 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 229495662d..7075ab9dd3 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 @@ public static char[] encodeHex(final byte[] data, final boolean toLowerCase) { * @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 @@ public byte[] encode(final ByteBuffer array) { */ @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 4e553cff96..9815034640 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 @@ private Digest(final String[] args) { 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 f607b88549..82011a2dfd 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 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St 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 6b471df4a7..0c28cff3a8 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 @@ public final void add(final byte[] data, final int offset, final int length) { } // 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 7be495860e..b77e4f3fe6 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 @@ public static String crypt(final byte[] original, String salt) { 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 84a85750cb..5ee6466492 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 Metaphone() { */ 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 644614aa0b..d55ce8c2fd 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 class Phoneme implements PhonemeExpr { public static final Comparator COMPARATOR = new Comparator() { @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 @@ public int compare(final Phoneme o1, final Phoneme o2) { } } - if (o1.phonemeText.length() < o2.phonemeText.length()) { + if (o1Length < o2Length) { return -1; } @@ -205,6 +207,9 @@ public boolean isMatch(final CharSequence input) { private static final String HASH_INCLUDE = "#include"; + private static final int HASH_INCLUDE_LENGTH = HASH_INCLUDE.length(); + + private static final Map>>>> RULES = new EnumMap<>(NameType.class); @@ -262,10 +267,13 @@ private static Scanner createScanner(final String lang) { } 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 @@ private static Map> parseRules(final Scanner scanner, final S 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 @@ private static RPattern pattern(final String regex) { if (!boxes) { if (startsWith && endsWith) { // exact match - if (content.length() == 0) { + if (content.isEmpty()) { // empty return new RPattern() { @Override @@ -507,7 +515,7 @@ public boolean isMatch(final CharSequence input) { 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 e18b298806..5641dc8fa3 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 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] 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 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] // 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 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] 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 90564fe36a..16bbcf48dc 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 @@ abstract class RFC1522Codec { * Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset. *

* 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 @@ protected String encodeText(final String text, final Charset charset) throws Enc * Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset. *

* 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 @@ protected String encodeText(final String text, final String charsetName) * Applies an RFC 1522 compliant decoding scheme to the given string of text. *

* 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 f60709df5a..baae38f1f0 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 @@ public void testBase16AtBufferMiddle() { 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 068f5f8775..475bb2f9c4 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 testBase32Samples() throws Exception { 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 a6e834be98..e4be6d01e4 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 @@ public void testBase64AtBufferMiddle() { 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 7f0fc1c814..bab875424b 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 0000000000..e5d279b228 --- /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 f04fe4d673..cb1a7ed81b 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 @@ public void testCompatibilityWithOriginalVersion() { * regressions in Commons-Codec. */ private static String encode(final Map 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