This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git


The following commit(s) were added to refs/heads/master by this push:
     new 868af384 Internal refactoring
868af384 is described below

commit 868af3848cd8807a21a3d6d3c1831feb9d9d74d4
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jul 9 12:22:49 2026 -0400

    Internal refactoring
---
 .../org/apache/commons/codec/binary/Base58.java    | 13 +---
 .../org/apache/commons/codec/binary/Base64.java    | 18 +-----
 .../apache/commons/codec/binary/BaseNCodec.java    | 20 ++++++
 ...Test.java => BaseNCodecToIntegerBytesTest.java} | 72 +++++++++++-----------
 4 files changed, 61 insertions(+), 62 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/binary/Base58.java 
b/src/main/java/org/apache/commons/codec/binary/Base58.java
index 480f98fc..b7851a39 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base58.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base58.java
@@ -93,8 +93,6 @@ public class Base58 extends BaseNCodec {
     private static final int DECODING_TABLE_LENGTH = 256;
     private static final int ENCODING_TABLE_LENGTH = 58;
 
-    private static final byte[] EMPTY = {};
-
     /**
      * Base58 alphabet: 
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
      * (excludes: 0, I, O, l).
@@ -203,13 +201,13 @@ public class Base58 extends BaseNCodec {
         }
         if (length < 0) {
             context.eof = true;
-            final byte[] accumulate = context.buffer = context.buffer != null 
? context.buffer : EMPTY;
+            final byte[] accumulate = context.buffer = context.buffer != null 
? context.buffer : BaseNCodec.EMPTY_BYTE_ARRAY;
             if (accumulate.length > 0) {
                 consumer.accept(accumulate, context);
             }
             return;
         }
-        final byte[] accumulate = context.buffer = context.buffer != null ? 
context.buffer : EMPTY;
+        final byte[] accumulate = context.buffer = context.buffer != null ? 
context.buffer : BaseNCodec.EMPTY_BYTE_ARRAY;
         final byte[] newAccumulated = new byte[accumulate.length + length];
         if (accumulate.length > 0) {
             System.arraycopy(accumulate, 0, newAccumulated, 0, 
accumulate.length);
@@ -249,12 +247,7 @@ public class Base58 extends BaseNCodec {
             value = value.add(BigInteger.valueOf(digit).multiply(power));
             power = power.multiply(BASE);
         }
-        byte[] decoded = value.equals(BigInteger.ZERO) ? EMPTY : 
value.toByteArray();
-        if (decoded.length > 1 && decoded[0] == 0) {
-            final byte[] tmp = new byte[decoded.length - 1];
-            System.arraycopy(decoded, 1, tmp, 0, tmp.length);
-            decoded = tmp;
-        }
+        final byte[] decoded = toUnsignedBytes(value);
         final byte[] result = new byte[leadingZeros + decoded.length];
         System.arraycopy(decoded, 0, result, leadingZeros, decoded.length);
         final byte[] buffer = ensureBufferSize(result.length, context);
diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java 
b/src/main/java/org/apache/commons/codec/binary/Base64.java
index 2e899fa9..511fa5dd 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -614,7 +614,7 @@ public class Base64 extends BaseNCodec {
      */
     public static byte[] encodeInteger(final BigInteger bigInteger) {
         Objects.requireNonNull(bigInteger, "bigInteger");
-        return encodeBase64(toIntegerBytes(bigInteger), false);
+        return encodeBase64(toUnsignedBytes(bigInteger), false);
     }
 
     /**
@@ -787,22 +787,6 @@ public class Base64 extends BaseNCodec {
         return isBase64Url(StringUtils.getBytesUtf8(base64));
     }
 
-    /**
-     * Returns a byte-array representation of a {@code BigInteger} without 
sign bit.
-     *
-     * @param bigInt {@code BigInteger} to be converted.
-     * @return a byte array representation of the BigInteger parameter.
-     */
-    static byte[] toIntegerBytes(final BigInteger bigInt) {
-        byte[] unsigned = bigInt.toByteArray();
-        if (unsigned[0] == 0 && unsigned.length != 1) {
-            final byte[] tmp = new byte[unsigned.length - 1];
-            System.arraycopy(unsigned, 1, tmp, 0, tmp.length);
-            unsigned = tmp;
-        }
-        return unsigned;
-    }
-
     static byte[] toUrlSafeEncodeTable(final boolean urlSafe) {
         return urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
     }
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 bf32569d..132dd1e6 100644
--- a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
+++ b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
@@ -17,6 +17,7 @@
 
 package org.apache.commons.codec.binary;
 
+import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Objects;
 import java.util.function.Supplier;
@@ -479,6 +480,25 @@ public abstract class BaseNCodec implements BinaryEncoder, 
BinaryDecoder {
         return b;
     }
 
+    /**
+     * Returns a byte-array representation of a {@code BigInteger} without 
sign bit.
+     * <p>
+     * The value {@link BigInteger#ZERO} maps to an empty array.
+     * </p>
+     *
+     * @param bigInt {@code BigInteger} to be converted.
+     * @return a byte array representation of the BigInteger parameter.
+     */
+    static byte[] toUnsignedBytes(final BigInteger value) {
+        byte[] unsigned = value.equals(BigInteger.ZERO) ? EMPTY_BYTE_ARRAY : 
value.toByteArray();
+        if (unsigned.length > 0 && unsigned[0] == 0) {
+            final byte[] tmp = new byte[unsigned.length - 1];
+            System.arraycopy(unsigned, 1, tmp, 0, tmp.length);
+            unsigned = tmp;
+        }
+        return unsigned;
+    }
+
     /**
      * Deprecated: Will be removed in 2.0.
      * <p>
diff --git 
a/src/test/java/org/apache/commons/codec/binary/Base64ToIntegerBytesTest.java 
b/src/test/java/org/apache/commons/codec/binary/BaseNCodecToIntegerBytesTest.java
similarity index 78%
rename from 
src/test/java/org/apache/commons/codec/binary/Base64ToIntegerBytesTest.java
rename to 
src/test/java/org/apache/commons/codec/binary/BaseNCodecToIntegerBytesTest.java
index 68d3bd0d..bd87af54 100644
--- 
a/src/test/java/org/apache/commons/codec/binary/Base64ToIntegerBytesTest.java
+++ 
b/src/test/java/org/apache/commons/codec/binary/BaseNCodecToIntegerBytesTest.java
@@ -25,17 +25,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.math.BigInteger;
 
+import org.apache.commons.lang3.ArrayUtils;
 import org.junit.jupiter.api.Test;
 
-class Base64ToIntegerBytesTest {
-
-    private static final byte[] ZERO_BYTE_ARRAY = new byte[] { 0 };
+/**
+ * Tests {@link BaseNCodec#toUnsignedBytes(BigInteger)}.
+ */
+class BaseNCodecToIntegerBytesTest {
 
     @Test
     void testBigIntegerWithAllZeros() {
         final BigInteger allZeros = new BigInteger("0");
-        final byte[] result = Base64.toIntegerBytes(allZeros);
-        assertArrayEquals(ZERO_BYTE_ARRAY, result);
+        final byte[] result = BaseNCodec.toUnsignedBytes(allZeros);
+        assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, result);
     }
 
     @Test
@@ -44,7 +46,7 @@ class Base64ToIntegerBytesTest {
         final BigInteger[] boundaries = { 
BigInteger.valueOf(Integer.MAX_VALUE), BigInteger.valueOf(Integer.MIN_VALUE), 
BigInteger.valueOf(Long.MAX_VALUE),
                 BigInteger.valueOf(Long.MIN_VALUE) };
         for (final BigInteger boundary : boundaries) {
-            assertNotNull(Base64.toIntegerBytes(boundary), "Should handle 
boundary value: " + boundary);
+            assertNotNull(BaseNCodec.toUnsignedBytes(boundary), "Should handle 
boundary value: " + boundary);
         }
     }
 
@@ -57,14 +59,14 @@ class Base64ToIntegerBytesTest {
                 BigInteger.valueOf(-256) // -256
         };
         for (final BigInteger boundary : boundaryValues) {
-            assertNotNull(Base64.toIntegerBytes(boundary), "Should handle 
boundary value: " + boundary);
+            assertNotNull(BaseNCodec.toUnsignedBytes(boundary), "Should handle 
boundary value: " + boundary);
         }
     }
 
     @Test
     void testBigIntegerWithDecimalString() {
         final BigInteger decimal = new BigInteger("100");
-        final byte[] result = Base64.toIntegerBytes(decimal);
+        final byte[] result = BaseNCodec.toUnsignedBytes(decimal);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -73,7 +75,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithHexValues() {
         // Test with hex string representation
         final BigInteger hexValue = new BigInteger("FF", 16); // 255
-        final byte[] result = Base64.toIntegerBytes(hexValue);
+        final byte[] result = BaseNCodec.toUnsignedBytes(hexValue);
         assertNotNull(result);
         assertEquals(1, result.length);
         assertEquals(-1, result[0]); // -1 in two's complement
@@ -83,7 +85,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithLargeValues() {
         // Test very large numbers that exceed typical integer ranges
         final BigInteger huge = new BigInteger("9223372036854775807"); // 
Long.MAX_VALUE
-        final byte[] result = Base64.toIntegerBytes(huge);
+        final byte[] result = BaseNCodec.toUnsignedBytes(huge);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -92,7 +94,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithLeadingZeros() {
         // Create a BigInteger that would have leading zeros in byte 
representation
         final BigInteger big = new 
BigInteger("00000000000000000000000000000001", 16);
-        final byte[] result = Base64.toIntegerBytes(big);
+        final byte[] result = BaseNCodec.toUnsignedBytes(big);
         assertNotNull(result);
         assertEquals(1, result.length);
         assertEquals(1, result[0]);
@@ -101,7 +103,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testBigIntegerWithMaxValue() {
         final BigInteger max = BigInteger.valueOf(Long.MAX_VALUE);
-        final byte[] result = Base64.toIntegerBytes(max);
+        final byte[] result = BaseNCodec.toUnsignedBytes(max);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -109,7 +111,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testBigIntegerWithMinValue() {
         final BigInteger min = BigInteger.valueOf(Long.MIN_VALUE);
-        final byte[] result = Base64.toIntegerBytes(min);
+        final byte[] result = BaseNCodec.toUnsignedBytes(min);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -120,7 +122,7 @@ class Base64ToIntegerBytesTest {
         final BigInteger[] negativeValues = { BigInteger.valueOf(-1), 
BigInteger.valueOf(-256), BigInteger.valueOf(Integer.MIN_VALUE),
                 new BigInteger("-999999999999999999") };
         for (final BigInteger negative : negativeValues) {
-            assertNotNull(Base64.toIntegerBytes(negative), "Should handle 
negative value: " + negative);
+            assertNotNull(BaseNCodec.toUnsignedBytes(negative), "Should handle 
negative value: " + negative);
         }
     }
 
@@ -128,7 +130,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithMultipleBytes() {
         // Test a number that requires multiple bytes to represent
         final BigInteger multiByte = new BigInteger("65536"); // 2^16
-        final byte[] result = Base64.toIntegerBytes(multiByte);
+        final byte[] result = BaseNCodec.toUnsignedBytes(multiByte);
         assertNotNull(result);
         assertTrue(result.length >= 2);
         // Should correctly represent the multi-byte value
@@ -138,7 +140,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithNegativeHexValues() {
         // Test with negative hex values
         final BigInteger hexNeg = new BigInteger("80", 16); // -128 in two's 
complement
-        final byte[] result = Base64.toIntegerBytes(hexNeg);
+        final byte[] result = BaseNCodec.toUnsignedBytes(hexNeg);
         assertNotNull(result);
         assertEquals(1, result.length);
         assertEquals(-128, result[0]);
@@ -147,7 +149,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testBigIntegerWithNegativeLargeValues() {
         final BigInteger hugeNeg = new BigInteger("-9223372036854775808"); // 
Long.MIN_VALUE
-        final byte[] result = Base64.toIntegerBytes(hugeNeg);
+        final byte[] result = BaseNCodec.toUnsignedBytes(hugeNeg);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -156,7 +158,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithNegativeVeryLargeNumber() {
         final String largeString = "-1234567890123456789012345678901234567890";
         final BigInteger large = new BigInteger(largeString);
-        final byte[] result = Base64.toIntegerBytes(large);
+        final byte[] result = BaseNCodec.toUnsignedBytes(large);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -165,7 +167,7 @@ class Base64ToIntegerBytesTest {
     void testBigIntegerWithOverflowProtection() {
         // Test that the method handles overflow conditions gracefully
         final BigInteger overflowTest = new 
BigInteger("12345678901234567890123456789012345678901234567890");
-        final byte[] result = Base64.toIntegerBytes(overflowTest);
+        final byte[] result = BaseNCodec.toUnsignedBytes(overflowTest);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -182,7 +184,7 @@ class Base64ToIntegerBytesTest {
                 BigInteger.valueOf(127) // 127 (maximum byte value)
         };
         for (final BigInteger value : testValues) {
-            final byte[] result = Base64.toIntegerBytes(value);
+            final byte[] result = BaseNCodec.toUnsignedBytes(value);
             assertNotNull(result, "Should not return null for value: " + 
value);
         }
     }
@@ -192,7 +194,7 @@ class Base64ToIntegerBytesTest {
         // Test with numbers that have many digits
         final String largeString = "1234567890123456789012345678901234567890";
         final BigInteger large = new BigInteger(largeString);
-        final byte[] result = Base64.toIntegerBytes(large);
+        final byte[] result = BaseNCodec.toUnsignedBytes(large);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -200,7 +202,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testBigIntegerWithVeryLargePositiveValue() {
         final BigInteger veryLarge = new 
BigInteger("123456789012345678901234567890");
-        final byte[] result = Base64.toIntegerBytes(veryLarge);
+        final byte[] result = BaseNCodec.toUnsignedBytes(veryLarge);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -208,7 +210,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testBigIntegerWithVerySmallNegativeValue() {
         final BigInteger verySmall = new BigInteger("-256");
-        final byte[] result = Base64.toIntegerBytes(verySmall);
+        final byte[] result = BaseNCodec.toUnsignedBytes(verySmall);
         assertNotNull(result);
         assertTrue(result.length > 0);
     }
@@ -219,19 +221,19 @@ class Base64ToIntegerBytesTest {
         final BigInteger zero1 = BigInteger.ZERO;
         final BigInteger zero2 = new BigInteger("0");
         final BigInteger zero3 = new BigInteger("0000000000000000");
-        final byte[] result1 = Base64.toIntegerBytes(zero1);
-        final byte[] result2 = Base64.toIntegerBytes(zero2);
-        final byte[] result3 = Base64.toIntegerBytes(zero3);
-        assertArrayEquals(ZERO_BYTE_ARRAY, result1);
-        assertArrayEquals(ZERO_BYTE_ARRAY, result2);
-        assertArrayEquals(ZERO_BYTE_ARRAY, result3);
+        final byte[] result1 = BaseNCodec.toUnsignedBytes(zero1);
+        final byte[] result2 = BaseNCodec.toUnsignedBytes(zero2);
+        final byte[] result3 = BaseNCodec.toUnsignedBytes(zero3);
+        assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, result1);
+        assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, result2);
+        assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, result3);
     }
 
 
     @Test
     void testLargeNegativeBigInteger() {
         final BigInteger large = new BigInteger("-2147483648"); // 
Integer.MIN_VALUE
-        final byte[] result = Base64.toIntegerBytes(large);
+        final byte[] result = BaseNCodec.toUnsignedBytes(large);
         assertNotNull(result);
         // Should handle large negative numbers correctly
         assertTrue(result.length > 0);
@@ -240,7 +242,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testLargePositiveBigInteger() {
         final BigInteger large = new BigInteger("2147483647"); // 
Integer.MAX_VALUE
-        final byte[] result = Base64.toIntegerBytes(large);
+        final byte[] result = BaseNCodec.toUnsignedBytes(large);
         assertNotNull(result);
         // Should handle large positive numbers correctly
         assertTrue(result.length > 0);
@@ -249,14 +251,14 @@ class Base64ToIntegerBytesTest {
     @Test
     void testNullBigInteger() {
         assertThrows(NullPointerException.class, () -> {
-            Base64.toIntegerBytes(null);
+            BaseNCodec.toUnsignedBytes(null);
         });
     }
 
     @Test
     void testOneBigInteger() {
         final BigInteger one = BigInteger.ONE;
-        final byte[] result = Base64.toIntegerBytes(one);
+        final byte[] result = BaseNCodec.toUnsignedBytes(one);
         assertNotNull(result);
         // Should represent the integer value 1
         assertEquals(1, result.length);
@@ -266,7 +268,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testSmallNegativeBigInteger() {
         final BigInteger small = new BigInteger("-128");
-        final byte[] result = Base64.toIntegerBytes(small);
+        final byte[] result = BaseNCodec.toUnsignedBytes(small);
         assertNotNull(result);
         assertEquals(1, result.length);
         assertEquals(-128, result[0]);
@@ -275,7 +277,7 @@ class Base64ToIntegerBytesTest {
     @Test
     void testSmallPositiveBigInteger() {
         final BigInteger small = new BigInteger("127");
-        final byte[] result = Base64.toIntegerBytes(small);
+        final byte[] result = BaseNCodec.toUnsignedBytes(small);
         assertNotNull(result);
         assertEquals(1, result.length);
         assertEquals(127, result[0]);

Reply via email to