Browse Source

FOP-3002: Optimise memory usage for data uri

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1886951 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-2_7
Simon Steiner 3 years ago
parent
commit
678d6c0e04
1 changed files with 37 additions and 6 deletions
  1. 37
    6
      fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java

+ 37
- 6
fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java View File

import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;


import org.apache.xmlgraphics.image.loader.util.SoftMapCache;

import org.apache.fop.pdf.StandardStructureAttributes.Table.Scope; import org.apache.fop.pdf.StandardStructureAttributes.Table.Scope;
import org.apache.fop.pdf.xref.CrossReferenceStream; import org.apache.fop.pdf.xref.CrossReferenceStream;
import org.apache.fop.pdf.xref.CrossReferenceTable; import org.apache.fop.pdf.xref.CrossReferenceTable;


/* TODO: Should be modified (works only for image subtype) */ /* TODO: Should be modified (works only for image subtype) */
private Map<String, PDFXObject> xObjectsMap = new HashMap<String, PDFXObject>(); private Map<String, PDFXObject> xObjectsMap = new HashMap<String, PDFXObject>();
private SoftMapCache xObjectsMapFast = new SoftMapCache(false);


private Map<String, PDFFont> fontMap = new HashMap<String, PDFFont>(); private Map<String, PDFFont> fontMap = new HashMap<String, PDFFont>();


*/ */
@Deprecated @Deprecated
public PDFImageXObject getImage(String key) { public PDFImageXObject getImage(String key) {
return (PDFImageXObject)this.xObjectsMap.get(key);
return (PDFImageXObject)getXObject(key);
} }


/** /**
* @return the PDFXObject for the key if found * @return the PDFXObject for the key if found
*/ */
public PDFXObject getXObject(String key) { public PDFXObject getXObject(String key) {
return this.xObjectsMap.get(key);
Object xObj = xObjectsMapFast.get(key);
if (xObj != null) {
return (PDFXObject) xObj;
}
return xObjectsMap.get(toHashCode(key));
}

private void putXObject(String key, PDFXObject pdfxObject) {
xObjectsMapFast.clear();
xObjectsMapFast.put(key, pdfxObject);
xObjectsMap.put(toHashCode(key), pdfxObject);
}

private String toHashCode(String key) {
if (key.length() < 1024) {
return key;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(key.getBytes("UTF-8"));
StringBuilder hex = new StringBuilder();
for (byte b : thedigest) {
hex.append(String.format("%02x", b));
}
return hex.toString();
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} }


/** /**
public PDFImageXObject addImage(PDFResourceContext res, PDFImage img) { public PDFImageXObject addImage(PDFResourceContext res, PDFImage img) {
// check if already created // check if already created
String key = img.getKey(); String key = img.getKey();
PDFImageXObject xObject = (PDFImageXObject)this.xObjectsMap.get(key);
PDFImageXObject xObject = (PDFImageXObject)getXObject(key);
if (xObject != null) { if (xObject != null) {
if (res != null) { if (res != null) {
res.addXObject(xObject); res.addXObject(xObject);
if (res != null) { if (res != null) {
res.addXObject(xObject); res.addXObject(xObject);
} }
this.xObjectsMap.put(key, xObject);
putXObject(key, xObject);
return xObject; return xObject;
} }


String key) { String key) {


// check if already created // check if already created
PDFFormXObject xObject = (PDFFormXObject)xObjectsMap.get(key);
PDFFormXObject xObject = (PDFFormXObject)getXObject(key);
if (xObject != null) { if (xObject != null) {
if (res != null) { if (res != null) {
res.addXObject(xObject); res.addXObject(xObject);
if (res != null) { if (res != null) {
res.addXObject(xObject); res.addXObject(xObject);
} }
this.xObjectsMap.put(key, xObject);
putXObject(key, xObject);
return xObject; return xObject;
} }



Loading…
Cancel
Save