Skip to content

Commit

Permalink
PDFBOX-5943: extend interface to support the removal of cached resources
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1923473 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed Jan 31, 2025
1 parent 2de20ea commit 8c2393b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ public class DefaultResourceCache implements ResourceCache
public PDFont getFont(COSObject indirect)
{
SoftReference<PDFont> font = fonts.get(indirect);
if (font != null)
{
return font.get();
}
return null;
return font != null ? font.get() : null;
}

@Override
Expand All @@ -75,15 +71,18 @@ public void put(COSObject indirect, PDFont font)
fonts.put(indirect, new SoftReference<>(font));
}

@Override
public PDFont removeFont(COSObject indirect)
{
SoftReference<PDFont> font = fonts.remove(indirect);
return font != null ? font.get() : null;
}

@Override
public PDColorSpace getColorSpace(COSObject indirect)
{
SoftReference<PDColorSpace> colorSpace = colorSpaces.get(indirect);
if (colorSpace != null)
{
return colorSpace.get();
}
return null;
return colorSpace != null ? colorSpace.get() : null;
}

@Override
Expand All @@ -92,15 +91,18 @@ public void put(COSObject indirect, PDColorSpace colorSpace)
colorSpaces.put(indirect, new SoftReference<>(colorSpace));
}

@Override
public PDColorSpace removeColorSpace(COSObject indirect)
{
SoftReference<PDColorSpace> colorSpace = colorSpaces.remove(indirect);
return colorSpace != null ? colorSpace.get() : null;
}

@Override
public PDExtendedGraphicsState getExtGState(COSObject indirect)
{
SoftReference<PDExtendedGraphicsState> extGState = extGStates.get(indirect);
if (extGState != null)
{
return extGState.get();
}
return null;
return extGState != null ? extGState.get() : null;
}

@Override
Expand All @@ -109,15 +111,18 @@ public void put(COSObject indirect, PDExtendedGraphicsState extGState)
extGStates.put(indirect, new SoftReference<>(extGState));
}

@Override
public PDExtendedGraphicsState removeExtState(COSObject indirect)
{
SoftReference<PDExtendedGraphicsState> extGState = extGStates.remove(indirect);
return extGState != null ? extGState.get() : null;
}

@Override
public PDShading getShading(COSObject indirect)
{
SoftReference<PDShading> shading = shadings.get(indirect);
if (shading != null)
{
return shading.get();
}
return null;
return shading != null ? shading.get() : null;
}

@Override
Expand All @@ -126,15 +131,18 @@ public void put(COSObject indirect, PDShading shading)
shadings.put(indirect, new SoftReference<>(shading));
}

@Override
public PDShading removeShading(COSObject indirect)
{
SoftReference<PDShading> shading = shadings.remove(indirect);
return shading != null ? shading.get() : null;
}

@Override
public PDAbstractPattern getPattern(COSObject indirect)
{
SoftReference<PDAbstractPattern> pattern = patterns.get(indirect);
if (pattern != null)
{
return pattern.get();
}
return null;
return pattern != null ? pattern.get() : null;
}

@Override
Expand All @@ -143,15 +151,18 @@ public void put(COSObject indirect, PDAbstractPattern pattern)
patterns.put(indirect, new SoftReference<>(pattern));
}

@Override
public PDAbstractPattern removePattern(COSObject indirect)
{
SoftReference<PDAbstractPattern> pattern = patterns.remove(indirect);
return pattern != null ? pattern.get() : null;
}

@Override
public PDPropertyList getProperties(COSObject indirect)
{
SoftReference<PDPropertyList> propertyList = properties.get(indirect);
if (propertyList != null)
{
return propertyList.get();
}
return null;
return propertyList != null ? propertyList.get() : null;
}

@Override
Expand All @@ -160,20 +171,30 @@ public void put(COSObject indirect, PDPropertyList propertyList)
properties.put(indirect, new SoftReference<>(propertyList));
}

@Override
public PDPropertyList removeProperties(COSObject indirect)
{
SoftReference<PDPropertyList> propertyList = properties.remove(indirect);
return propertyList != null ? propertyList.get() : null;
}

@Override
public PDXObject getXObject(COSObject indirect)
{
SoftReference<PDXObject> xobject = xobjects.get(indirect);
if (xobject != null)
{
return xobject.get();
}
return null;
return xobject != null ? xobject.get() : null;
}

@Override
public void put(COSObject indirect, PDXObject xobject)
{
xobjects.put(indirect, new SoftReference<>(xobject));
}

@Override
public PDXObject removeXObject(COSObject indirect)
{
SoftReference<PDXObject> xobject = xobjects.remove(indirect);
return xobject != null ? xobject.get() : null;
}
}
64 changes: 64 additions & 0 deletions pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ResourceCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

0 comments on commit 8c2393b

Please sign in to comment.