Skip to content

Commit 62d55fd

Browse files
CODEC-295 - Minor improvement (#67)
1 parent d720e87 commit 62d55fd

16 files changed

Lines changed: 105 additions & 42 deletions

File tree

src/main/java/org/apache/commons/codec/binary/BinaryCodec.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ public static byte[] fromAscii(final byte[] ascii) {
8080
if (isEmpty(ascii)) {
8181
return EMPTY_BYTE_ARRAY;
8282
}
83+
final int asciiLength = ascii.length;
8384
// get length/8 times bytes with 3 bit shifts to the right of the length
84-
final byte[] l_raw = new byte[ascii.length >> 3];
85+
final byte[] l_raw = new byte[asciiLength >> 3];
8586
/*
8687
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
8788
* loop.
8889
*/
89-
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
90+
for (int ii = 0, jj = asciiLength - 1; ii < l_raw.length; ii++, jj -= 8) {
9091
for (int bits = 0; bits < BITS.length; ++bits) {
9192
if (ascii[jj - bits] == '1') {
9293
l_raw[ii] |= BITS[bits];
@@ -112,13 +113,14 @@ public static byte[] fromAscii(final char[] ascii) {
112113
if (ascii == null || ascii.length == 0) {
113114
return EMPTY_BYTE_ARRAY;
114115
}
116+
final int asciiLength = ascii.length;
115117
// get length/8 times bytes with 3 bit shifts to the right of the length
116-
final byte[] l_raw = new byte[ascii.length >> 3];
118+
final byte[] l_raw = new byte[asciiLength >> 3];
117119
/*
118120
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
119121
* loop.
120122
*/
121-
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
123+
for (int ii = 0, jj = asciiLength - 1; ii < l_raw.length; ii++, jj -= 8) {
122124
for (int bits = 0; bits < BITS.length; ++bits) {
123125
if (ascii[jj - bits] == '1') {
124126
l_raw[ii] |= BITS[bits];
@@ -152,13 +154,14 @@ public static byte[] toAsciiBytes(final byte[] raw) {
152154
if (isEmpty(raw)) {
153155
return EMPTY_BYTE_ARRAY;
154156
}
157+
final int rawLength = raw.length;
155158
// get 8 times the bytes with 3 bit shifts to the left of the length
156-
final byte[] l_ascii = new byte[raw.length << 3];
159+
final byte[] l_ascii = new byte[rawLength << 3];
157160
/*
158161
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
159162
* loop.
160163
*/
161-
for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
164+
for (int ii = 0, jj = l_ascii.length - 1; ii < rawLength; ii++, jj -= 8) {
162165
for (int bits = 0; bits < BITS.length; ++bits) {
163166
if ((raw[ii] & BITS[bits]) == 0) {
164167
l_ascii[jj - bits] = '0';
@@ -182,13 +185,14 @@ public static char[] toAsciiChars(final byte[] raw) {
182185
if (isEmpty(raw)) {
183186
return EMPTY_CHAR_ARRAY;
184187
}
188+
final int rawLength = raw.length;
185189
// get 8 times the bytes with 3 bit shifts to the left of the length
186-
final char[] l_ascii = new char[raw.length << 3];
190+
final char[] l_ascii = new char[rawLength << 3];
187191
/*
188192
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
189193
* loop.
190194
*/
191-
for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
195+
for (int ii = 0, jj = l_ascii.length - 1; ii < rawLength; ii++, jj -= 8) {
192196
for (int bits = 0; bits < BITS.length; ++bits) {
193197
if ((raw[ii] & BITS[bits]) == 0) {
194198
l_ascii[jj - bits] = '0';

src/main/java/org/apache/commons/codec/binary/Hex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
166166
* @since 1.4
167167
*/
168168
protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
169-
final int l = data.length;
170-
final char[] out = new char[l << 1];
171-
encodeHex(data, 0, data.length, toDigits, out, 0);
169+
final int dataLength = data.length;
170+
final char[] out = new char[dataLength << 1];
171+
encodeHex(data, 0, dataLength, toDigits, out, 0);
172172
return out;
173173
}
174174

@@ -519,7 +519,7 @@ public byte[] encode(final ByteBuffer array) {
519519
*/
520520
@Override
521521
public Object encode(final Object object) throws EncoderException {
522-
byte[] byteArray;
522+
final byte[] byteArray;
523523
if (object instanceof String) {
524524
byteArray = ((String) object).getBytes(this.getCharset());
525525
} else if (object instanceof ByteBuffer) {

src/main/java/org/apache/commons/codec/cli/Digest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ private Digest(final String[] args) {
6161
if (args == null) {
6262
throw new IllegalArgumentException("args");
6363
}
64-
if (args.length == 0) {
64+
final int argsLength = args.length;
65+
if (argsLength == 0) {
6566
throw new IllegalArgumentException(
6667
String.format("Usage: java %s [algorithm] [FILE|DIRECTORY|string] ...", Digest.class.getName()));
6768
}
6869
this.args = args;
6970
algorithm = args[0];
70-
if (args.length <= 1) {
71+
if (argsLength <= 1) {
7172
inputs = null;
7273
} else {
73-
inputs = new String[args.length -1];
74+
inputs = new String[argsLength - 1];
7475
System.arraycopy(args, 1, inputs, 0, inputs.length);
7576
}
7677
}

src/main/java/org/apache/commons/codec/digest/Md5Crypt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St
283283
final int keyLen = keyBytes.length;
284284

285285
// Extract the real salt from the given string which can be a complete hash string.
286-
String saltString;
286+
final String saltString;
287287
if (salt == null) {
288288
saltString = B64.getRandomSalt(8, random);
289289
} else {

src/main/java/org/apache/commons/codec/digest/MurmurHash3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,8 @@ public final void add(final byte[] data, final int offset, final int length) {
10781078
}
10791079

10801080
// Combine unprocessed bytes with new bytes.
1081-
int newOffset;
1082-
int newLength;
1081+
final int newOffset;
1082+
final int newLength;
10831083
if (unprocessedLength > 0) {
10841084
int k = -1;
10851085
switch (unprocessedLength) {

src/main/java/org/apache/commons/codec/digest/UnixCrypt.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public static String crypt(final byte[] original, String salt) {
220220
final byte key[] = new byte[8];
221221
Arrays.fill(key, (byte) 0);
222222

223-
for (int i = 0; i < key.length && i < original.length; i++) {
223+
final int originalLength = original.length;
224+
for (int i = 0; i < key.length && i < originalLength; i++) {
224225
final int iChar = original[i];
225226
key[i] = (byte) (iChar << 1);
226227
}

src/main/java/org/apache/commons/codec/language/Metaphone.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Metaphone() {
8989
*/
9090
public String metaphone(final String txt) {
9191
boolean hard = false;
92-
int txtLength;
92+
final int txtLength;
9393
if (txt == null || (txtLength = txt.length()) == 0) {
9494
return "";
9595
}

src/main/java/org/apache/commons/codec/language/bm/Rule.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ public static final class Phoneme implements PhonemeExpr {
8484
public static final Comparator<Phoneme> COMPARATOR = new Comparator<Phoneme>() {
8585
@Override
8686
public int compare(final Phoneme o1, final Phoneme o2) {
87-
for (int i = 0; i < o1.phonemeText.length(); i++) {
88-
if (i >= o2.phonemeText.length()) {
87+
final int o1Length = o1.phonemeText.length();
88+
final int o2Length = o2.phonemeText.length();
89+
for (int i = 0; i < o1Length; i++) {
90+
if (i >= o2Length) {
8991
return +1;
9092
}
9193
final int c = o1.phonemeText.charAt(i) - o2.phonemeText.charAt(i);
@@ -94,7 +96,7 @@ public int compare(final Phoneme o1, final Phoneme o2) {
9496
}
9597
}
9698

97-
if (o1.phonemeText.length() < o2.phonemeText.length()) {
99+
if (o1Length < o2Length) {
98100
return -1;
99101
}
100102

@@ -205,6 +207,9 @@ public boolean isMatch(final CharSequence input) {
205207

206208
private static final String HASH_INCLUDE = "#include";
207209

210+
private static final int HASH_INCLUDE_LENGTH = HASH_INCLUDE.length();
211+
212+
208213
private static final Map<NameType, Map<RuleType, Map<String, Map<String, List<Rule>>>>> RULES =
209214
new EnumMap<>(NameType.class);
210215

@@ -262,10 +267,13 @@ private static Scanner createScanner(final String lang) {
262267
}
263268

264269
private static boolean endsWith(final CharSequence input, final CharSequence suffix) {
265-
if (suffix.length() > input.length()) {
270+
final int suffixLength = suffix.length();
271+
final int inputLength = input.length();
272+
273+
if (suffixLength > inputLength) {
266274
return false;
267275
}
268-
for (int i = input.length() - 1, j = suffix.length() - 1; j >= 0; i--, j--) {
276+
for (int i = inputLength - 1, j = suffixLength - 1; j >= 0; i--, j--) {
269277
if (input.charAt(i) != suffix.charAt(j)) {
270278
return false;
271279
}
@@ -419,7 +427,7 @@ private static Map<String, List<Rule>> parseRules(final Scanner scanner, final S
419427

420428
if (line.startsWith(HASH_INCLUDE)) {
421429
// include statement
422-
final String incl = line.substring(HASH_INCLUDE.length()).trim();
430+
final String incl = line.substring(HASH_INCLUDE_LENGTH).trim();
423431
if (incl.contains(" ")) {
424432
throw new IllegalArgumentException("Malformed import statement '" + rawLine + "' in " +
425433
location);
@@ -492,7 +500,7 @@ private static RPattern pattern(final String regex) {
492500
if (!boxes) {
493501
if (startsWith && endsWith) {
494502
// exact match
495-
if (content.length() == 0) {
503+
if (content.isEmpty()) {
496504
// empty
497505
return new RPattern() {
498506
@Override
@@ -507,7 +515,7 @@ public boolean isMatch(final CharSequence input) {
507515
return input.equals(content);
508516
}
509517
};
510-
} else if ((startsWith || endsWith) && content.length() == 0) {
518+
} else if ((startsWith || endsWith) && content.isEmpty()) {
511519
// matches every string
512520
return ALL_STRINGS_RMATCHER;
513521
} else if (startsWith) {

src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,13 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[]
282282
printable = PRINTABLE_CHARS;
283283
}
284284
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
285+
final int bytesLength = bytes.length;
285286

286287
if (strict) {
287288
int pos = 1;
288289
// encode up to buffer.length - 3, the last three octets will be treated
289290
// separately for simplification of note #3
290-
for (int i = 0; i < bytes.length - 3; i++) {
291+
for (int i = 0; i < bytesLength - 3; i++) {
291292
final int b = getUnsignedOctet(i, bytes);
292293
if (pos < SAFE_LENGTH) {
293294
// 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[]
306307

307308
// rule #3: whitespace at the end of a line *must* be encoded
308309
// if we would do a soft break line after this octet, encode whitespace
309-
int b = getUnsignedOctet(bytes.length - 3, bytes);
310+
int b = getUnsignedOctet(bytesLength - 3, bytes);
310311
boolean encode = !printable.get(b) || (isWhitespace(b) && pos > SAFE_LENGTH - 5);
311312
pos += encodeByte(b, encode, buffer);
312313

@@ -318,10 +319,10 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[]
318319
buffer.write(CR);
319320
buffer.write(LF);
320321
}
321-
for (int i = bytes.length - 2; i < bytes.length; i++) {
322+
for (int i = bytesLength - 2; i < bytesLength; i++) {
322323
b = getUnsignedOctet(i, bytes);
323324
// rule #3: trailing whitespace shall be encoded
324-
encode = !printable.get(b) || (i > bytes.length - 2 && isWhitespace(b));
325+
encode = !printable.get(b) || (i > bytesLength - 2 && isWhitespace(b));
325326
encodeByte(b, encode, buffer);
326327
}
327328
} else {

src/main/java/org/apache/commons/codec/net/RFC1522Codec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class RFC1522Codec {
5555
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.
5656
* <p>
5757
* This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes
58-
* {@link #doEncoding(byte [])} method of a concrete class to perform the specific encoding.
58+
* {@link #doEncoding(byte[])} method of a concrete class to perform the specific encoding.
5959
*
6060
* @param text
6161
* a string to encode
@@ -85,7 +85,7 @@ protected String encodeText(final String text, final Charset charset) throws Enc
8585
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.
8686
* <p>
8787
* This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes
88-
* {@link #doEncoding(byte [])} method of a concrete class to perform the specific encoding.
88+
* {@link #doEncoding(byte[])} method of a concrete class to perform the specific encoding.
8989
*
9090
* @param text
9191
* a string to encode
@@ -111,7 +111,7 @@ protected String encodeText(final String text, final String charsetName)
111111
* Applies an RFC 1522 compliant decoding scheme to the given string of text.
112112
* <p>
113113
* This method processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
114-
* {@link #doEncoding(byte [])} method of a concrete class to perform the specific decoding.
114+
* {@link #doDecoding(byte[])} method of a concrete class to perform the specific decoding.
115115
*
116116
* @param text
117117
* a string to decode

0 commit comments

Comments
 (0)