From a583b272c89b2c65aff7088c78f548eb45cb09b2 Mon Sep 17 00:00:00 2001 From: Asaf Zafran Date: Thu, 21 Aug 2014 10:18:56 +0300 Subject: [PATCH 1/2] remove sync from configuration --- .../main/java/org/xhtmlrenderer/util/Configuration.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/Configuration.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/Configuration.java index bd3c3c06b..c5f172d78 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/Configuration.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/Configuration.java @@ -85,7 +85,7 @@ public class Configuration { /** * The Singleton instance of the class. */ - private static Configuration sInstance; + private static Configuration sInstance = new Configuration(); /** * List of LogRecords for messages from Configuration startup; used to hold these @@ -762,10 +762,7 @@ public static boolean isFalse(String key, boolean defaultVal) { /** * @return The singleton instance of the class. */ - private static synchronized Configuration instance() { - if (Configuration.sInstance == null) { - Configuration.sInstance = new Configuration(); - } + private static Configuration instance() { return Configuration.sInstance; }// end main() From d6f9752a3b24c94c7786aa0f38e7c082229b9975 Mon Sep 17 00:00:00 2001 From: Asaf Zafran Date: Thu, 21 Aug 2014 10:29:28 +0300 Subject: [PATCH 2/2] resolve uri only when image not in cache --- .../java/org/xhtmlrenderer/swing/NaiveUserAgent.java | 9 +++++---- .../main/java/org/xhtmlrenderer/pdf/ITextUserAgent.java | 9 ++++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java index 14536b692..8fb6ca1af 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java @@ -234,11 +234,12 @@ public ImageResource getImageResource(String uri) { BufferedImage image = ImageUtil.loadEmbeddedBase64Image(uri); ir = createImageResource(null, image); } else { - uri = resolveURI(uri); - ir = (ImageResource) _imageCache.get(uri); + String unresolvedUri = uri; + ir = (ImageResource) _imageCache.get(unresolvedUri); //TODO: check that cached image is still valid if (ir == null) { - InputStream is = resolveAndOpenStream(uri); + uri = resolveURI(uri); + InputStream is = resolveAndOpenStream(uri); if (is != null) { try { BufferedImage img = ImageIO.read(is); @@ -246,7 +247,7 @@ public ImageResource getImageResource(String uri) { throw new IOException("ImageIO.read() returned null"); } ir = createImageResource(uri, img); - _imageCache.put(uri, ir); + _imageCache.put(unresolvedUri, ir); } catch (FileNotFoundException e) { XRLog.exception("Can't read image file; image at URI '" + uri + "' not found"); } catch (IOException e) { diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextUserAgent.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextUserAgent.java index 8da54c5fd..5ace0038d 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextUserAgent.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextUserAgent.java @@ -38,6 +38,8 @@ import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.PdfReader; +import org.xhtmlrenderer.util.ImageUtil; + public class ITextUserAgent extends NaiveUserAgent { private static final int IMAGE_CACHE_CAPACITY = 32; @@ -62,16 +64,17 @@ private byte[] readStream(InputStream is) throws IOException { } public ImageResource getImageResource(String uriStr) { + String unresolvedUri = uriStr; ImageResource resource; if (!ImageUtil.isEmbeddedBase64Image(uriStr)) { uriStr = resolveURI(uriStr); } - resource = (ImageResource) _imageCache.get(uriStr); + resource = (ImageResource) _imageCache.get(unresolvedUri); if (resource == null) { if (ImageUtil.isEmbeddedBase64Image(uriStr)) { resource = loadEmbeddedBase64ImageResource(uriStr); - _imageCache.put(uriStr, resource); + _imageCache.put(unresolvedUri, resource); } else { InputStream is = resolveAndOpenStream(uriStr); if (is != null) { @@ -91,7 +94,7 @@ public ImageResource getImageResource(String uriStr) { scaleToOutputResolution(image); resource = new ImageResource(uriStr, new ITextFSImage(image)); } - _imageCache.put(uriStr, resource); + _imageCache.put(unresolvedUri, resource); } catch (Exception e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uriStr + "'", e); } finally {