Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/main/java/org/apache/commons/codec/binary/BinaryCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/apache/commons/codec/binary/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/apache/commons/codec/cli/Digest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/apache/commons/codec/language/bm/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ public static final class Phoneme implements PhonemeExpr {
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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<NameType, Map<RuleType, Map<String, Map<String, List<Rule>>>>> RULES =
new EnumMap<>(NameType.class);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -419,7 +427,7 @@ private static Map<String, List<Rule>> 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);
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class RFC1522Codec {
* 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
Expand Down Expand Up @@ -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.
* <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
Expand All @@ -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.
* <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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/apache/commons/codec/binary/HexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/org/apache/commons/codec/cli/DigestTest.java
Original file line number Diff line number Diff line change
@@ -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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a local var?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. fixit

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public void testCompatibilityWithOriginalVersion() {
* 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
Expand Down