You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/08/02 10:57:00 UTC

(commons-compress) 02/02: Fix kilobyte to kibibyte conversion

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 f58f3759492981a46bea4d2631c23d16734346ca
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Aug 2 06:56:52 2025 -0400

    Fix kilobyte to kibibyte conversion
    
    - Fix kilobyte to kibibyte conversion in
    org.apache.commons.compress.archivers.sevenz.SevenZFile.ArchiveStatistics.assertValidity(int)
    - Fix kilobyte to kibibyte conversion in
    org.apache.commons.compress.archivers.sevenz.SevenZFile.ArchiveStatistics.toString()
    - Fix kilobyte to kibibyte conversion in
    org.apache.commons.compress.archivers.sevenz.SevenZFile.Builder.setMaxMemoryLimitKb(int)
---
 src/changes/changes.xml                                    |  3 +++
 .../commons/compress/archivers/sevenz/SevenZFile.java      | 14 +++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c5c437178..b163847f4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -46,6 +46,9 @@ The <action> type attribute can be add,update,fix,remove.
       <!-- FIX -->
       <action type="fix" dev="ggregory" due-to="Piotr P. Karwasz, Gary Gregory">Add missing Javadoc @since tag to org.apache.commons.compress.compressors.lz77support.LZ77Compressor.AbstractReference.</action>
       <action type="fix" issue="COMPRESS-702" dev="ggregory" due-to="Zhang Di, Lastoneee">Performance issue in SevenZFile #681.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix kilobyte to kibibyte conversion in org.apache.commons.compress.archivers.sevenz.SevenZFile.ArchiveStatistics.assertValidity(int).</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix kilobyte to kibibyte conversion in org.apache.commons.compress.archivers.sevenz.SevenZFile.ArchiveStatistics.toString().</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix kilobyte to kibibyte conversion in org.apache.commons.compress.archivers.sevenz.SevenZFile.Builder.setMaxMemoryLimitKb(int).</action>
       <!-- ADD -->
       <!-- UPDATE -->
     </release>
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
index c29771348..9865db0b9 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
@@ -110,7 +110,7 @@ void assertValidity(final int maxMemoryLimitKiB) throws IOException {
             if (numberOfEntriesWithStream > numberOfUnpackSubStreams) {
                 throw new IOException("archive doesn't contain enough substreams for entries");
             }
-            final long memoryNeededInKiB = estimateSize() / 1024;
+            final long memoryNeededInKiB = kbToKiB(estimateSize());
             if (maxMemoryLimitKiB < memoryNeededInKiB) {
                 throw new MemoryLimitException(memoryNeededInKiB, maxMemoryLimitKiB);
             }
@@ -171,7 +171,7 @@ private long streamMapSize() {
 
         @Override
         public String toString() {
-            return String.format("Archive with %,d entries in %,d folders, estimated size %,d KiB.", numberOfEntries, numberOfFolders, estimateSize() / 1024L);
+            return String.format("Archive with %,d entries in %,d folders, estimated size %,d KiB.", numberOfEntries, numberOfFolders, kbToKiB(estimateSize()));
         }
     }
 
@@ -239,7 +239,7 @@ public Builder setDefaultName(final String defaultName) {
          * @return {@code this} instance.
          */
         public Builder setMaxMemoryLimitKb(final int maxMemoryLimitKb) {
-            this.maxMemoryLimitKiB = maxMemoryLimitKb / 1024;
+            this.maxMemoryLimitKiB = kbToKiB(maxMemoryLimitKb);
             return this;
         }
 
@@ -387,6 +387,14 @@ private static int getUnsignedByte(final ByteBuffer buf) throws EOFException {
         return buf.get() & 0xff;
     }
 
+    private static int kbToKiB(final int kilobytes) {
+        return kilobytes * 1000 / 1024;
+    }
+
+    private static long kbToKiB(final long kilobytes) {
+        return kilobytes * 1000 / 1024;
+    }
+
     /**
      * Checks if the signature matches what is expected for a 7z file.
      *