1

I have been writing a GUI wrapper (in Java) for a command line program that generates SVG images under one filename.

Unfortunately, the underlying program fails to close the svg tag when generating a new image, so I am using a FileWriter like so to modify the file:

FileWriter fw = new FileWriter(("ImageCache/"+file), true); //true: allows appending to file        
BufferedWriter bw = new BufferedWriter(fw);
bw.write("\n</svg>");   
bw.close();
fw.close();

Batik transcodes the first image perfectly, however the second image fails due to the missing svg tag.

I have manually checked to verify that the FileWriter is updating the file both times, which it is.

Below is my transcoder code (with the inputs file and newFileName):

org.apache.batik.ext.awt.image.spi.ImageTagRegistry.getRegistry().flushCache();
PNGTranscoder t = new PNGTranscoder();

String svgURI = new File(file).toURL().toString();
TranscoderInput i = new TranscoderInput(svgURI);
OutputStream outStream = new FileOutputStream(newFileName);
TranscoderOutput o = new TranscoderOutput(outStream);

t.transcode(i, o);
outStream.flush();
outStream.close();

Each time an updated SVG image is generated, a new transcoder object is instantiated and the above method is run.

The image is always generated and the closing tag added before the transcoder runs, so I don't know why the transcoder doesn't seem to read the updated file. I've also tried suspending the thread for a few seconds, without any improvement.

Any suggestions would be appreciated.

Oscar Barrett
  • 3,135
  • 31
  • 36

0 Answers0