Class MemoryLimitsAwareHandler
The handler is primarily used during decoding (decompression) of PDF streams, but also guards against other potentially unbounded structures during reading, e.g. cross-reference (xref) table growth and per-page XObject usage.
How it is configured
A configured handler can be set on ReaderProperties and will be used by PdfReader when opening a document.
Note that MemoryLimitsAwareHandler must not be shared between different PdfDocument instances.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates aMemoryLimitsAwareHandlerto handle decompression of PDF streams and other memory-related limits during reading.MemoryLimitsAwareHandler(long documentSize) Creates aMemoryLimitsAwareHandlerwith calculated memory limits for decompression and xref structures based on the provided document size. -
Method Summary
Modifier and TypeMethodDescriptionprotected static intcalculateMaxElementsInXref(long documentSizeInBytes) Calculate max number of elements allowed in xref table based on the size of the document, achieving max limit at 100MB.voidcheckIfPageSizeExceedsTheLimit(long totalPageSize) Deprecated.voidcheckIfXrefStructureExceedsTheLimit(int requestedCapacity) Performs a check of possible extension of xref structure.Creates a new instance ofMemoryLimitsAwareHandlerby copying settings from this instance ofMemoryLimitsAwareHandler.intGets maximum number of elements in xref structure.longGets the maximum allowed size which can be occupied by all decompressed PDF streams.intReturns the maximum allowed size (in bytes) of a single decompressed PDF stream.longDeprecated.in favor ofsetMaxSizeOfDecompressedPdfStreamsSum(long)booleanDeprecated.to be made protected and accept stream dictionary instead ofPdfArrayof filtersvoidsetMaxNumberOfElementsInXrefStructure(int maxNumberOfElementsInXrefStructure) Sets maximum number of elements in xref structure.setMaxSizeOfDecompressedPdfStreamsSum(long maxSizeOfDecompressedPdfStreamsSum) Sets the maximum allowed size which can be occupied by all decompressed PDF streams.setMaxSizeOfSingleDecompressedPdfStream(int maxSizeOfSingleDecompressedPdfStream) Sets the maximum allowed size which can be occupied by a single decompressed PDF stream.voidsetMaxXObjectsSizePerPage(long maxPageSize) Deprecated.in favor ofsetMaxSizeOfDecompressedPdfStreamsSum(long)
-
Constructor Details
-
MemoryLimitsAwareHandler
public MemoryLimitsAwareHandler()Creates aMemoryLimitsAwareHandlerto handle decompression of PDF streams and other memory-related limits during reading. -
MemoryLimitsAwareHandler
public MemoryLimitsAwareHandler(long documentSize) Creates aMemoryLimitsAwareHandlerwith calculated memory limits for decompression and xref structures based on the provided document size.- Parameters:
-
documentSize- the size of the document (in bytes)
-
-
Method Details
-
createNewInstance
Creates a new instance ofMemoryLimitsAwareHandlerby copying settings from this instance ofMemoryLimitsAwareHandler.- Returns:
-
a new instance of
MemoryLimitsAwareHandler
-
getMaxSizeOfSingleDecompressedPdfStream
public int getMaxSizeOfSingleDecompressedPdfStream()Returns the maximum allowed size (in bytes) of a single decompressed PDF stream.- Returns:
- maximum allowed size of a single decompressed stream in bytes
-
setMaxSizeOfSingleDecompressedPdfStream
public MemoryLimitsAwareHandler setMaxSizeOfSingleDecompressedPdfStream(int maxSizeOfSingleDecompressedPdfStream) Sets the maximum allowed size which can be occupied by a single decompressed PDF stream. This value correlates with maximum heap size. This value should not exceed limit of the heap size.iText will throw an exception if during decompression a PDF stream which was identified as requiring memory limits awareness occupies more memory than allowed.
- Parameters:
-
maxSizeOfSingleDecompressedPdfStream- the maximum allowed size which can be occupied by a single decompressed PDF stream - Returns:
-
this
MemoryLimitsAwareHandlerinstance - See Also:
-
getMaxSizeOfDecompressedPdfStreamsSum
public long getMaxSizeOfDecompressedPdfStreamsSum()Gets the maximum allowed size which can be occupied by all decompressed PDF streams.- Returns:
- the maximum allowed size value which streams may occupy
-
setMaxSizeOfDecompressedPdfStreamsSum
public MemoryLimitsAwareHandler setMaxSizeOfDecompressedPdfStreamsSum(long maxSizeOfDecompressedPdfStreamsSum) Sets the maximum allowed size which can be occupied by all decompressed PDF streams. This value can be limited by the maximum expected PDF file size when it's completely decompressed. Setting this value correlates with the maximum processing time spent on document readingiText will throw an exception if during decompression of PDF streams which were identified as requiring memory limits awareness occupy more memory than allowed.
Note that if a PDF stream is decompressed multiple times, it is counted multiple times for this limit.
- Parameters:
-
maxSizeOfDecompressedPdfStreamsSum- he maximum allowed size which can be occupied by all decompressed PDF streams - Returns:
-
this
MemoryLimitsAwareHandlerinstance - See Also:
-
isMemoryLimitsAwarenessRequiredOnDecompression
Deprecated.to be made protected and accept stream dictionary instead ofPdfArrayof filtersPerforms a check if thePdfStreamwith provided setup of the filters requires memory limits awareness during decompression.- Parameters:
-
filters- is anPdfArrayof names of filters - Returns:
-
trueif PDF stream is suspicious andfalseotherwise
-
getMaxNumberOfElementsInXrefStructure
public int getMaxNumberOfElementsInXrefStructure()Gets maximum number of elements in xref structure.- Returns:
- maximum number of elements in xref structure
-
getMaxXObjectsSizePerPage
Deprecated.in favor ofsetMaxSizeOfDecompressedPdfStreamsSum(long)Gets maximum page size.- Returns:
- maximum page size
-
setMaxXObjectsSizePerPage
Deprecated.in favor ofsetMaxSizeOfDecompressedPdfStreamsSum(long)Sets maximum page size.- Parameters:
-
maxPageSize- maximum page size
-
setMaxNumberOfElementsInXrefStructure
public void setMaxNumberOfElementsInXrefStructure(int maxNumberOfElementsInXrefStructure) Sets maximum number of elements in xref structure.- Parameters:
-
maxNumberOfElementsInXrefStructure- maximum number of elements in xref structure
-
checkIfXrefStructureExceedsTheLimit
public void checkIfXrefStructureExceedsTheLimit(int requestedCapacity) Performs a check of possible extension of xref structure.- Parameters:
-
requestedCapacity- capacity to which we need to expand xref array
-
checkIfPageSizeExceedsTheLimit
Deprecated.in favor ofsetMaxSizeOfDecompressedPdfStreamsSum(long). Also get rid of its usage inPdfCanvasProcessorChecks if page size limit is exceeded.- Parameters:
-
totalPageSize- total size of the read page content
-
calculateMaxElementsInXref
protected static int calculateMaxElementsInXref(long documentSizeInBytes) Calculate max number of elements allowed in xref table based on the size of the document, achieving max limit at 100MB.- Parameters:
-
documentSizeInBytes- document size in bytes - Returns:
- calculated limit
-
setMaxSizeOfDecompressedPdfStreamsSum(long).