From 209a45ee662cf94239718ec84ba909a7b60f43b2 Mon Sep 17 00:00:00 2001 From: Maxim Valyanskiy Date: Wed, 18 May 2011 10:38:08 +0000 Subject: [PATCH] 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 --- .../util/ZipInputStreamZipEntrySource.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 0b9822eab..45674f897 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 ) {