diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/DefaultResourceCache.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/DefaultResourceCache.java index 8bcb35aa601..9d93abd6047 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/DefaultResourceCache.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/DefaultResourceCache.java @@ -62,11 +62,7 @@ public class DefaultResourceCache implements ResourceCache public PDFont getFont(COSObject indirect) { SoftReference font = fonts.get(indirect); - if (font != null) - { - return font.get(); - } - return null; + return font != null ? font.get() : null; } @Override @@ -75,15 +71,18 @@ public void put(COSObject indirect, PDFont font) fonts.put(indirect, new SoftReference<>(font)); } + @Override + public PDFont removeFont(COSObject indirect) + { + SoftReference font = fonts.remove(indirect); + return font != null ? font.get() : null; + } + @Override public PDColorSpace getColorSpace(COSObject indirect) { SoftReference colorSpace = colorSpaces.get(indirect); - if (colorSpace != null) - { - return colorSpace.get(); - } - return null; + return colorSpace != null ? colorSpace.get() : null; } @Override @@ -92,15 +91,18 @@ public void put(COSObject indirect, PDColorSpace colorSpace) colorSpaces.put(indirect, new SoftReference<>(colorSpace)); } + @Override + public PDColorSpace removeColorSpace(COSObject indirect) + { + SoftReference colorSpace = colorSpaces.remove(indirect); + return colorSpace != null ? colorSpace.get() : null; + } + @Override public PDExtendedGraphicsState getExtGState(COSObject indirect) { SoftReference extGState = extGStates.get(indirect); - if (extGState != null) - { - return extGState.get(); - } - return null; + return extGState != null ? extGState.get() : null; } @Override @@ -109,15 +111,18 @@ public void put(COSObject indirect, PDExtendedGraphicsState extGState) extGStates.put(indirect, new SoftReference<>(extGState)); } + @Override + public PDExtendedGraphicsState removeExtState(COSObject indirect) + { + SoftReference extGState = extGStates.remove(indirect); + return extGState != null ? extGState.get() : null; + } + @Override public PDShading getShading(COSObject indirect) { SoftReference shading = shadings.get(indirect); - if (shading != null) - { - return shading.get(); - } - return null; + return shading != null ? shading.get() : null; } @Override @@ -126,15 +131,18 @@ public void put(COSObject indirect, PDShading shading) shadings.put(indirect, new SoftReference<>(shading)); } + @Override + public PDShading removeShading(COSObject indirect) + { + SoftReference shading = shadings.remove(indirect); + return shading != null ? shading.get() : null; + } + @Override public PDAbstractPattern getPattern(COSObject indirect) { SoftReference pattern = patterns.get(indirect); - if (pattern != null) - { - return pattern.get(); - } - return null; + return pattern != null ? pattern.get() : null; } @Override @@ -143,15 +151,18 @@ public void put(COSObject indirect, PDAbstractPattern pattern) patterns.put(indirect, new SoftReference<>(pattern)); } + @Override + public PDAbstractPattern removePattern(COSObject indirect) + { + SoftReference pattern = patterns.remove(indirect); + return pattern != null ? pattern.get() : null; + } + @Override public PDPropertyList getProperties(COSObject indirect) { SoftReference propertyList = properties.get(indirect); - if (propertyList != null) - { - return propertyList.get(); - } - return null; + return propertyList != null ? propertyList.get() : null; } @Override @@ -160,15 +171,18 @@ public void put(COSObject indirect, PDPropertyList propertyList) properties.put(indirect, new SoftReference<>(propertyList)); } + @Override + public PDPropertyList removeProperties(COSObject indirect) + { + SoftReference propertyList = properties.remove(indirect); + return propertyList != null ? propertyList.get() : null; + } + @Override public PDXObject getXObject(COSObject indirect) { SoftReference xobject = xobjects.get(indirect); - if (xobject != null) - { - return xobject.get(); - } - return null; + return xobject != null ? xobject.get() : null; } @Override @@ -176,4 +190,11 @@ public void put(COSObject indirect, PDXObject xobject) { xobjects.put(indirect, new SoftReference<>(xobject)); } + + @Override + public PDXObject removeXObject(COSObject indirect) + { + SoftReference xobject = xobjects.remove(indirect); + return xobject != null ? xobject.get() : null; + } } diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ResourceCache.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ResourceCache.java index 9d25bf24e49..eb787250dcd 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ResourceCache.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ResourceCache.java @@ -144,4 +144,68 @@ public interface ResourceCache * @param xobject the XObject to be cached */ void put(COSObject indirect, PDXObject xobject); + + /** + * Removes the given indirect color space resource from the cache. + * + * @param indirect the indirect reference of the color space to be removed + * + * @return the removed resource if present + */ + PDColorSpace removeColorSpace(COSObject indirect); + + /** + * Removes the given indirect extended graphics state resource from the cache. + * + * @param indirect the indirect reference of the extended graphics state to be removed + * + * @return the removed resource if present + */ + PDExtendedGraphicsState removeExtState(COSObject indirect); + + /** + * Removes the given indirect font resource from the cache. + * + * @param indirect the indirect reference of the font to be removed + * + * @return the removed resource if present + */ + PDFont removeFont(COSObject indirect); + + /** + * Removes the given indirect shading resource from the cache. + * + * @param indirect the indirect reference of the shading to be removed + * + * @return the removed resource if present + */ + PDShading removeShading(COSObject indirect); + + /** + * Removes the given indirect pattern resource from the cache. + * + * @param indirect the indirect reference of the pattern to be removed + * + * @return the removed resource if present + */ + PDAbstractPattern removePattern(COSObject indirect); + + /** + * Removes the given indirect property list resource from the cache. + * + * @param indirect the indirect reference of the property list to be removed + * + * @return the removed resource if present + */ + PDPropertyList removeProperties(COSObject indirect); + + /** + * Removes the given indirect XObject resource from the cache. + * + * @param indirect the indirect reference of the XObject to be removed + * + * @return the removed resource if present + */ + PDXObject removeXObject(COSObject indirect); + }