Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/apache/commons/compress/utils/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down