From 7005a44cc72f07564a197769fd3593f4e86b131f Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 19 Feb 2022 22:13:26 +0000 Subject: [PATCH] add extra max size config git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898237 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFChart.java | 2 +- .../poi/xwpf/usermodel/XWPFComments.java | 2 +- .../poi/xwpf/usermodel/XWPFDocument.java | 2 +- .../poi/xwpf/usermodel/XWPFHeaderFooter.java | 2 +- .../poi/xwpf/usermodel/XWPFPictureData.java | 27 ++++++++++++++----- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFChart.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFChart.java index 126a553f36..1c51924507 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFChart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFChart.java @@ -94,7 +94,7 @@ public class XWPFChart extends XDDFChart { if (this.checksum == null) { byte[] data; try (InputStream is = getPackagePart().getInputStream()) { - data = IOUtils.toByteArray(is); + data = IOUtils.toByteArray(is, XWPFPictureData.getMaxImageSize()); } catch (IOException e) { throw new POIXMLException(e); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java index e6b62b8f6e..52253b6a97 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java @@ -107,7 +107,7 @@ public class XWPFComments extends POIXMLDocumentPart { * @throws IOException If reading the picture-data from the stream fails. */ public String addPictureData(InputStream is, int format) throws InvalidFormatException, IOException { - byte[] data = IOUtils.toByteArray(is); + byte[] data = IOUtils.toByteArray(is, XWPFPictureData.getMaxImageSize()); return addPictureData(data, format); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 65936bf9dc..62192da980 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -1520,7 +1520,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { public String addPictureData(InputStream is, int format) throws InvalidFormatException { try { - byte[] data = IOUtils.toByteArray(is); + byte[] data = IOUtils.toByteArray(is, XWPFPictureData.getMaxImageSize()); return addPictureData(data, format); } catch (IOException e) { throw new POIXMLException(e); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java index a839c587cf..4d423e1cbf 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java @@ -279,7 +279,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo * @throws IOException If reading the picture-data from the stream fails. */ public String addPictureData(InputStream is, int format) throws InvalidFormatException, IOException { - byte[] data = IOUtils.toByteArray(is); + byte[] data = IOUtils.toByteArray(is, XWPFPictureData.getMaxImageSize()); return addPictureData(data, format); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java index d4ea1849b4..20cebfc175 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java @@ -34,6 +34,23 @@ import org.apache.poi.util.IOUtils; */ public class XWPFPictureData extends POIXMLDocumentPart { + private static final int DEFAULT_MAX_IMAGE_SIZE = 100_000_000; + private static int MAX_IMAGE_SIZE = DEFAULT_MAX_IMAGE_SIZE; + + /** + * @param length the max image size allowed for XSSF pictures + */ + public static void setMaxImageSize(int length) { + MAX_IMAGE_SIZE = length; + } + + /** + * @return the max image size allowed for XSSF pictures + */ + public static int getMaxImageSize() { + return MAX_IMAGE_SIZE; + } + /** * Relationships for each known picture type */ @@ -94,7 +111,7 @@ public class XWPFPictureData extends POIXMLDocumentPart { */ public byte[] getData() { try (InputStream stream = getPackagePart().getInputStream()) { - return IOUtils.toByteArray(stream); + return IOUtils.toByteArray(stream, getMaxImageSize()); } catch (IOException e) { throw new POIXMLException(e); } @@ -146,15 +163,11 @@ public class XWPFPictureData extends POIXMLDocumentPart { public Long getChecksum() { if (this.checksum == null) { - InputStream is = null; byte[] data; - try { - is = getPackagePart().getInputStream(); - data = IOUtils.toByteArray(is); + try (InputStream is = getPackagePart().getInputStream()) { + data = IOUtils.toByteArray(is, getMaxImageSize()); } catch (IOException e) { throw new POIXMLException(e); - } finally { - IOUtils.closeQuietly(is); } this.checksum = IOUtils.calculateChecksum(data); } -- 2.39.5