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/15 00:29:00 UTC

(commons-compress) branch master updated (703751f06 -> 88fceef2e)

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

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


    from 703751f06 org.apache.commons.compress.archivers.zip.ZipArchiveInputStream now throws an MemoryLimitException instead of ArchiveException, or OutOfMemoryError when running with low memory settings set on the command line
     new 60dd897f9 Javadoc
     new 0b93d10f3 Fix for when a valid raw Snappy stream with uncompressed size > 2 GiB used to decompress and then fail at physical EOF with a “Premature end of stream” exception instead of completing cleanly
     new 88fceef2e Javadoc

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |   5 +-
 .../archivers/zip/ZipArchiveOutputStream.java      | 110 ++++++++++-----------
 .../compressors/lz77support/LZ77Compressor.java    |   2 +-
 .../snappy/FramedSnappyCompressorInputStream.java  |  34 +++----
 .../snappy/FramedSnappyCompressorOutputStream.java |  10 +-
 .../snappy/SnappyCompressorInputStream.java        |  78 +++++++--------
 .../snappy/SnappyCompressorOutputStream.java       |  27 ++---
 .../compressors/snappy/SnappyRoundtripTest.java    |   3 +
 8 files changed, 132 insertions(+), 137 deletions(-)


(commons-compress) 01/03: Javadoc

Posted by gg...@apache.org.
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 60dd897f97341e2edd177cb5a8a06644da518420
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 14 19:05:20 2025 -0400

    Javadoc
    
    Reduce vertical whitespace
---
 .../compressors/lz77support/LZ77Compressor.java    |  2 +-
 .../snappy/FramedSnappyCompressorInputStream.java  | 34 +++++++--------
 .../snappy/FramedSnappyCompressorOutputStream.java | 10 ++---
 .../snappy/SnappyCompressorInputStream.java        | 48 +++++++---------------
 .../snappy/SnappyCompressorOutputStream.java       | 26 ++++++------
 5 files changed, 48 insertions(+), 72 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
index 4ec694563..5ee128b1a 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
@@ -451,7 +451,7 @@ private void doCompress(final byte[] data, final int off, final int len) throws
      * The compressor will in turn emit at least one block ({@link EOD}) but potentially multiple blocks to the callback during the execution of this method.
      * </p>
      *
-     * @throws IOException if the callback throws an exception
+     * @throws IOException if the callback throws an exception.
      */
     public void finish() throws IOException {
         if (blockStart != currentPosition || lookahead > 0) {
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
index d2ce21c97..f83f5251e 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
@@ -69,27 +69,23 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream imp
      * .sz files start with a chunk with tag 0xff and content sNaPpY.
      * </p>
      *
-     * @param signature the bytes to check
-     * @param length    the number of bytes to check
-     * @return true if this is a .sz stream, false otherwise
+     * @param signature the bytes to check.
+     * @param length    the number of bytes to check.
+     * @return true if this is a .sz stream, false otherwise.
      */
     public static boolean matches(final byte[] signature, final int length) {
-
         if (length < SZ_SIGNATURE.length) {
             return false;
         }
-
         byte[] shortenedSig = signature;
         if (signature.length > SZ_SIGNATURE.length) {
             shortenedSig = Arrays.copyOf(signature, SZ_SIGNATURE.length);
         }
-
         return Arrays.equals(shortenedSig, SZ_SIGNATURE);
     }
 
     static long unmask(long x) {
-        // ugly, maybe we should just have used ints and deal with the
-        // overflow
+        // ugly, maybe we should just have used ints and deal with the overflow.
         x -= MASK_OFFSET;
         x &= 0xffffFFFFL;
         return (x >> 17 | x << 15) & 0xffffFFFFL;
@@ -99,7 +95,7 @@ static long unmask(long x) {
 
     private final BoundedInputStream countingStream;
 
-    /** The underlying stream to read compressed data from */
+    /** The underlying stream to read compressed data from. */
     private final PushbackInputStream inputStream;
 
     /** The dialect to expect */
@@ -124,8 +120,8 @@ static long unmask(long x) {
      * Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream using the
      * {@link FramedSnappyDialect#STANDARD} dialect.
      *
-     * @param in the InputStream from which to read the compressed data
-     * @throws IOException if reading fails
+     * @param in the InputStream from which to read the compressed data.
+     * @throws IOException if reading fails.
      */
     public FramedSnappyCompressorInputStream(final InputStream in) throws IOException {
         this(in, FramedSnappyDialect.STANDARD);
@@ -134,9 +130,9 @@ public FramedSnappyCompressorInputStream(final InputStream in) throws IOExceptio
     /**
      * Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream.
      *
-     * @param in      the InputStream from which to read the compressed data
-     * @param dialect the dialect used by the compressed stream
-     * @throws IOException if reading fails
+     * @param in      the InputStream from which to read the compressed data.
+     * @param dialect the dialect used by the compressed stream.
+     * @throws IOException if reading fails.
      */
     public FramedSnappyCompressorInputStream(final InputStream in, final FramedSnappyDialect dialect) throws IOException {
         this(in, SnappyCompressorInputStream.DEFAULT_BLOCK_SIZE, dialect);
@@ -145,11 +141,11 @@ public FramedSnappyCompressorInputStream(final InputStream in, final FramedSnapp
     /**
      * Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream.
      *
-     * @param in        the InputStream from which to read the compressed data
-     * @param blockSize the block size to use for the compressed stream
-     * @param dialect   the dialect used by the compressed stream
-     * @throws IOException              if reading fails
-     * @throws IllegalArgumentException if blockSize is not bigger than 0
+     * @param in        the InputStream from which to read the compressed data.
+     * @param blockSize the block size to use for the compressed stream.
+     * @param dialect   the dialect used by the compressed stream.
+     * @throws IOException              if reading fails.
+     * @throws IllegalArgumentException if blockSize is not bigger than 0.
      * @since 1.14
      */
     public FramedSnappyCompressorInputStream(final InputStream in, final int blockSize, final FramedSnappyDialect dialect) throws IOException {
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java
index 813848888..ce3be3c3c 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java
@@ -67,8 +67,8 @@ static long mask(long x) {
     /**
      * Constructs a new output stream that compresses snappy-framed-compressed data to the specified output stream.
      *
-     * @param out the OutputStream to which to write the compressed data
-     * @throws IOException if writing the signature fails
+     * @param out the OutputStream to which to write the compressed data.
+     * @throws IOException if writing the signature fails.
      */
     public FramedSnappyCompressorOutputStream(final OutputStream out) throws IOException {
         this(out, SnappyCompressorOutputStream.createParameterBuilder(SnappyCompressorInputStream.DEFAULT_BLOCK_SIZE).build());
@@ -77,9 +77,9 @@ public FramedSnappyCompressorOutputStream(final OutputStream out) throws IOExcep
     /**
      * Constructs a new output stream that compresses snappy-framed-compressed data to the specified output stream.
      *
-     * @param out    the OutputStream to which to write the compressed data
+     * @param out    the OutputStream to which to write the compressed data.
      * @param params parameters used to fine-tune compression, in particular to balance compression ratio vs compression speed.
-     * @throws IOException if writing the signature fails
+     * @throws IOException if writing the signature fails.
      */
     public FramedSnappyCompressorOutputStream(final OutputStream out, final Parameters params) throws IOException {
         super(out);
@@ -100,7 +100,7 @@ public void close() throws IOException {
     /**
      * Compresses all remaining data and writes it to the stream, doesn't close the underlying stream.
      *
-     * @throws IOException if an error occurs
+     * @throws IOException if an error occurs.
      */
     @Override
     public void finish() throws IOException {
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
index ecbc55b64..62a108ee6 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
@@ -43,19 +43,19 @@ private enum State {
         NO_BLOCK, IN_LITERAL, IN_BACK_REFERENCE
     }
 
-    /** Mask used to determine the type of "tag" is being processed */
+    /** Mask used to determine the type of "tag" is being processed. */
     private static final int TAG_MASK = 0x03;
 
-    /** Default block size */
+    /** Default block size. */
     public static final int DEFAULT_BLOCK_SIZE = 32768;
 
-    /** The size of the uncompressed data */
+    /** The size of the uncompressed data. */
     private final int size;
 
     /** Number of uncompressed bytes still to be read. */
     private int uncompressedBytesRemaining;
 
-    /** Current state of the stream */
+    /** Current state of the stream. */
     private State state = State.NO_BLOCK;
 
     private boolean endReached;
@@ -63,8 +63,8 @@ private enum State {
     /**
      * Constructor using the default buffer size of 32k.
      *
-     * @param is An InputStream to read compressed data from
-     * @throws IOException if reading fails
+     * @param is An InputStream to read compressed data from.
+     * @throws IOException if reading fails.
      */
     public SnappyCompressorInputStream(final InputStream is) throws IOException {
         this(is, DEFAULT_BLOCK_SIZE);
@@ -73,10 +73,10 @@ public SnappyCompressorInputStream(final InputStream is) throws IOException {
     /**
      * Constructor using a configurable buffer size.
      *
-     * @param is        An InputStream to read compressed data from
-     * @param blockSize The block size used in compression
-     * @throws IOException              if reading fails
-     * @throws IllegalArgumentException if blockSize is not bigger than 0
+     * @param is        An InputStream to read compressed data from.
+     * @param blockSize The block size used in compression.
+     * @throws IOException              if reading fails.
+     * @throws IllegalArgumentException if blockSize is not bigger than 0.
      */
     public SnappyCompressorInputStream(final InputStream is, final int blockSize) throws IOException {
         super(is, blockSize);
@@ -91,18 +91,14 @@ private void fill() throws IOException {
             endReached = true;
             return;
         }
-
         int b = readOneByte();
         if (b == -1) {
             throw new CompressorException("Premature end of stream reading block start");
         }
         int length = 0;
         int offset = 0;
-
         switch (b & TAG_MASK) {
-
         case 0x00:
-
             length = readLiteralLength(b);
             if (length < 0) {
                 throw new CompressorException("Illegal block with a negative literal size found");
@@ -111,15 +107,12 @@ private void fill() throws IOException {
             startLiteral(length);
             state = State.IN_LITERAL;
             break;
-
         case 0x01:
-
             /*
              * These elements can encode lengths between [4..11] bytes and offsets between [0..2047] bytes. (len-4) occupies three bits and is stored in bits
              * [2..4] of the tag byte. The offset occupies 11 bits, of which the upper three are stored in the upper three bits ([5..7]) of the tag byte, and
              * the lower eight are stored in a byte following the tag byte.
              */
-
             length = 4 + (b >> 2 & 0x07);
             uncompressedBytesRemaining -= length;
             offset = (b & 0xE0) << 3;
@@ -128,7 +121,6 @@ private void fill() throws IOException {
                 throw new CompressorException("Premature end of stream reading back-reference length");
             }
             offset |= b;
-
             try {
                 startBackReference(offset, length);
             } catch (final IllegalArgumentException ex) {
@@ -136,22 +128,17 @@ private void fill() throws IOException {
             }
             state = State.IN_BACK_REFERENCE;
             break;
-
         case 0x02:
-
             /*
              * These elements can encode lengths between [1..64] and offsets from [0..65535]. (len-1) occupies six bits and is stored in the upper six bits
              * ([2..7]) of the tag byte. The offset is stored as a little-endian 16-bit integer in the two bytes following the tag byte.
              */
-
             length = (b >> 2) + 1;
             if (length < 0) {
                 throw new CompressorException("Illegal block with a negative match length found");
             }
             uncompressedBytesRemaining -= length;
-
             offset = (int) ByteUtils.fromLittleEndian(supplier, 2);
-
             try {
                 startBackReference(offset, length);
             } catch (final IllegalArgumentException ex) {
@@ -159,22 +146,17 @@ private void fill() throws IOException {
             }
             state = State.IN_BACK_REFERENCE;
             break;
-
         case 0x03:
-
             /*
              * These are like the copies with 2-byte offsets (see previous subsection), except that the offset is stored as a 32-bit integer instead of a 16-bit
              * integer (and thus will occupy four bytes).
              */
-
             length = (b >> 2) + 1;
             if (length < 0) {
                 throw new CompressorException("Illegal block with a negative match length found");
             }
             uncompressedBytesRemaining -= length;
-
             offset = (int) ByteUtils.fromLittleEndian(supplier, 4) & 0x7fffffff;
-
             try {
                 startBackReference(offset, length);
             } catch (final IllegalArgumentException ex) {
@@ -189,9 +171,9 @@ private void fill() throws IOException {
     }
 
     /**
-     * Gets the uncompressed size of the stream
+     * Gets the uncompressed size of the stream.
      *
-     * @return the uncompressed size
+     * @return the uncompressed size.
      */
     @Override
     public int getSize() {
@@ -257,7 +239,6 @@ private int readLiteralLength(final int b) throws IOException {
             length = b >> 2;
             break;
         }
-
         return length + 1;
     }
 
@@ -266,14 +247,13 @@ private int readLiteralLength(final int b) throws IOException {
      * where the lower 7 bits are data and the upper bit is set iff there are more bytes to be read. In other words, an uncompressed length of 64 would be
      * stored as 0x40, and an uncompressed length of 2097150 (0x1FFFFE) would be stored as 0xFE 0xFF 0x7F.
      *
-     * @return The size of the uncompressed data
-     * @throws IOException Could not read a byte
+     * @return The size of the uncompressed data.
+     * @throws IOException Could not read a byte.
      */
     private long readSize() throws IOException {
         int index = 0;
         long sz = 0;
         int b = 0;
-
         do {
             b = readOneByte();
             if (b == -1) {
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
index f7ff8e4b2..f78e5c7fe 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
@@ -88,7 +88,7 @@ public class SnappyCompressorOutputStream extends CompressorOutputStream<OutputS
      * Returns a builder correctly configured for the Snappy algorithm using the gven block size.
      *
      * @param blockSize the block size.
-     * @return a builder correctly configured for the Snappy algorithm using the gven block size
+     * @return a builder correctly configured for the Snappy algorithm using the gven block size.
      */
     public static Parameters.Builder createParameterBuilder(final int blockSize) {
         // the max offset and max literal length defined by the format
@@ -107,9 +107,9 @@ public static Parameters.Builder createParameterBuilder(final int blockSize) {
     /**
      * Constructor using the default block size of 32k.
      *
-     * @param os               the outputstream to write compressed data to
-     * @param uncompressedSize the uncompressed size of data
-     * @throws IOException if writing of the size fails
+     * @param os               the outputstream to write compressed data to.
+     * @param uncompressedSize the uncompressed size of data.
+     * @throws IOException if writing of the size fails.
      */
     public SnappyCompressorOutputStream(final OutputStream os, final long uncompressedSize) throws IOException {
         this(os, uncompressedSize, SnappyCompressorInputStream.DEFAULT_BLOCK_SIZE);
@@ -118,10 +118,10 @@ public SnappyCompressorOutputStream(final OutputStream os, final long uncompress
     /**
      * Constructor using a configurable block size.
      *
-     * @param os               the outputstream to write compressed data to
-     * @param uncompressedSize the uncompressed size of data
-     * @param blockSize        the block size used - must be a power of two
-     * @throws IOException if writing of the size fails
+     * @param os               the outputstream to write compressed data to.
+     * @param uncompressedSize the uncompressed size of data.
+     * @param blockSize        the block size used - must be a power of two.
+     * @throws IOException if writing of the size fails.
      */
     public SnappyCompressorOutputStream(final OutputStream os, final long uncompressedSize, final int blockSize) throws IOException {
         this(os, uncompressedSize, createParameterBuilder(blockSize).build());
@@ -130,10 +130,10 @@ public SnappyCompressorOutputStream(final OutputStream os, final long uncompress
     /**
      * Constructor providing full control over the underlying LZ77 compressor.
      *
-     * @param out               the outputstream to write compressed data to
-     * @param uncompressedSize the uncompressed size of data
-     * @param params           the parameters to use by the compressor - note that the format itself imposes some limits like a maximum match length of 64 bytes
-     * @throws IOException if writing of the size fails
+     * @param out               the outputstream to write compressed data to.
+     * @param uncompressedSize the uncompressed size of data.
+     * @param params           the parameters to use by the compressor - note that the format itself imposes some limits like a maximum match length of 64 bytes.
+     * @throws IOException if writing of the size fails.
      */
     public SnappyCompressorOutputStream(final OutputStream out, final long uncompressedSize, final Parameters params) throws IOException {
         super(out);
@@ -165,7 +165,7 @@ public void close() throws IOException {
     /**
      * Compresses all remaining data and writes it to the stream, doesn't close the underlying stream.
      *
-     * @throws IOException if an error occurs
+     * @throws IOException if an error occurs.
      */
     @Override
     public void finish() throws IOException {


(commons-compress) 02/03: Fix for when a valid raw Snappy stream with uncompressed size > 2 GiB used to decompress and then fail at physical EOF with a “Premature end of stream” exception instead of completing cleanly

Posted by gg...@apache.org.
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 0b93d10f3d705dff8f56e6e63bff82e648293ccf
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 14 20:24:24 2025 -0400

    Fix for when a valid raw Snappy stream with uncompressed size > 2 GiB
    used to decompress and then fail at physical EOF with a “Premature end
    of stream” exception instead of completing cleanly
    
    - Add org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream.getUncompressedSize()
    and deprecate getSize()
---
 src/changes/changes.xml                            |  5 +++-
 .../snappy/SnappyCompressorInputStream.java        | 32 +++++++++++++++-------
 .../compressors/snappy/SnappyRoundtripTest.java    |  3 ++
 3 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 80034556d..fa4e937a2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -85,9 +85,11 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Stan, Gary Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream.getNextEntry() now throws a MemoryLimitException instead of OutOfMemoryError when it can't process input greater than available memory.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream.readOldAsciiEntry(boolean) now throws ArchiveException instead of Arithmetic exception.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream.readOldBinaryEntry(boolean) now throws ArchiveException instead of Arithmetic exception.</action>
-      <!-- FIX gzip -->      
+      <!-- FIX gzip -->
       <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.compressors.gzip.GzipParameters.setOperatingSystem(int) now throws CompressorException on illegal input.</action>
       <action type="fix" issue="COMPRESS-705" dev="ggregory" due-to="Mario Fredenhagen, Gary Gregory">GZip IOException: Extra subfield lenght exceeds remaining bytes in extra field; use new option org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream.Builder.setIgnoreExtraField(boolean).</action>
+      <!-- FIX snappy -->
+      <action type="fix" dev="ggregory" due-to="Stanislav Fort, Gary Gregory">Fix for when a valid raw Snappy stream with uncompressed size > 2 GiB used to decompress and then fail at physical EOF with a “Premature end of stream” exception instead of completing cleanly.</action>
       <!-- FIX general -->      
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Classes in org.apache.commons.compress.archivers now throw a subclass of IOException called ArchiveException instead of IOException when a formatting problem is found.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Classes in org.apache.commons.compress.compressors now throw a subclass of IOException called CompressorException instead of IOException when a formatting problem is found.</action>
@@ -107,6 +109,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.CompressException.addExact(int, long).</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.harmony.pack200.Pack200Exception.addExact(int, long).</action>
       <action type="add" issue="COMPRESS-705" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream.Builder.setIgnoreExtraField(boolean).</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream.getUncompressedSize() and deprecate getSize().</action>
       <!-- UPDATE -->
     </release>
     <release version="1.28.0" date="2025-07-26" description="This is a feature and maintenance release. Java 8 or later is required.">
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
index 62a108ee6..037903314 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
@@ -49,11 +49,11 @@ private enum State {
     /** Default block size. */
     public static final int DEFAULT_BLOCK_SIZE = 32768;
 
-    /** The size of the uncompressed data. */
-    private final int size;
+    /** Number of uncompressed bytes still to be read. */
+    private long uncompressRemaining;
 
     /** Number of uncompressed bytes still to be read. */
-    private int uncompressedBytesRemaining;
+    private final long uncompressedSize;
 
     /** Current state of the stream. */
     private State state = State.NO_BLOCK;
@@ -80,14 +80,14 @@ public SnappyCompressorInputStream(final InputStream is) throws IOException {
      */
     public SnappyCompressorInputStream(final InputStream is, final int blockSize) throws IOException {
         super(is, blockSize);
-        uncompressedBytesRemaining = size = (int) readSize();
+        uncompressRemaining = uncompressedSize = readSize();
     }
 
     /**
      * Try to fill the buffer with the next block of data.
      */
     private void fill() throws IOException {
-        if (uncompressedBytesRemaining == 0) {
+        if (uncompressRemaining == 0) {
             endReached = true;
             return;
         }
@@ -103,7 +103,7 @@ private void fill() throws IOException {
             if (length < 0) {
                 throw new CompressorException("Illegal block with a negative literal size found");
             }
-            uncompressedBytesRemaining -= length;
+            uncompressRemaining -= length;
             startLiteral(length);
             state = State.IN_LITERAL;
             break;
@@ -114,7 +114,7 @@ private void fill() throws IOException {
              * the lower eight are stored in a byte following the tag byte.
              */
             length = 4 + (b >> 2 & 0x07);
-            uncompressedBytesRemaining -= length;
+            uncompressRemaining -= length;
             offset = (b & 0xE0) << 3;
             b = readOneByte();
             if (b == -1) {
@@ -137,7 +137,7 @@ private void fill() throws IOException {
             if (length < 0) {
                 throw new CompressorException("Illegal block with a negative match length found");
             }
-            uncompressedBytesRemaining -= length;
+            uncompressRemaining -= length;
             offset = (int) ByteUtils.fromLittleEndian(supplier, 2);
             try {
                 startBackReference(offset, length);
@@ -155,7 +155,7 @@ private void fill() throws IOException {
             if (length < 0) {
                 throw new CompressorException("Illegal block with a negative match length found");
             }
-            uncompressedBytesRemaining -= length;
+            uncompressRemaining -= length;
             offset = (int) ByteUtils.fromLittleEndian(supplier, 4) & 0x7fffffff;
             try {
                 startBackReference(offset, length);
@@ -174,10 +174,22 @@ private void fill() throws IOException {
      * Gets the uncompressed size of the stream.
      *
      * @return the uncompressed size.
+     * @deprecated Use {@link #getUncompressedSize()}
      */
+    @Deprecated
     @Override
     public int getSize() {
-        return size;
+        return (int) uncompressedSize;
+    }
+
+    /**
+     * Gets the uncompressed size of the stream.
+     *
+     * @return the uncompressed size.
+     * @since 1.29.0
+     */
+    public long getUncompressedSize() {
+        return uncompressedSize;
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/compress/compressors/snappy/SnappyRoundtripTest.java b/src/test/java/org/apache/commons/compress/compressors/snappy/SnappyRoundtripTest.java
index b28d435f1..16647c817 100644
--- a/src/test/java/org/apache/commons/compress/compressors/snappy/SnappyRoundtripTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/snappy/SnappyRoundtripTest.java
@@ -19,6 +19,7 @@
 package org.apache.commons.compress.compressors.snappy;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -51,6 +52,8 @@ private void roundTripTest(final byte[] input, final Parameters params) throws I
         try (SnappyCompressorInputStream sis = new SnappyCompressorInputStream(new ByteArrayInputStream(os.toByteArray()), params.getWindowSize())) {
             final byte[] actual = IOUtils.toByteArray(sis);
             assertArrayEquals(input, actual);
+            assertEquals(input.length, sis.getUncompressedSize());
+            assertEquals(input.length, sis.getSize());
         }
     }
 


(commons-compress) 03/03: Javadoc

Posted by gg...@apache.org.
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 88fceef2e5cd6a246e61db0a76178b4d9900b721
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 14 20:28:55 2025 -0400

    Javadoc
---
 .../archivers/zip/ZipArchiveOutputStream.java      | 110 ++++++++++-----------
 .../snappy/SnappyCompressorOutputStream.java       |   9 +-
 2 files changed, 60 insertions(+), 59 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
index 8926e29be..8672e2fd6 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
@@ -384,8 +384,8 @@ public String toString() {
     /**
      * Creates a new ZIP OutputStream writing to a File. Will use random access if possible.
      *
-     * @param file the file to ZIP to
-     * @throws IOException on error
+     * @param file the file to ZIP to.
+     * @throws IOException on error.
      */
     public ZipArchiveOutputStream(final File file) throws IOException {
         this(file.toPath());
@@ -404,10 +404,10 @@ public ZipArchiveOutputStream(final File file) throws IOException {
      * known before the actual entry data is written.
      * </p>
      *
-     * @param file         the file that will become the last part of the split archive
+     * @param file         the file that will become the last part of the split archive.
      * @param zipSplitSize maximum size of a single part of the split archive created by this stream. Must be between 64kB and about 4GB.
-     * @throws IOException              on error
-     * @throws IllegalArgumentException if zipSplitSize is not in the required range
+     * @throws IOException              on error.
+     * @throws IllegalArgumentException if zipSplitSize is not in the required range.
      * @since 1.20
      */
     public ZipArchiveOutputStream(final File file, final long zipSplitSize) throws IOException {
@@ -417,7 +417,7 @@ public ZipArchiveOutputStream(final File file, final long zipSplitSize) throws I
     /**
      * Creates a new ZIP OutputStream filtering the underlying stream.
      *
-     * @param out the outputstream to zip
+     * @param out the output stream to zip.
      */
     public ZipArchiveOutputStream(final OutputStream out) {
         this.out = out;
@@ -437,10 +437,10 @@ public ZipArchiveOutputStream(final OutputStream out) {
      * known before the actual entry data is written.
      * </p>
      *
-     * @param path         the path to the file that will become the last part of the split archive
+     * @param path         the path to the file that will become the last part of the split archive.
      * @param zipSplitSize maximum size of a single part of the split archive created by this stream. Must be between 64kB and about 4GB.
-     * @throws IOException              on error
-     * @throws IllegalArgumentException if zipSplitSize is not in the required range
+     * @throws IOException              on error.
+     * @throws IllegalArgumentException if zipSplitSize is not in the required range.
      * @since 1.22
      */
     public ZipArchiveOutputStream(final Path path, final long zipSplitSize) throws IOException {
@@ -453,9 +453,9 @@ public ZipArchiveOutputStream(final Path path, final long zipSplitSize) throws I
     /**
      * Creates a new ZIP OutputStream writing to a Path. Will use random access if possible.
      *
-     * @param file    the file to ZIP to
+     * @param file    the file to ZIP to.
      * @param options options specifying how the file is opened.
-     * @throws IOException on error
+     * @throws IOException on error.
      * @since 1.21
      */
     public ZipArchiveOutputStream(final Path file, final OpenOption... options) throws IOException {
@@ -472,7 +472,7 @@ public ZipArchiveOutputStream(final Path file, final OpenOption... options) thro
      * {@link org.apache.commons.compress.utils.SeekableInMemoryByteChannel} allows you to write to an in-memory archive using random access.
      * </p>
      *
-     * @param channel the channel to ZIP to
+     * @param channel the channel to ZIP to.
      * @since 1.13
      */
     public ZipArchiveOutputStream(final SeekableByteChannel channel) {
@@ -492,9 +492,9 @@ public ZipArchiveOutputStream(final SeekableByteChannel channel) {
      * The entry is put and closed immediately.
      * </p>
      *
-     * @param entry     The archive entry to add
+     * @param entry     The archive entry to add.
      * @param rawStream The raw input stream of a different entry. May be compressed/encrypted.
-     * @throws IOException If copying fails
+     * @throws IOException If copying fails.
      */
     public void addRawArchiveEntry(final ZipArchiveEntry entry, final InputStream rawStream) throws IOException {
         final ZipArchiveEntry ae = new ZipArchiveEntry(entry);
@@ -580,7 +580,7 @@ public void close() throws IOException {
     /**
      * Writes all necessary data for this entry.
      *
-     * @throws IOException            on error
+     * @throws IOException            on error.
      * @throws Zip64RequiredException if the entry's uncompressed or compressed size exceeds 4 GByte and {@link #setUseZip64} is {@link Zip64Mode#Never}.
      */
     @Override
@@ -602,7 +602,7 @@ public void closeArchiveEntry() throws IOException {
      * Writes all necessary data for this entry.
      *
      * @param phased This entry is second phase of a 2-phase ZIP creation, size, compressed size and CRC are known in ZipArchiveEntry
-     * @throws IOException            on error
+     * @throws IOException            on error.
      * @throws Zip64RequiredException if the entry's uncompressed or compressed size exceeds 4 GByte and {@link #setUseZip64} is {@link Zip64Mode#Never}.
      */
     private void closeCopiedEntry(final boolean phased) throws IOException {
@@ -704,10 +704,10 @@ private byte[] createCentralFileHeader(final ZipArchiveEntry ze) throws IOExcept
     /**
      * Writes the central file header entry.
      *
-     * @param ze            the entry to write
-     * @param name          The encoded name
-     * @param entryMetaData meta data for this file
-     * @throws IOException on error
+     * @param ze            the entry to write.
+     * @param name          The encoded name.
+     * @param entryMetaData meta data for this file.
+     * @throws IOException on error.
      */
     private byte[] createCentralFileHeader(final ZipArchiveEntry ze, final ByteBuffer name, final EntryMetaData entryMetaData, final boolean needsZip64Extra)
             throws IOException {
@@ -896,7 +896,7 @@ private byte[] createLocalFileHeader(final ZipArchiveEntry ze, final ByteBuffer
     /**
      * Writes next block of compressed data to the output stream.
      *
-     * @throws IOException on error
+     * @throws IOException on error.
      */
     protected final void deflate() throws IOException {
         streamCompressor.deflate();
@@ -992,7 +992,7 @@ private void flushDeflater() throws IOException {
     /**
      * Returns the total number of bytes written to this stream.
      *
-     * @return the number of written bytes
+     * @return the number of written bytes.
      * @since 1.22
      */
     @Override
@@ -1142,7 +1142,7 @@ private boolean hasZip64Extra(final ZipArchiveEntry ze) {
      * {@link #putArchiveEntry(ZipArchiveEntry)}.
      * </p>
      *
-     * @return true if seekable
+     * @return true if seekable.
      */
     public boolean isSeekable() {
         return out instanceof RandomAccessOutputStream;
@@ -1173,7 +1173,7 @@ private void preClose() throws IOException {
     /**
      * {@inheritDoc}
      *
-     * @throws ClassCastException     if entry is not an instance of ZipArchiveEntry
+     * @throws ClassCastException     if entry is not an instance of ZipArchiveEntry.
      * @throws Zip64RequiredException if the entry's uncompressed or compressed size is known to exceed 4 GByte and {@link #setUseZip64} is
      *                                {@link Zip64Mode#Never}.
      */
@@ -1186,9 +1186,9 @@ public void putArchiveEntry(final ZipArchiveEntry archiveEntry) throws IOExcepti
      * Writes the headers for an archive entry to the output stream. The caller must then write the content to the stream and call {@link #closeArchiveEntry()}
      * to complete the process.
      *
-     * @param archiveEntry The archiveEntry
-     * @param phased       If true size, compressedSize and CRC required to be known up-front in the archiveEntry
-     * @throws ClassCastException     if entry is not an instance of ZipArchiveEntry
+     * @param archiveEntry The archiveEntry.
+     * @param phased       If true size, compressedSize and CRC required to be known up-front in the archiveEntry.
+     * @throws ClassCastException     if entry is not an instance of ZipArchiveEntry.
      * @throws Zip64RequiredException if the entry's uncompressed or compressed size is known to exceed 4 GByte and {@link #setUseZip64} is
      *                                {@link Zip64Mode#Never}.
      */
@@ -1295,7 +1295,7 @@ private void rewriteSizesAndCrc(final boolean actuallyNeedsZip64) throws IOExcep
     /**
      * Sets the file comment.
      *
-     * @param comment the comment
+     * @param comment the comment.
      */
     public void setComment(final String comment) {
         this.comment = comment;
@@ -1565,10 +1565,10 @@ private int versionNeededToExtractMethod(final int zipMethod) {
     /**
      * Writes bytes to ZIP entry.
      *
-     * @param b      the byte array to write
-     * @param offset the start position to write from
-     * @param length the number of bytes to write
-     * @throws IOException on error
+     * @param b      the byte array to write.
+     * @param offset the start position to write from.
+     * @param length the number of bytes to write.
+     * @throws IOException on error.
      */
     @Override
     public void write(final byte[] b, final int offset, final int length) throws IOException {
@@ -1583,7 +1583,7 @@ public void write(final byte[] b, final int offset, final int length) throws IOE
     /**
      * Writes the &quot;End of central dir record&quot;.
      *
-     * @throws IOException            on error
+     * @throws IOException            on error.
      * @throws Zip64RequiredException if the archive's size exceeds 4 GByte or there are more than 65535 entries inside the archive and
      *                                {@link #setUseZip64(Zip64Mode)} is {@link Zip64Mode#Never}.
      */
@@ -1647,8 +1647,8 @@ private void writeCentralDirectoryInChunks() throws IOException {
     /**
      * Writes the central file header entry.
      *
-     * @param ze the entry to write
-     * @throws IOException            on error
+     * @param ze the entry to write.
+     * @throws IOException            on error.
      * @throws Zip64RequiredException if the archive's size exceeds 4 GByte and {@link #setUseZip64(Zip64Mode)} is {@link Zip64Mode#Never}.
      */
     protected void writeCentralFileHeader(final ZipArchiveEntry ze) throws IOException {
@@ -1659,8 +1659,8 @@ protected void writeCentralFileHeader(final ZipArchiveEntry ze) throws IOExcepti
     /**
      * Writes bytes to output or random access file.
      *
-     * @param data the byte array to write
-     * @throws IOException on error
+     * @param data the byte array to write.
+     * @throws IOException on error.
      */
     private void writeCounted(final byte[] data) throws IOException {
         streamCompressor.writeCounted(data);
@@ -1669,8 +1669,8 @@ private void writeCounted(final byte[] data) throws IOException {
     /**
      * Writes the data descriptor entry.
      *
-     * @param ze the entry to write
-     * @throws IOException on error
+     * @param ze the entry to write.
+     * @throws IOException on error.
      */
     protected void writeDataDescriptor(final ZipArchiveEntry ze) throws IOException {
         if (!usesDataDescriptor(ze.getMethod(), false)) {
@@ -1688,10 +1688,10 @@ protected void writeDataDescriptor(final ZipArchiveEntry ze) throws IOException
     }
 
     /**
-     * Writes the local file header entry
+     * Writes the local file header entry.
      *
-     * @param ze the entry to write
-     * @throws IOException on error
+     * @param ze the entry to write.
+     * @throws IOException on error.
      */
     protected void writeLocalFileHeader(final ZipArchiveEntry ze) throws IOException {
         writeLocalFileHeader(ze, false);
@@ -1724,8 +1724,8 @@ private void writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased
     /**
      * Writes bytes to output or random access file.
      *
-     * @param data the byte array to write
-     * @throws IOException on error
+     * @param data the byte array to write.
+     * @throws IOException on error.
      */
     protected final void writeOut(final byte[] data) throws IOException {
         streamCompressor.writeOut(data, 0, data.length);
@@ -1734,10 +1734,10 @@ protected final void writeOut(final byte[] data) throws IOException {
     /**
      * Writes bytes to output or random access file.
      *
-     * @param data   the byte array to write
-     * @param offset the start position to write from
-     * @param length the number of bytes to write
-     * @throws IOException on error
+     * @param data   the byte array to write.
+     * @param offset the start position to write from.
+     * @param length the number of bytes to write.
+     * @throws IOException on error.
      */
     protected final void writeOut(final byte[] data, final int offset, final int length) throws IOException {
         streamCompressor.writeOut(data, offset, length);
@@ -1746,8 +1746,8 @@ protected final void writeOut(final byte[] data, final int offset, final int len
     /**
      * Writes preamble data. For most of the time, this is used to make self-extracting zips.
      *
-     * @param preamble data to write
-     * @throws IOException if an entry already exists
+     * @param preamble data to write.
+     * @throws IOException if an entry already exists.
      * @since 1.21
      */
     public void writePreamble(final byte[] preamble) throws IOException {
@@ -1757,10 +1757,10 @@ public void writePreamble(final byte[] preamble) throws IOException {
     /**
      * Writes preamble data. For most of the time, this is used to make self-extracting zips.
      *
-     * @param preamble data to write
-     * @param offset   the start offset in the data
-     * @param length   the number of bytes to write
-     * @throws IOException if an entry already exists
+     * @param preamble data to write.
+     * @param offset   the start offset in the data.
+     * @param length   the number of bytes to write.
+     * @throws IOException if an entry already exists.
      * @since 1.21
      */
     public void writePreamble(final byte[] preamble, final int offset, final int length) throws IOException {
@@ -1773,7 +1773,7 @@ public void writePreamble(final byte[] preamble, final int offset, final int len
     /**
      * Writes the &quot;ZIP64 End of central dir record&quot; and &quot;ZIP64 End of central dir locator&quot;.
      *
-     * @throws IOException on error
+     * @throws IOException on error.
      * @since 1.3
      */
     protected void writeZip64CentralDirectory() throws IOException {
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
index f78e5c7fe..ea03b7b17 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
@@ -107,7 +107,7 @@ public static Parameters.Builder createParameterBuilder(final int blockSize) {
     /**
      * Constructor using the default block size of 32k.
      *
-     * @param os               the outputstream to write compressed data to.
+     * @param os               the output stream to write compressed data to.
      * @param uncompressedSize the uncompressed size of data.
      * @throws IOException if writing of the size fails.
      */
@@ -118,7 +118,7 @@ public SnappyCompressorOutputStream(final OutputStream os, final long uncompress
     /**
      * Constructor using a configurable block size.
      *
-     * @param os               the outputstream to write compressed data to.
+     * @param os               the output stream to write compressed data to.
      * @param uncompressedSize the uncompressed size of data.
      * @param blockSize        the block size used - must be a power of two.
      * @throws IOException if writing of the size fails.
@@ -130,9 +130,10 @@ public SnappyCompressorOutputStream(final OutputStream os, final long uncompress
     /**
      * Constructor providing full control over the underlying LZ77 compressor.
      *
-     * @param out               the outputstream to write compressed data to.
+     * @param out              the output stream to write compressed data to.
      * @param uncompressedSize the uncompressed size of data.
-     * @param params           the parameters to use by the compressor - note that the format itself imposes some limits like a maximum match length of 64 bytes.
+     * @param params           the parameters to use by the compressor - note that the format itself imposes some limits like a maximum match length of 64
+     *                         bytes.
      * @throws IOException if writing of the size fails.
      */
     public SnappyCompressorOutputStream(final OutputStream out, final long uncompressedSize, final Parameters params) throws IOException {