Package com.itextpdf.kernel.pdf
Class MemoryLimitsAwareHandler
- java.lang.Object
-
- com.itextpdf.kernel.pdf.MemoryLimitsAwareHandler
-
public class MemoryLimitsAwareHandler extends java.lang.ObjectAMemoryLimitsAwareHandlerhandles memory allocation and prevents decompressed pdf streams from occupation of more space than allowed.A configured MemoryLimitsAwareHandler can be set as a property of
ReaderPropertiesinstance which is passed toPdfReader.
-
-
Field Summary
Fields Modifier and Type Field Description private longallMemoryUsedForDecompression(package private) booleanconsiderCurrentPdfStreamprivate static org.slf4j.LoggerLOGGERprivate static intMAX_NUMBER_OF_ELEMENTS_IN_XREF_STRUCTUREprivate static longMAX_X_OBJECTS_SIZE_PER_PAGEprivate intmaxNumberOfElementsInXrefStructureprivate longmaxSizeOfDecompressedPdfStreamsSumprivate intmaxSizeOfSingleDecompressedPdfStreamprivate longmaxXObjectsSizePerPageprivate longmemoryUsedForCurrentPdfStreamDecompressionprivate static intMIN_LIMIT_FOR_NUMBER_OF_ELEMENTS_IN_XREF_STRUCTUREprivate static intSINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZEprivate static intSINGLE_SCALE_COEFFICIENTprivate static longSUM_OF_DECOMPRESSED_PDF_STREAMS_MIN_SIZEprivate static intSUM_SCALE_COEFFICIENT
-
Constructor Summary
Constructors Modifier Constructor Description MemoryLimitsAwareHandler()Creates aMemoryLimitsAwareHandlerwhich will be used to handle decompression of pdf streams.privateMemoryLimitsAwareHandler(int maxSizeOfSingleDecompressedPdfStream, long maxSizeOfDecompressedPdfStreamsSum, int maxNumberOfElementsInXrefStructure, long maxXObjectsSizePerPage)MemoryLimitsAwareHandler(long documentSize)Creates aMemoryLimitsAwareHandlerwhich will be used to handle decompression of pdf streams.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) MemoryLimitsAwareHandlerbeginDecompressedPdfStreamProcessing()Begins handling of current pdf stream decompression.private static longcalculateDefaultParameter(long documentSize, int scale, long min)protected 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 totalXObjectsSize)voidcheckIfXrefStructureExceedsTheLimit(int requestedCapacity)Performs a check of possible extension of xref structure.(package private) MemoryLimitsAwareHandlerconsiderBytesOccupiedByDecompressedPdfStream(long numOfOccupiedBytes)Considers the number of bytes which are occupied by the decompressed pdf stream.MemoryLimitsAwareHandlercreateNewInstance()Creates a new instance ofMemoryLimitsAwareHandlerby copying settings from this instance ofMemoryLimitsAwareHandler.(package private) MemoryLimitsAwareHandlerendDecompressedPdfStreamProcessing()Ends handling of current pdf stream decompression.private voidensureCurrentStreamIsReset()(package private) longgetAllMemoryUsedForDecompression()intgetMaxNumberOfElementsInXrefStructure()Gets maximum number of elements in xref structure.longgetMaxSizeOfDecompressedPdfStreamsSum()Gets the maximum allowed size which can be occupied by all decompressed pdf streams.intgetMaxSizeOfSingleDecompressedPdfStream()Gets the maximum allowed size which can be occupied by a single decompressed pdf stream.longgetMaxXObjectsSizePerPage()Gets maximum page size.booleanisMemoryLimitsAwarenessRequiredOnDecompression(PdfArray filters)Performs a check if thePdfStreamwith provided setup of the filters requires memory limits awareness during decompression.voidsetMaxNumberOfElementsInXrefStructure(int maxNumberOfElementsInXrefStructure)Sets maximum number of elements in xref structure.MemoryLimitsAwareHandlersetMaxSizeOfDecompressedPdfStreamsSum(long maxSizeOfDecompressedPdfStreamsSum)Sets the maximum allowed size which can be occupied by all decompressed pdf streams.MemoryLimitsAwareHandlersetMaxSizeOfSingleDecompressedPdfStream(int maxSizeOfSingleDecompressedPdfStream)Sets the maximum allowed size which can be occupied by a single decompressed pdf stream.voidsetMaxXObjectsSizePerPage(long maxPageSize)Sets maximum page size.
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
SINGLE_SCALE_COEFFICIENT
private static final int SINGLE_SCALE_COEFFICIENT
- See Also:
- Constant Field Values
-
SUM_SCALE_COEFFICIENT
private static final int SUM_SCALE_COEFFICIENT
- See Also:
- Constant Field Values
-
MAX_NUMBER_OF_ELEMENTS_IN_XREF_STRUCTURE
private static final int MAX_NUMBER_OF_ELEMENTS_IN_XREF_STRUCTURE
- See Also:
- Constant Field Values
-
MIN_LIMIT_FOR_NUMBER_OF_ELEMENTS_IN_XREF_STRUCTURE
private static final int MIN_LIMIT_FOR_NUMBER_OF_ELEMENTS_IN_XREF_STRUCTURE
- See Also:
- Constant Field Values
-
SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE
private static final int SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE
- See Also:
- Constant Field Values
-
SUM_OF_DECOMPRESSED_PDF_STREAMS_MIN_SIZE
private static final long SUM_OF_DECOMPRESSED_PDF_STREAMS_MIN_SIZE
- See Also:
- Constant Field Values
-
MAX_X_OBJECTS_SIZE_PER_PAGE
private static final long MAX_X_OBJECTS_SIZE_PER_PAGE
- See Also:
- Constant Field Values
-
maxSizeOfSingleDecompressedPdfStream
private int maxSizeOfSingleDecompressedPdfStream
-
maxSizeOfDecompressedPdfStreamsSum
private long maxSizeOfDecompressedPdfStreamsSum
-
maxNumberOfElementsInXrefStructure
private int maxNumberOfElementsInXrefStructure
-
maxXObjectsSizePerPage
private long maxXObjectsSizePerPage
-
allMemoryUsedForDecompression
private long allMemoryUsedForDecompression
-
memoryUsedForCurrentPdfStreamDecompression
private long memoryUsedForCurrentPdfStreamDecompression
-
considerCurrentPdfStream
boolean considerCurrentPdfStream
-
-
Constructor Detail
-
MemoryLimitsAwareHandler
public MemoryLimitsAwareHandler()
Creates aMemoryLimitsAwareHandlerwhich will be used to handle decompression of pdf streams. The max allowed memory limits will be generated by default.
-
MemoryLimitsAwareHandler
public MemoryLimitsAwareHandler(long documentSize)
Creates aMemoryLimitsAwareHandlerwhich will be used to handle decompression of pdf streams. The max allowed memory limits will be generated by default, based on the size of the document.- Parameters:
documentSize- the size of the document, which is going to be handled by iText.
-
MemoryLimitsAwareHandler
private MemoryLimitsAwareHandler(int maxSizeOfSingleDecompressedPdfStream, long maxSizeOfDecompressedPdfStreamsSum, int maxNumberOfElementsInXrefStructure, long maxXObjectsSizePerPage)
-
-
Method Detail
-
createNewInstance
public MemoryLimitsAwareHandler createNewInstance()
Creates a new instance ofMemoryLimitsAwareHandlerby copying settings from this instance ofMemoryLimitsAwareHandler.- Returns:
- a new instance of
MemoryLimitsAwareHandler.
-
getMaxSizeOfSingleDecompressedPdfStream
public int getMaxSizeOfSingleDecompressedPdfStream()
Gets the maximum allowed size which can be occupied by a single decompressed pdf stream.- Returns:
- the maximum allowed size which can be occupied by a single decompressed pdf stream.
-
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:
isMemoryLimitsAwarenessRequiredOnDecompression(PdfArray)
-
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 pdf streams which were identified as requiring memory limits awareness occupy more memory than allowed.
- Parameters:
maxSizeOfDecompressedPdfStreamsSum- he maximum allowed size which can be occupied by all decompressed pdf streams.- Returns:
- this
MemoryLimitsAwareHandlerinstance. - See Also:
isMemoryLimitsAwarenessRequiredOnDecompression(PdfArray)
-
isMemoryLimitsAwarenessRequiredOnDecompression
public boolean isMemoryLimitsAwarenessRequiredOnDecompression(PdfArray filters)
Performs a check if thePdfStreamwith provided setup of the filters requires memory limits awareness during decompression.- Parameters:
filters- is anPdfArrayof names of filters- Returns:
- true if PDF stream is suspicious and false otherwise
-
getMaxNumberOfElementsInXrefStructure
public int getMaxNumberOfElementsInXrefStructure()
Gets maximum number of elements in xref structure.- Returns:
- maximum number of elements in xref structure.
-
getMaxXObjectsSizePerPage
public long getMaxXObjectsSizePerPage()
Gets maximum page size.- Returns:
- maximum page size.
-
setMaxXObjectsSizePerPage
public void setMaxXObjectsSizePerPage(long maxPageSize)
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
public void checkIfPageSizeExceedsTheLimit(long totalXObjectsSize)
-
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.
-
considerBytesOccupiedByDecompressedPdfStream
MemoryLimitsAwareHandler considerBytesOccupiedByDecompressedPdfStream(long numOfOccupiedBytes)
Considers the number of bytes which are occupied by the decompressed pdf stream. If memory limits have not been faced, throws an exception.- Parameters:
numOfOccupiedBytes- the number of bytes which are occupied by the decompressed pdf stream.- Returns:
- this
MemoryLimitsAwareHandlerinstance. - See Also:
MemoryLimitsAwareException
-
beginDecompressedPdfStreamProcessing
MemoryLimitsAwareHandler beginDecompressedPdfStreamProcessing()
Begins handling of current pdf stream decompression.- Returns:
- this
MemoryLimitsAwareHandlerinstance.
-
endDecompressedPdfStreamProcessing
MemoryLimitsAwareHandler endDecompressedPdfStreamProcessing()
Ends handling of current pdf stream decompression. If memory limits have not been faced, throws an exception.- Returns:
- this
MemoryLimitsAwareHandlerinstance. - See Also:
MemoryLimitsAwareException
-
getAllMemoryUsedForDecompression
long getAllMemoryUsedForDecompression()
-
calculateDefaultParameter
private static long calculateDefaultParameter(long documentSize, int scale, long min)
-
ensureCurrentStreamIsReset
private void ensureCurrentStreamIsReset()
-
-