This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit a795aa9662aac989e2b3ab913d56b6f01711d23d Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Dec 7 16:08:51 2024 -0500 Use BigInteger.longValueExact() instead of a magic number (Java 8 API) - Avoids computing the bit length on every call when usually not needed - Better method name - Better Javadoc --- .../compress/archivers/zip/X7875_NewUnix.java | 4 ++-- .../commons/compress/archivers/zip/ZipUtil.java | 27 +++++++++++----------- .../compress/archivers/zip/ZipUtilTest.java | 10 ++++---- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/X7875_NewUnix.java b/src/main/java/org/apache/commons/compress/archivers/zip/X7875_NewUnix.java index 43a02ab3a..567af5116 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/X7875_NewUnix.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/X7875_NewUnix.java @@ -174,7 +174,7 @@ public class X7875_NewUnix implements ZipExtraField, Cloneable, Serializable { * @return the GID value. */ public long getGID() { - return ZipUtil.bigToLong(gid); + return ZipUtil.toLong(gid); } /** @@ -257,7 +257,7 @@ public class X7875_NewUnix implements ZipExtraField, Cloneable, Serializable { * @return the UID value. */ public long getUID() { - return ZipUtil.bigToLong(uid); + return ZipUtil.toLong(uid); } @Override diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java index cc141e96d..1658e7862 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java @@ -112,19 +112,6 @@ public abstract class ZipUtil { return i; } - /** - * Converts a BigInteger into a long, and blows up (NumberFormatException) if the BigInteger is too big. - * - * @param big BigInteger to convert. - * @return long representation of the BigInteger. - */ - static long bigToLong(final BigInteger big) { - if (big.bitLength() <= 63) { // bitLength() doesn't count the sign bit. - return big.longValue(); - } - throw new NumberFormatException("The BigInteger cannot fit inside a 64 bit java long: [" + big + "]"); - } - /** * Tests if this library is able to read or write the given entry. */ @@ -389,6 +376,20 @@ public abstract class ZipUtil { ZipLong.putLong(javaToDosTime(t), buf, offset); } + /** + * Converts a BigInteger to a long, and throws a NumberFormatException if the BigInteger is too big. + * + * @param big BigInteger to convert. + * @return {@code BigInteger} converted to a {@code long}. + */ + static long toLong(final BigInteger big) { + try { + return big.longValueExact(); + } catch (final ArithmeticException e) { + throw new NumberFormatException("The BigInteger cannot fit inside a 64 bit java long: [" + big + "]"); + } + } + /** * Converts an unsigned integer to a signed byte (e.g., 255 becomes -1). * diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java index 8d4961575..b4fa80de3 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java @@ -91,15 +91,15 @@ public class ZipUtilTest { final BigInteger big2 = BigInteger.valueOf(Long.MAX_VALUE); final BigInteger big3 = BigInteger.valueOf(Long.MIN_VALUE); - assertEquals(1L, ZipUtil.bigToLong(big1)); - assertEquals(Long.MAX_VALUE, ZipUtil.bigToLong(big2)); - assertEquals(Long.MIN_VALUE, ZipUtil.bigToLong(big3)); + assertEquals(1L, ZipUtil.toLong(big1)); + assertEquals(Long.MAX_VALUE, ZipUtil.toLong(big2)); + assertEquals(Long.MIN_VALUE, ZipUtil.toLong(big3)); final BigInteger big4 = big2.add(big1); - assertThrows(IllegalArgumentException.class, () -> ZipUtil.bigToLong(big4), "Should have thrown IllegalArgumentException"); + assertThrows(IllegalArgumentException.class, () -> ZipUtil.toLong(big4), "Should have thrown IllegalArgumentException"); final BigInteger big5 = big3.subtract(big1); - assertThrows(IllegalArgumentException.class, () -> ZipUtil.bigToLong(big5), + assertThrows(IllegalArgumentException.class, () -> ZipUtil.toLong(big5), "ZipUtil.bigToLong(BigInteger) should have thrown IllegalArgumentException"); }