Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,20 @@ public static RandomAccessRead decode(InputStream encoded, List<Filter> filterLi
length = randomAccessWriteBuffer.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
if (length <= 0 || length >= RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB / 4)
// use the estimated stream size as chunk size, capped to avoid excessive per-chunk
// preallocation, but without collapsing back to the tiny default for large streams -
// that would otherwise force thousands of small chunk allocations
if (length <= 0)
{
length = RandomAccessReadBuffer.DEFAULT_CHUNK_SIZE_4KB;
}
else
{
length = length * 4;
// upper bound for the chunk size estimated from the encoded stream length in decode(), to
// avoid thousands of small chunk allocations for large streams while still bounding the
// worst-case preallocation for a single chunk
final int maxEstimatedChunkSize = 1 << 20;
length = Math.min(length, maxEstimatedChunkSize / 4) * 4;
}
randomAccessWriteBuffer = new RandomAccessReadWriteBuffer((int) length);
output = new RandomAccessOutputStream(randomAccessWriteBuffer);
Expand Down