Skip to content

Commit

Permalink
Longer lines
Browse files Browse the repository at this point in the history
- Whitespace
- Simplify if
  • Loading branch information
garydgregory committed Apr 20, 2024
1 parent 8714b5f commit 5f0cfd4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 44 deletions.
29 changes: 2 additions & 27 deletions src/main/java/org/apache/commons/codec/binary/Base16.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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];
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/apache/commons/codec/binary/Base32.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -435,8 +434,7 @@ static int toLength(final byte[] array) {
* @param lineLength if &gt; 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);
}

Expand All @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5f0cfd4

Please sign in to comment.