14

I try to convert an svg into PNG. the svg document is coming from a server as an Inputstream.

First, I convert the svg stream into byte array with:

 byte[] streamBytes = IOUtils.toByteArray(svgStream);

Then I convert the bytes into OutputStream(PNG) with the following 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;
    }

But i get null pointer exception by "t.transcode(input, output);"

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)

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.

TranscoderInput input = new TranscoderInput(new File("c:/a.svg").toURI().toString());
Kayser
  • 6,116
  • 17
  • 49
  • 86
  • Have you validated that `streamBytes` is correct? Is that the *exact* same byte array that you've tried saving to disk? – Jon Skeet Nov 17 '11 at 13:40
  • @jon-skeet As I said, if i save on the disk as svg. it works perfect. – Kayser Nov 17 '11 at 13:41
  • I created an svg with. Then it was succesfull with convertion.
    try { File file = new File("c:/a.svg"); if (!file.exists()) { file.createNewFile(); } OutputStream out = new FileOutputStream(file); int read = 0; byte[] bytes1 = new byte[1024]; while ((read = svgStream.read(bytes1)) != -1) { out.write(bytes1, 0, read); } out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
    – Kayser Nov 17 '11 at 13:43
  • What is `svgStream` here? My point is that if you've created the file with different code to `streamBytes`, there could be an error in (say) `IOUtils.toByteArray`. Where is the implementation of `IOUtils`? (There are various different open source ones, or it could be your own one.) – Jon Skeet Nov 17 '11 at 13:51
  • @jon-skeet It is from Apache `org.apache.commons.io.IOUtils;` – Kayser Nov 17 '11 at 13:52
  • Right. I'd expect that to be okay then... – Jon Skeet Nov 17 '11 at 13:54

1 Answers1

4

I found the problem.

I checked each time if the svgstream is ok or not. To see if it is ok, I created each time an SVG file with the code in my comment. Logically it consumed the stream. There was no real stream at the end. It caused the Exception. Thanks to all..

Kayser
  • 6,116
  • 17
  • 49
  • 86