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 6c4474f3f9c7418538ff3e1fdaa6ab90c96514b0 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Fri Apr 25 08:38:20 2025 -0400 Throw a better exception in org.apache.commons.compress.archivers.sevenz.SevenZFile.readFilesInfo(ByteBuffer, Archive) - Add org.apache.commons.compress.archivers.ArchiveException.requireNonNull(T, Supplier<String>) --- src/changes/changes.xml | 2 ++ .../commons/compress/archivers/ArchiveException.java | 18 ++++++++++++++++++ .../commons/compress/archivers/sevenz/SevenZFile.java | 7 +++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 884f61b45..85cde0d85 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -88,6 +88,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate FileNameUtils#getExtension(Path).</action> <action type="fix" dev="ggregory" due-to="Alexis Jehan, Gary Gregory">org.apache.commons.compress.harmony.unpack200.Archive.unpack() should not log to system out (the console).</action> <action type="fix" dev="Sebb" due-to="aSemy">[site] Fix minor zip docs type: remove extraneous 'a' #665.</action> + <action type="fix" dev="ggregory" due-to="Zaki, Gary Gregory">Throw a better exception in org.apache.commons.compress.archivers.sevenz.SevenZFile.readFilesInfo(ByteBuffer, Archive).</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.getModificationInstant().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.setModificationInstant(Instant).</action> @@ -117,6 +118,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.compressors.xz.XZCompressorOutputStream.builder/Builder().</action> <action type="add" dev="ggregory" due-to="Gary Gregory, David Walluck, Piotr P. Karwasz">Add org.apache.commons.compress.compressors.xz.ZstdCompressorOutputStream.builder/Builder() #666.</action> <action type="add" dev="ggregory" due-to="Gary Gregory, David Walluck, Piotr P. Karwasz">Add org.apache.commons.compress.compressors.xz.ZstdConstants #666.</action> + <action type="add" dev="ggregory" due-to="Gary Gregory, Zaki">Add org.apache.commons.compress.archivers.ArchiveException.requireNonNull(T, Supplier<String>).</action> <!-- UPDATE --> <action type="update" dev="sebb">Bump Commons Parent from 79 to 81</action> <action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 72 to 79 #563, #567, #574, #582, #587, #595.</action> diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java index ab14a5a10..29bb12214 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java +++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java @@ -19,6 +19,7 @@ package org.apache.commons.compress.archivers; import java.io.IOException; +import java.util.function.Supplier; /** * Signals that an Archive exception of some sort has occurred. @@ -28,6 +29,23 @@ public class ArchiveException extends IOException { /** Serial. */ private static final long serialVersionUID = 2772690708123267100L; + /** + * Checks that the specified object reference is not {@code null} and throws a customized {@link ArchiveException} if it is. * + * + * @param obj the object reference to check for nullity. + * @param messageSupplier supplier of the detail message to be used in the event that a {@code ArchiveException} is thrown + * @param <T> the type of the reference. + * @return {@code obj} if not {@code null} + * @throws ArchiveException if {@code obj} is {@code null} + * @since 1.28.0 + */ + public static <T> T requireNonNull(final T obj, final Supplier<String> messageSupplier) throws ArchiveException { + if (obj == null) { + throw new ArchiveException(messageSupplier == null ? null : messageSupplier.get()); + } + return obj; + } + /** * Constructs a new exception with the specified detail message. The cause is not initialized. * 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 73402796f..0534f1554 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 @@ -50,6 +50,7 @@ import java.util.zip.CheckedInputStream; import org.apache.commons.compress.MemoryLimitException; +import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.utils.ByteUtils; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.InputStreamStatistics; @@ -1290,11 +1291,11 @@ private void readFilesInfo(final ByteBuffer header, final Archive archive) throw break; } case NID.kEmptyFile: { - isEmptyFile = readBits(header, isEmptyStream.cardinality()); + isEmptyFile = readBits(header, ArchiveException.requireNonNull(isEmptyStream, () -> "isEmptyStream for " + archive).cardinality()); break; } case NID.kAnti: { - isAnti = readBits(header, isEmptyStream.cardinality()); + isAnti = readBits(header, ArchiveException.requireNonNull(isEmptyStream, () -> "isEmptyStream for " + archive).cardinality()); break; } case NID.kName: { @@ -1372,11 +1373,9 @@ private void readFilesInfo(final ByteBuffer header, final Archive archive) throw case NID.kDummy: { // 7z 9.20 asserts the content is all zeros and ignores the property // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287 - skipBytesFully(header, size); break; } - default: { // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287 skipBytesFully(header, size);