From b9b817fefc09e2c689cbbcbfbf305f0795a10abc Mon Sep 17 00:00:00 2001 From: Tilman Hausherr Date: Wed, 27 Nov 2024 15:36:04 +0000 Subject: [PATCH] PDFBOX-5908: refactor git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922168 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/pdfbox/filter/Filter.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java b/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java index 04a9f155041..24f1b27f37a 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java @@ -232,7 +232,7 @@ public static RandomAccessRead decode(InputStream encoded, List filterLi COSDictionary parameters, DecodeOptions options, List results) throws IOException { - int length = parameters.getInt(COSName.LENGTH, + long length = parameters.getLong(COSName.LENGTH, RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB); if (filterList.isEmpty()) { @@ -266,17 +266,19 @@ public static RandomAccessRead decode(InputStream encoded, List filterLi { randomAccessWriteBuffer.seek(0); input = new RandomAccessInputStream(randomAccessWriteBuffer); - length = (int) randomAccessWriteBuffer.length(); + length = randomAccessWriteBuffer.length(); } - // avoid invalid values - length = length <= 0 ? RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB : length; // we don't know the size of the decoded stream, just estimate a 4 times bigger size than the encoded stream // use the estimated stream size as chunk size, use the default chunk size as limit to avoid to big values - length <<= 2; - length = length <= 0 ? // PDFBOX-5908 avoid invalid values (again) - RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB : - Math.min(length, RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB); - randomAccessWriteBuffer = new RandomAccessReadWriteBuffer(length); + if (length >= RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB / 4) + { + length = RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB; + } + else + { + length = Math.min(length << 2, RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB); + } + randomAccessWriteBuffer = new RandomAccessReadWriteBuffer((int) length); output = new RandomAccessOutputStream(randomAccessWriteBuffer); try {