From: Maxim Valyanskiy Date: Wed, 18 May 2011 10:38:08 +0000 (+0000) Subject: FakeZipEntry: pre-allocate ByteArrayOutputStream when zip entry size is known to... X-Git-Tag: REL_3_8_BETA3~20 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d399e2ad567517336439bb92271818000a47ed57;p=poi.git FakeZipEntry: pre-allocate ByteArrayOutputStream when zip entry size is known to prevent reallocation git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1124179 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java index 0b9822eab1..45674f897e 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java @@ -108,7 +108,20 @@ public class ZipInputStreamZipEntrySource implements ZipEntrySource { super(entry.getName()); // Grab the de-compressed contents for later - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos; + + long entrySize = entry.getSize(); + + if (entrySize !=-1) { + if (entrySize>=Integer.MAX_VALUE) { + throw new IOException("ZIP entry size is too large"); + } + + baos = new ByteArrayOutputStream((int) entrySize); + } else { + baos = new ByteArrayOutputStream(); + } + byte[] buffer = new byte[4096]; int read = 0; while( (read = inp.read(buffer)) != -1 ) {