From 4c660023394920402e4b89b2e7e4eea3884308d9 Mon Sep 17 00:00:00 2001 From: Paulo Veiga Date: Sun, 16 Sep 2018 18:06:47 -0700 Subject: [PATCH] Hack for workaround batik OOM --- .../wisemapping/exporter/ExporterFactory.java | 23 +++++++++++++++---- .../wisemapping/exporter/XSLTExporter.java | 4 +--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java b/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java index 47fc820b..98859fe7 100644 --- a/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java +++ b/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java @@ -19,6 +19,7 @@ package com.wisemapping.exporter; import com.wisemapping.importer.VersionNumber; +import org.apache.batik.ext.awt.image.rendered.TileCache; import org.apache.batik.parser.AWTTransformProducer; import org.apache.batik.parser.ParseException; import org.apache.batik.parser.TransformListParser; @@ -53,6 +54,11 @@ import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; public class ExporterFactory { + + static { + // Try to prevent OOM. + TileCache.setSize(0); + } private static final String GROUP_NODE_NAME = "g"; private static final String IMAGE_NODE_NAME = "image"; private static final int MANGING = 50; @@ -70,6 +76,7 @@ public class ExporterFactory { public void export(@NotNull ExportProperties properties, @Nullable String xml, @NotNull OutputStream output, @Nullable String mapSvg) throws ExportException, IOException, TranscoderException { final ExportFormat format = properties.getFormat(); + switch (format) { case PNG: { // Create a JPEG transcoder @@ -81,12 +88,14 @@ public class ExporterFactory { // Create the transcoder input. final String svgString = normalizeSvg(mapSvg); - final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray())); + final CharArrayReader reader = new CharArrayReader(svgString.toCharArray()); + final TranscoderInput input = new TranscoderInput(reader); TranscoderOutput transcoderOutput = new TranscoderOutput(output); // Save the image. transcoder.transcode(input, transcoderOutput); + reader.close(); break; } case JPG: { @@ -101,7 +110,9 @@ public class ExporterFactory { // Create the transcoder input. final String svgString = normalizeSvg(mapSvg); - final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray())); + CharArrayReader reader = new CharArrayReader(svgString.toCharArray()); + final TranscoderInput input = new TranscoderInput(reader); + reader.close(); TranscoderOutput trascoderOutput = new TranscoderOutput(output); @@ -115,9 +126,10 @@ public class ExporterFactory { // Create the transcoder input. final String svgString = normalizeSvg(mapSvg); - final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray())); + CharArrayReader reader = new CharArrayReader(svgString.toCharArray()); + final TranscoderInput input = new TranscoderInput(reader); TranscoderOutput trascoderOutput = new TranscoderOutput(output); - + reader.close(); // Save the image. transcoder.transcode(input, trascoderOutput); break; @@ -156,6 +168,9 @@ public class ExporterFactory { default: throw new UnsupportedOperationException("Export method not supported."); } + + output.flush(); + output.close(); } private String normalizeSvg(@NotNull String svgXml) throws ExportException { diff --git a/wise-webapp/src/main/java/com/wisemapping/exporter/XSLTExporter.java b/wise-webapp/src/main/java/com/wisemapping/exporter/XSLTExporter.java index e3388104..9180e9e8 100644 --- a/wise-webapp/src/main/java/com/wisemapping/exporter/XSLTExporter.java +++ b/wise-webapp/src/main/java/com/wisemapping/exporter/XSLTExporter.java @@ -41,9 +41,7 @@ public class XSLTExporter implements Exporter { final CharArrayReader reader = new CharArrayReader(mmos.toString("iso-8859-1").toCharArray()); final Source mmSource = new StreamSource(reader); transformer.transform(mmSource, new StreamResult(outputStream)); - } catch (TransformerException e) { - throw new ExportException(e); - } catch (UnsupportedEncodingException e) { + } catch (TransformerException | UnsupportedEncodingException e) { throw new ExportException(e); }