diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java b/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java index 41a26f0c..7a6bc952 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java @@ -57,6 +57,12 @@ public class ByteArrayOutputStream extends OutputStream { */ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + /** + * Maximum size of a single buffer (16MB) to prevent excessive memory allocation. + * When buffers need to grow beyond this size, additional buffers are created instead. + */ + private static final int MAX_BUFFER_SIZE = 16 * 1024 * 1024; + /** * The list of buffers, which grows and never reduces. */ @@ -136,6 +142,9 @@ private void needNewBuffer(final int newcount) { filledBufferSum += currentBuffer.length; } + // Cap the buffer size to prevent excessive memory allocation + newBufferSize = Math.min(newBufferSize, MAX_BUFFER_SIZE); + currentBufferIndex++; currentBuffer = new byte[newBufferSize]; buffers.add(currentBuffer); diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java b/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java index e431efe7..18c6ae09 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java @@ -112,7 +112,7 @@ public ConcurrentJarCreator(int nThreads) throws IOException { */ public ConcurrentJarCreator(boolean compressAddedZips, int nThreads) throws IOException { this.compressAddedZips = compressAddedZips; - ScatterGatherBackingStoreSupplier defaultSupplier = new DeferredSupplier(100000000 / nThreads); + ScatterGatherBackingStoreSupplier defaultSupplier = new DeferredSupplier(10000000 / nThreads); metaInfDir = createDeferred(defaultSupplier); manifest = createDeferred(defaultSupplier); directories = createDeferred(defaultSupplier);