title: How to convert SVG into PNG on-the-fly
body: <p>I try to convert an svg into PNG. the svg document is coming from a server as an <code>Inputstream</code>.<p>
  First, I convert the svg stream into byte array with:</p>
  
  <pre><code> byte[] streamBytes = IOUtils.toByteArray(svgStream);
  </code></pre>
  
  <p>Then I convert the bytes into <code>OutputStream</code>(PNG) with the following code.    </p>
  
  <pre><code>private ByteArrayOutputStream svgToPng(byte[] streamBytes)
                                              throws TranscoderException, IOException {
          PNGTranscoder t = new PNGTranscoder();
          TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(streamBytes));
          ByteArrayOutputStream ostream = new ByteArrayOutputStream();
          TranscoderOutput output = new TranscoderOutput(ostream);
  
          t.transcode(input, output);
  
          ostream.flush();
          // ostream.close();
          return ostream;
      }
  </code></pre>
  
  <p>But i get null pointer exception by  "<code>t.transcode(input, output);</code>"</p>
  
  <pre><code>org.apache.batik.transcoder.TranscoderException: null
  Enclosed Exception:
  Premature end of file.
  graphdata : null
      at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:136)
      at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
  </code></pre>
  
  <p>Note: If i save the svgstream on th disk and use the following transcoderinput with uri constructor, then it works. But in my case i don't want to save on the disk.</p>
  
  <pre><code>TranscoderInput input = new TranscoderInput(new File("c:/a.svg").toURI().toString());
  </code></pre>