From 5f0cfd46c89df69b579f37562ff1eded7ffd4b5c Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 20 Apr 2024 13:56:58 -0400 Subject: [PATCH] Longer lines - Whitespace - Simplify if --- .../apache/commons/codec/binary/Base16.java | 29 ++----------------- .../apache/commons/codec/binary/Base32.java | 2 -- .../commons/codec/binary/BaseNCodec.java | 24 ++++++--------- 3 files changed, 11 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/apache/commons/codec/binary/Base16.java b/src/main/java/org/apache/commons/codec/binary/Base16.java index c3dfe8180d..2968944215 100644 --- a/src/main/java/org/apache/commons/codec/binary/Base16.java +++ b/src/main/java/org/apache/commons/codec/binary/Base16.java @@ -68,12 +68,7 @@ public class Base16 extends BaseNCodec { * This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" equivalents as specified in Table 5 of RFC * 4648. */ - // @formatter:off - private static final byte[] UPPER_CASE_ENCODE_TABLE = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'A', 'B', 'C', 'D', 'E', 'F' - }; - // @formatter:on + private static final byte[] UPPER_CASE_ENCODE_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * This array is a lookup table that translates Unicode characters drawn from the a lower-case "Base16 Alphabet" into their 4-bit positive integer @@ -95,12 +90,7 @@ public class Base16 extends BaseNCodec { /** * This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" lower-case equivalents. */ - // @formatter:off - private static final byte[] LOWER_CASE_ENCODE_TABLE = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f' - }; - // @formatter:on + private static final byte[] LOWER_CASE_ENCODE_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** Mask used to extract 4 bits, used when decoding character. */ private static final int MASK_4BITS = 0x0f; @@ -164,42 +154,33 @@ void decode(final byte[] data, int offset, final int length, final Context conte } return; } - final int dataLen = Math.min(data.length - offset, length); final int availableChars = (context.ibitWorkArea != 0 ? 1 : 0) + dataLen; - // small optimization to short-cut the rest of this method when it is fed byte-by-byte if (availableChars == 1 && availableChars == dataLen) { // store 1/2 byte for next invocation of decode, we offset by +1 as empty-value is 0 context.ibitWorkArea = decodeOctet(data[offset]) + 1; return; } - // we must have an even number of chars to decode final int charsToProcess = availableChars % BYTES_PER_ENCODED_BLOCK == 0 ? availableChars : availableChars - 1; final int end = offset + dataLen; - final byte[] buffer = ensureBufferSize(charsToProcess / BYTES_PER_ENCODED_BLOCK, context); - int result; if (dataLen < availableChars) { // we have 1/2 byte from previous invocation to decode result = context.ibitWorkArea - 1 << BITS_PER_ENCODED_BYTE; result |= decodeOctet(data[offset++]); - buffer[context.pos++] = (byte) result; - // reset to empty-value for next invocation! context.ibitWorkArea = 0; } - final int loopEnd = end - 1; while (offset < loopEnd) { result = decodeOctet(data[offset++]) << BITS_PER_ENCODED_BYTE; result |= decodeOctet(data[offset++]); buffer[context.pos++] = (byte) result; } - // we have one char of a hex-pair left over if (offset < end) { // store 1/2 byte for next invocation of decode, we offset by +1 as empty-value is 0 @@ -212,11 +193,9 @@ private int decodeOctet(final byte octet) { if ((octet & 0xff) < decodeTable.length) { decoded = decodeTable[octet]; } - if (decoded == -1) { throw new IllegalArgumentException("Invalid octet in encoded value: " + (int) octet); } - return decoded; } @@ -225,19 +204,15 @@ void encode(final byte[] data, final int offset, final int length, final Context if (context.eof) { return; } - if (length < 0) { context.eof = true; return; } - final int size = length * BYTES_PER_ENCODED_BLOCK; if (size < 0) { throw new IllegalArgumentException("Input length exceeds maximum size for encoded data: " + length); } - final byte[] buffer = ensureBufferSize(size, context); - final int end = offset + length; for (int i = offset; i < end; i++) { final int value = data[i]; diff --git a/src/main/java/org/apache/commons/codec/binary/Base32.java b/src/main/java/org/apache/commons/codec/binary/Base32.java index ad39da408c..06ecec4736 100644 --- a/src/main/java/org/apache/commons/codec/binary/Base32.java +++ b/src/main/java/org/apache/commons/codec/binary/Base32.java @@ -437,7 +437,6 @@ void decode(final byte[] input, int inPos, final int inAvail, final Context cont // This approach makes the '=' padding characters completely optional. if (context.eof && context.modulus > 0) { // if modulus == 0, nothing to do final byte[] buffer = ensureBufferSize(decodeSize, context); - // We ignore partial bytes, i.e. only multiples of 8 count. // Any combination not part of a valid encoding is either partially decoded // or will raise an exception. Possible trailing characters are 2, 4, 5, 7. @@ -507,7 +506,6 @@ void decode(final byte[] input, int inPos, final int inAvail, final Context cont @Override void encode(final byte[] input, int inPos, final int inAvail, final Context context) { // package protected for access from I/O streams - if (context.eof) { return; } diff --git a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java index df68641a44..c598abc3d1 100644 --- a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java +++ b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java @@ -359,7 +359,6 @@ private static byte[] resizeBuffer(final Context context, final int minCapacity) if (Integer.compareUnsigned(newCapacity, MAX_BUFFER_SIZE) > 0) { newCapacity = createPositiveCapacity(minCapacity); } - final byte[] b = Arrays.copyOf(context.buffer, newCapacity); context.buffer = b; return b; @@ -435,8 +434,7 @@ static int toLength(final byte[] array) { * @param lineLength if > 0, use chunking with a length {@code lineLength} * @param chunkSeparatorLength the chunk separator length, if relevant */ - protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, - final int lineLength, final int chunkSeparatorLength) { + protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength) { this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, PAD_DEFAULT); } @@ -453,8 +451,7 @@ protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, * @param chunkSeparatorLength the chunk separator length, if relevant * @param pad byte used as padding byte. */ - protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, - final int lineLength, final int chunkSeparatorLength, final byte pad) { + protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad) { this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, pad, DECODING_POLICY_DEFAULT); } @@ -473,9 +470,8 @@ protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, * @param decodingPolicy Decoding policy. * @since 1.15 */ - protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, - final int lineLength, final int chunkSeparatorLength, final byte pad, - final CodecPolicy decodingPolicy) { + protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad, + final CodecPolicy decodingPolicy) { this.unencodedBlockSize = unencodedBlockSize; this.encodedBlockSize = encodedBlockSize; final boolean useChunking = lineLength > 0 && chunkSeparatorLength > 0; @@ -505,12 +501,11 @@ int available(final Context context) { // package protected for access from I/O * @return {@code true} if any byte is a valid character in the alphabet or PAD; {@code false} otherwise */ protected boolean containsAlphabetOrPad(final byte[] arrayOctet) { - if (arrayOctet == null) { - return false; - } - for (final byte element : arrayOctet) { - if (pad == element || isInAlphabet(element)) { - return true; + if (arrayOctet != null) { + for (final byte element : arrayOctet) { + if (pad == element || isInAlphabet(element)) { + return true; + } } } return false; @@ -672,7 +667,6 @@ protected byte[] ensureBufferSize(final int size, final Context context) { context.buffer = new byte[Math.max(size, getDefaultBufferSize())]; context.pos = 0; context.readPos = 0; - // Overflow-conscious: // x + y > z == x + y - z > 0 } else if (context.pos + size - context.buffer.length > 0) {