This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit bd1331f364c58e96311102b7d6e400944f0d4680 Author: Gary Gregory <[email protected]> AuthorDate: Mon Aug 10 16:41:48 2026 -0400 SevenZFile.getInputStream(SevenZArchiveEntry) now throws ArchiveException instead of IllehalArgumentException. More internal exception clean ups. --- src/changes/changes.xml | 1 + .../compress/archivers/sevenz/SevenZFile.java | 31 +++++++++++----------- .../compress/archivers/sevenz/SubStreamsInfo.java | 6 ++--- .../compress/archivers/sevenz/SevenZFileTest.java | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9c32acb97..902ee3957 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -171,6 +171,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">ParsingUtils now throws the IOException subclass CompressException.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Most ArArchiveEntry constructors now throws the IOException subclass ArchiveException.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">TarArchiveEntry.addPaxHeader(String, String) now throws ArchiveException instead of IllegalArgumentException.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">SevenZFile.getInputStream(SevenZArchiveEntry) now throws ArchiveException instead of IllehalArgumentException.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add MemoryLimitException.MemoryLimitException(long, long).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add CompressException.CompressException(String, Object...).</action> 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 55677407d..138150631 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 @@ -39,6 +39,7 @@ import java.util.Objects; import java.util.zip.CRC32; +import org.apache.commons.compress.CompressException; import org.apache.commons.compress.MemoryLimitException; import org.apache.commons.compress.archivers.AbstractArchiveBuilder; import org.apache.commons.compress.archivers.ArchiveException; @@ -93,7 +94,7 @@ private static final class ArchiveStatistics { * @param maxMemoryLimitKiB kibibytes (KiB) to test. * @throws IOException Thrown on basic assertion failure. */ - void assertValidity(final int maxMemoryLimitKiB) throws IOException { + void assertValidity(final int maxMemoryLimitKiB) throws CompressException { if (numberOfEntriesWithStream > 0 && numberOfFolders == 0) { throw new ArchiveException("7z archive with entries but no folders"); } @@ -1019,7 +1020,7 @@ private void buildDecodingStream(final int entryIndex, final boolean isRandomAcc deferredBlockStreams.add(fileStream); } - private void calculateStreamMap(final Archive archive) throws IOException { + private void calculateStreamMap(final Archive archive) throws CompressException { int nextFolderPackStreamIndex = 0; final int numFolders = archive.folders.length; final int[] folderFirstPackStreamIndex = intArray(numFolders); @@ -1096,7 +1097,7 @@ private InputStream getCurrentStream() throws IOException { return new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY); } if (deferredBlockStreams.isEmpty()) { - throw new IllegalStateException("No current 7z entry (call getNextEntry() first)."); + throw new ArchiveException("No current 7z entry (call getNextEntry() first)."); } while (deferredBlockStreams.size() > 1) { // In solid compression mode we need to decompress all leading folder' @@ -1173,7 +1174,7 @@ public InputStream getInputStream(final SevenZArchiveEntry entry) throws IOExcep } } if (entryIndex < 0) { - throw new IllegalArgumentException("Can not find " + entry.getName() + " in " + fileName); + throw new ArchiveException("Can not find " + entry.getName() + " in " + fileName); } buildDecodingStream(entryIndex, true); currentEntryIndex = entryIndex; @@ -1355,7 +1356,7 @@ public int read(final byte[] b, final int off, final int len) throws IOException return current; } - private BitSet readAllOrBits(final ByteBuffer header, final int size) throws IOException { + private BitSet readAllOrBits(final ByteBuffer header, final int size) throws ArchiveException { final int areAllDefined = getUnsignedByte(header); final BitSet bits; if (areAllDefined != 0) { @@ -1369,7 +1370,7 @@ private BitSet readAllOrBits(final ByteBuffer header, final int size) throws IOE return bits; } - private void readArchiveProperties(final ByteBuffer header) throws IOException { + private void readArchiveProperties(final ByteBuffer header) throws ArchiveException { // FIXME: the reference implementation just throws them away? long nid = readUint64(header); while (nid != NID.kEnd) { @@ -1380,7 +1381,7 @@ private void readArchiveProperties(final ByteBuffer header) throws IOException { } } - private BitSet readBits(final ByteBuffer header, final int size) throws IOException { + private BitSet readBits(final ByteBuffer header, final int size) throws ArchiveException { ensureRemaining(header, (size + 7) / 8); final BitSet bits = new BitSet(size); int mask = 0; @@ -1590,7 +1591,7 @@ private void readFilesInfo(final ByteBuffer header, final Archive archive) throw calculateStreamMap(archive); } - Folder readFolder(final ByteBuffer header) throws IOException { + Folder readFolder(final ByteBuffer header) throws CompressException { final Folder folder = new Folder(); final long numCoders = readUint64(header); if (numCoders == 0 || numCoders > MAX_CODERS_PER_FOLDER) { @@ -1737,7 +1738,7 @@ private Archive readHeaders(final byte[] password) throws IOException { + "the archive could be a multi volume archive that has been closed prematurely."); } - private void readPackInfo(final ByteBuffer header, final Archive archive) throws IOException { + private void readPackInfo(final ByteBuffer header, final Archive archive) throws CompressException { archive.packPos = readUint64(header); final int numPackStreamsInt = readUint64ToIntExact(header, "numPackStreams"); int nid = getUnsignedByte(header); @@ -1778,7 +1779,7 @@ private StartHeader readStartHeader(final ByteBuffer startHeader) throws IOExcep return new StartHeader(nextHeaderOffset, nextHeaderSize, nextHeaderCrc); } - private void readStreamsInfo(final ByteBuffer header, final Archive archive) throws IOException { + private void readStreamsInfo(final ByteBuffer header, final Archive archive) throws CompressException { int nid = getUnsignedByte(header); if (nid == NID.kPackInfo) { readPackInfo(header, archive); @@ -1797,7 +1798,7 @@ private void readStreamsInfo(final ByteBuffer header, final Archive archive) thr } } - private void readSubStreamsInfo(final ByteBuffer header, final Archive archive) throws IOException { + private void readSubStreamsInfo(final ByteBuffer header, final Archive archive) throws CompressException { for (final Folder folder : archive.folders) { folder.numUnpackSubStreams = 1; } @@ -1870,7 +1871,7 @@ private void readSubStreamsInfo(final ByteBuffer header, final Archive archive) archive.subStreamsInfo = subStreamsInfo; } - private void readUnpackInfo(final ByteBuffer header, final Archive archive) throws IOException { + private void readUnpackInfo(final ByteBuffer header, final Archive archive) throws CompressException { int nid = getUnsignedByte(header); final int numFoldersInt = readUint64ToIntExact(header, "numFolders"); /* final int external = */ getUnsignedByte(header); @@ -1953,7 +1954,7 @@ private ArchiveStatistics sanityCheckAndCollectStatistics(final ByteBuffer heade return stats; } - private void sanityCheckArchiveProperties(final ByteBuffer header) throws IOException { + private void sanityCheckArchiveProperties(final ByteBuffer header) throws ArchiveException { long nid = readUint64(header); while (nid != NID.kEnd) { // We validate the size but ignore the value @@ -1963,7 +1964,7 @@ private void sanityCheckArchiveProperties(final ByteBuffer header) throws IOExce } } - private void sanityCheckFilesInfo(final ByteBuffer header, final ArchiveStatistics stats) throws IOException { + private void sanityCheckFilesInfo(final ByteBuffer header, final ArchiveStatistics stats) throws ArchiveException { stats.numberOfEntries = readUint64ToIntExact(header, "numFiles"); int emptyStreams = -1; final int originalLimit = header.limit(); @@ -2346,7 +2347,7 @@ public IOStream<? extends SevenZArchiveEntry> stream() { * @return A byte array containing the data from the buffer. * @throws IOException if there are insufficient resources to allocate the array or insufficient data in the buffer. */ - private byte[] toByteArray(final ByteBuffer header, final int size) throws IOException { + private byte[] toByteArray(final ByteBuffer header, final int size) throws CompressException { // Check if we have enough resources to allocate the array MemoryLimitException.checkKiB(bytesToKiB(size * Byte.BYTES), maxMemoryLimitKiB); final byte[] result = new byte[size]; diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java index 6666598e9..fe404de04 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java @@ -21,7 +21,7 @@ import java.util.BitSet; -import org.apache.commons.compress.CompressException; +import org.apache.commons.compress.archivers.ArchiveException; /** * Properties for non-empty files. @@ -43,7 +43,7 @@ final class SubStreamsInfo { */ final long[] crcs; - SubStreamsInfo(final int totalUnpackStreams, final int maxMemoryLimitKiB) throws CompressException { + SubStreamsInfo(final int totalUnpackStreams, final int maxMemoryLimitKiB) throws ArchiveException { long alloc; try { // 2 long arrays, just count the longs @@ -52,7 +52,7 @@ final class SubStreamsInfo { final int sizeOfBitSet = Math.multiplyExact(Long.BYTES, (totalUnpackStreams - 1 >> 6) + 1); alloc = Math.addExact(alloc, Math.multiplyExact(totalUnpackStreams, sizeOfBitSet)); } catch (final ArithmeticException e) { - throw new CompressException("Cannot create allocation request for a SubStreamsInfo of totalUnpackStreams %,d, maxMemoryLimitKiB %,d: %s", + throw new ArchiveException("Cannot create allocation request for a SubStreamsInfo of totalUnpackStreams %,d, maxMemoryLimitKiB %,d: %s", totalUnpackStreams, maxMemoryLimitKiB, e); } // Avoid false positives. diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java index 1962d1f17..67efd1080 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java @@ -459,7 +459,7 @@ void testExtractNonExistSpecifiedFile() throws Exception { try (SevenZFile sevenZFile = getSevenZFile("COMPRESS-256.7z"); SevenZFile anotherSevenZFile = getSevenZFile("bla.7z")) { for (final SevenZArchiveEntry nonExistEntry : anotherSevenZFile.getEntries()) { - assertThrows(IllegalArgumentException.class, () -> sevenZFile.getInputStream(nonExistEntry)); + assertThrows(ArchiveException.class, () -> sevenZFile.getInputStream(nonExistEntry)); } } }
