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


The following commit(s) were added to refs/heads/master by this push:
     new 1bf57e0e6 Fix Javadoc and names to reflect kibibytes scale
1bf57e0e6 is described below

commit 1bf57e0e60f4ef0d37e02e38d06dbf446bc0f05c
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Jan 4 10:05:28 2025 -0500

    Fix Javadoc and names to reflect kibibytes scale
---
 .../commons/compress/MemoryLimitException.java     | 53 ++++++++++++++++------
 .../compress/archivers/sevenz/SevenZFile.java      |  2 +-
 .../lzma/LZMACompressorInputStream.java            |  6 +--
 .../compress/compressors/lzw/LZWInputStream.java   | 12 ++---
 4 files changed, 48 insertions(+), 25 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/compress/MemoryLimitException.java 
b/src/main/java/org/apache/commons/compress/MemoryLimitException.java
index edb07adc5..c637f458d 100644
--- a/src/main/java/org/apache/commons/compress/MemoryLimitException.java
+++ b/src/main/java/org/apache/commons/compress/MemoryLimitException.java
@@ -31,32 +31,55 @@ public class MemoryLimitException extends IOException {
     private static final long serialVersionUID = 1L;
 
     private static String buildMessage(final long memoryNeededInKb, final int 
memoryLimitInKb) {
-        return memoryNeededInKb + " kb of memory would be needed; limit was " 
+ memoryLimitInKb + " kb. "
-                + "If the file is not corrupt, consider increasing the memory 
limit.";
+        return String.format("%,d Kb of memory would be needed; limit was %,d 
Kb. If the file is not corrupt, consider increasing the memory limit.",
+                memoryNeededInKb, memoryLimitInKb);
     }
 
     /** A long instead of int to account for overflow for corrupt files. */
-    private final long memoryNeededInKb;
+    private final long memoryNeededKiB;
+    private final int memoryLimitKiB;
 
-    private final int memoryLimitInKb;
-
-    public MemoryLimitException(final long memoryNeededInKb, final int 
memoryLimitInKb) {
-        super(buildMessage(memoryNeededInKb, memoryLimitInKb));
-        this.memoryNeededInKb = memoryNeededInKb;
-        this.memoryLimitInKb = memoryLimitInKb;
+    /**
+     * Constructs a new instance.
+     *
+     * @param memoryNeededKiB The memory needed in kibibytes (KiB).
+     * @param memoryLimitKiB  The memory limit in kibibytes (KiB).
+     */
+    public MemoryLimitException(final long memoryNeededKiB, final int 
memoryLimitKiB) {
+        super(buildMessage(memoryNeededKiB, memoryLimitKiB));
+        this.memoryNeededKiB = memoryNeededKiB;
+        this.memoryLimitKiB = memoryLimitKiB;
     }
 
-    public MemoryLimitException(final long memoryNeededInKb, final int 
memoryLimitInKb, final Exception e) {
-        super(buildMessage(memoryNeededInKb, memoryLimitInKb), e);
-        this.memoryNeededInKb = memoryNeededInKb;
-        this.memoryLimitInKb = memoryLimitInKb;
+    /**
+     * Constructs a new instance.
+     *
+     * @param memoryNeededKiB The memory needed in kibibytes (KiB).
+     * @param memoryLimitKiB  The memory limit in kibibytes (KiB).
+     * @param cause            The cause (which is saved for later retrieval 
by the {@link #getCause()} method). (A null value is permitted, and indicates 
that
+     *                         the cause is nonexistent or unknown.)
+     */
+    public MemoryLimitException(final long memoryNeededKiB, final int 
memoryLimitKiB, final Exception cause) {
+        super(buildMessage(memoryNeededKiB, memoryLimitKiB), cause);
+        this.memoryNeededKiB = memoryNeededKiB;
+        this.memoryLimitKiB = memoryLimitKiB;
     }
 
+    /**
+     * Gets the memory limit in kibibytes (KiB).
+     *
+     * @return the memory limit in kibibytes (KiB).
+     */
     public int getMemoryLimitInKb() {
-        return memoryLimitInKb;
+        return memoryLimitKiB;
     }
 
+    /**
+     * Gets the memory needed in kibibytes (KiB).
+     *
+     * @return the memory needed in kibibytes (KiB).
+     */
     public long getMemoryNeededInKb() {
-        return memoryNeededInKb;
+        return memoryNeededKiB;
     }
 }
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 08209b526..166fb75e3 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
@@ -98,7 +98,7 @@ public class SevenZFile implements Closeable {
         /**
          * Asserts the validity of the given input.
          *
-         * @param maxMemoryLimitKiB kibibytes to test.
+         * @param maxMemoryLimitKiB kibibytes (KiB) to test.
          * @throws IOException Thrown on basic assertion failure.
          */
         void assertValidity(final int maxMemoryLimitKiB) throws IOException {
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorInputStream.java
 
b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorInputStream.java
index 0abf1a485..834330523 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorInputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorInputStream.java
@@ -65,15 +65,15 @@ public class LZMACompressorInputStream extends 
CompressorInputStream implements
      * Creates a new input stream that decompresses LZMA-compressed data from 
the specified input stream.
      *
      * @param inputStream     where to read the compressed data
-     * @param memoryLimitInKb calculated memory use threshold. Throws 
MemoryLimitException if calculate memory use is above this threshold
+     * @param memoryLimitKiB calculated memory use threshold in kibibytes 
(KiB). Throws MemoryLimitException if calculate memory use is above this 
threshold
      * @throws IOException if the input is not in the .lzma format, the input 
is corrupt or truncated, the .lzma headers specify sizes that are not supported 
by
      *                     this implementation, or the underlying {@code 
inputStream} throws an exception
      *
      * @since 1.14
      */
-    public LZMACompressorInputStream(final InputStream inputStream, final int 
memoryLimitInKb) throws IOException {
+    public LZMACompressorInputStream(final InputStream inputStream, final int 
memoryLimitKiB) throws IOException {
         try {
-            in = new LZMAInputStream(countingStream = 
BoundedInputStream.builder().setInputStream(inputStream).get(), 
memoryLimitInKb);
+            in = new LZMAInputStream(countingStream = 
BoundedInputStream.builder().setInputStream(inputStream).get(), memoryLimitKiB);
         } catch (final org.tukaani.xz.MemoryLimitException e) {
             // convert to commons-compress exception
             throw new MemoryLimitException(e.getMemoryNeeded(), 
e.getMemoryLimit(), e);
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java 
b/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
index 0aa346854..6aef44a30 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
@@ -198,23 +198,23 @@ public abstract class LZWInputStream extends 
CompressorInputStream implements In
      * Initializes the arrays based on the maximum code size. First checks 
that the estimated memory usage is below memoryLimitInKb
      *
      * @param maxCodeSize     maximum code size
-     * @param memoryLimitInKiB maximum allowed estimated memory usage in 
kibibytes.
-     * @throws MemoryLimitException     if estimated memory usage is greater 
than memoryLimitInKiB
+     * @param memoryLimiKiB maximum allowed estimated memory usage in 
kibibytes (KiB).
+     * @throws MemoryLimitException     if estimated memory usage is greater 
than memoryLimitKiB
      * @throws IllegalArgumentException if {@code maxCodeSize} is not bigger 
than 0
      */
-    protected void initializeTables(final int maxCodeSize, final int 
memoryLimitInKiB) throws MemoryLimitException {
+    protected void initializeTables(final int maxCodeSize, final int 
memoryLimiKiB) throws MemoryLimitException {
         if (maxCodeSize <= 0) {
             throw new IllegalArgumentException("maxCodeSize is " + maxCodeSize 
+ ", must be bigger than 0");
         }
 
-        if (memoryLimitInKiB > -1) {
+        if (memoryLimiKiB > -1) {
             final int maxTableSize = 1 << maxCodeSize;
             // account for potential overflow
             final long memoryUsageBytes = (long) maxTableSize * 6; // (4 
(prefixes) + 1 (characters) +1 (outputStack))
             final long memoryUsageKiB = memoryUsageBytes >> 10;
 
-            if (memoryUsageKiB > memoryLimitInKiB) {
-                throw new MemoryLimitException(memoryUsageKiB, 
memoryLimitInKiB);
+            if (memoryUsageKiB > memoryLimiKiB) {
+                throw new MemoryLimitException(memoryUsageKiB, memoryLimiKiB);
             }
         }
         initializeTables(maxCodeSize);

Reply via email to