This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch fix/restrict-apis in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 7ccc2209ff9bd96f5b0b4579ce34ad826f4761e1 Author: Piotr P. Karwasz <[email protected]> AuthorDate: Wed Oct 15 16:57:39 2025 +0200 Restrict visibility of unintentionally exposed APIs This PR removes or hides several members that leaked into the public surface, were never meant for external use and were **never released**. There is **no functional behavior change** to the library; this only corrects API visibility and duplication. ## What changed * **`SevenZFile.SOFT_MAX_ARRAY_LENGTH`** * **Change:** Removed. * **Rationale:** Duplicates the constant already available in Commons IO’s `IOUtils`. * **`SevenZFile.toNonNegativeInt(...)`** * **Change:** Visibility reduced (internal helper). * **Rationale:** Not part of the supported API; only used internally. * **`SeekableInMemoryByteChannel.getSize()`** * **Change:** Removed public alias. * **Rationale:** Only used in tests; behavior diverges from `size()` after channel closure and shouldn’t be exposed. * **`ElementValue.BYTES`** * **Change:** Migrated to caller class. * **Rationale:** Had a single call site in another package; not a public contract. --- .../commons/compress/archivers/sevenz/SevenZFile.java | 13 +++---------- .../compress/harmony/unpack200/MetadataBandGroup.java | 5 ++++- .../harmony/unpack200/bytecode/AnnotationsAttribute.java | 3 --- .../java/org/apache/commons/compress/utils/IOUtils.java | 7 +------ .../commons/compress/utils/SeekableInMemoryByteChannel.java | 9 --------- .../apache/commons/compress/archivers/zip/ZipFileTest.java | 4 +++- 6 files changed, 11 insertions(+), 30 deletions(-) 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 4ebf1c619..3b756ab44 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 @@ -22,7 +22,6 @@ import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.EOFException; import java.io.File; @@ -341,13 +340,6 @@ public Builder setUseDefaultNameForUnnamedEntries(final boolean useDefaultNameFo /** Shared with SevenZOutputFile and tests, neither mutates it. */ static final byte[] SIGNATURE = { (byte) '7', (byte) 'z', (byte) 0xBC, (byte) 0xAF, (byte) 0x27, (byte) 0x1C }; - /** - * The maximum array size defined privately in {@link ByteArrayOutputStream}. - * - * @since 1.29.0 - */ - public static int SOFT_MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; - /** * Creates a new Builder. * @@ -461,7 +453,7 @@ private static long skipBytesFully(final ByteBuffer input, long bytesToSkip) { * @return The given value as an int. * @throws IOException Thrown if the given value is not in {@code [0, Integer.MAX_VALUE]}. */ - public static int toNonNegativeInt(final String description, final long value) throws IOException { + private static int toNonNegativeInt(final String description, final long value) throws IOException { if (value > Integer.MAX_VALUE || value < 0) { throw new ArchiveException("Cannot handle %s %,d", description, value); } @@ -1130,7 +1122,8 @@ private boolean hasCurrentEntryBeenRead() { private Archive initializeArchive(final StartHeader startHeader, final byte[] password, final boolean verifyCrc) throws IOException { final int nextHeaderSizeInt = toNonNegativeInt("startHeader.nextHeaderSize", startHeader.nextHeaderSize); - MemoryLimitException.checkKiB(bytesToKiB(nextHeaderSizeInt), Math.min(bytesToKiB(SOFT_MAX_ARRAY_LENGTH), maxMemoryLimitKiB)); + MemoryLimitException.checkKiB(bytesToKiB(nextHeaderSizeInt), Math.min(bytesToKiB(org.apache.commons.io.IOUtils.SOFT_MAX_ARRAY_LENGTH), + maxMemoryLimitKiB)); channel.position(SIGNATURE_HEADER_SIZE + startHeader.nextHeaderOffset); if (verifyCrc) { final long position = channel.position(); diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/MetadataBandGroup.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/MetadataBandGroup.java index 43292679f..1811efdee 100644 --- a/src/main/java/org/apache/commons/compress/harmony/unpack200/MetadataBandGroup.java +++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/MetadataBandGroup.java @@ -44,6 +44,9 @@ */ public class MetadataBandGroup { + /** Size in bytes of an {@link ElementValue} instance: header, Object, int, int. */ + private static final int ELEMENT_VALUE_BYTES = 8 + 8 + Integer.BYTES + Integer.BYTES; + private static CPUTF8 rvaUTF8; private static CPUTF8 riaUTF8; @@ -220,7 +223,7 @@ private Object getNextValue(final int t) throws Pack200Exception { return cases_RU[cases_RU_Index++]; case '[': final int arraySize = casearray_N[casearray_N_Index++]; - final ElementValue[] nestedArray = new ElementValue[Pack200Exception.checkObjectArray(arraySize, ElementValue.BYTES)]; + final ElementValue[] nestedArray = new ElementValue[Pack200Exception.checkObjectArray(arraySize, ELEMENT_VALUE_BYTES)]; for (int i = 0; i < arraySize; i++) { final int nextT = T[T_index++]; nestedArray[i] = new ElementValue(nextT, getNextValue(nextT)); diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/AnnotationsAttribute.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/AnnotationsAttribute.java index fd09af326..e08110dea 100644 --- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/AnnotationsAttribute.java +++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/AnnotationsAttribute.java @@ -127,9 +127,6 @@ public void writeBody(final DataOutputStream dos) throws IOException { */ public static class ElementValue { - /** Size in bytes of an instance: header, Object, int, int. */ - public static final int BYTES = 8 + 8 + Integer.BYTES + Integer.BYTES; - private final Object value; private final int tag; diff --git a/src/main/java/org/apache/commons/compress/utils/IOUtils.java b/src/main/java/org/apache/commons/compress/utils/IOUtils.java index 1eedd52b7..0bdd1a36b 100644 --- a/src/main/java/org/apache/commons/compress/utils/IOUtils.java +++ b/src/main/java/org/apache/commons/compress/utils/IOUtils.java @@ -48,11 +48,6 @@ public final class IOUtils { */ public static final LinkOption[] EMPTY_LINK_OPTIONS = {}; - /** - * The {@code SOFT_MAX_ARRAY_LENGTH} constant from Java's internal ArraySupport class. - */ - private static final int SOFT_MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; - /** * Closes the given Closeable and swallows any IOException that may occur. * @@ -235,7 +230,7 @@ public static void readFully(final ReadableByteChannel channel, final ByteBuffer */ public static byte[] readRange(final InputStream input, final int length) throws IOException { final ByteArrayOutputStream output = new ByteArrayOutputStream(); - org.apache.commons.io.IOUtils.copyLarge(input, output, 0, MemoryLimitException.checkBytes(length, SOFT_MAX_ARRAY_LENGTH)); + org.apache.commons.io.IOUtils.copyLarge(input, output, 0, MemoryLimitException.checkBytes(length, org.apache.commons.io.IOUtils.SOFT_MAX_ARRAY_LENGTH)); return output.toByteArray(); } diff --git a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java index 7c6a8e542..2f998c0e2 100644 --- a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java +++ b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java @@ -99,15 +99,6 @@ private void ensureOpen() throws ClosedChannelException { } } - /** - * Like {@link #size()} but never throws {@link ClosedChannelException}. - * - * @return See {@link #size()}. - */ - public long getSize() { - return size; - } - @Override public boolean isOpen() { return !closed.get(); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java index e047753c4..6dc78d315 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java @@ -66,6 +66,7 @@ import org.apache.commons.lang3.ArrayFill; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Assume; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assumptions; @@ -500,7 +501,8 @@ void testEntryAlignment() throws Exception { } - try (ZipFile zf = ZipFile.builder().setByteArray(Arrays.copyOfRange(zipContent.array(), 0, (int) zipContent.getSize())).get()) { + try (ZipFile zf = ZipFile.builder().setByteArray(Arrays.copyOfRange(zipContent.array(), 0, (int) FieldUtils.readDeclaredField(zipContent, "size", + true))).get()) { final ZipArchiveEntry inflatedEntry = zf.getEntry("inflated.txt"); final ResourceAlignmentExtraField inflatedAlignmentEx = (ResourceAlignmentExtraField) inflatedEntry .getExtraField(ResourceAlignmentExtraField.ID);
