The following code converts a SMIL to JPG and saves it
locally – that part of the code works as designed. The problem is
that the File delete() and rename() methods in the finally block work only
intermittently. When they don’t work, Windows also complains that
some process has the file resource. Is it possible the Batik library is not
freeing the file handle properly? Running tomcat-5.0.27 on Win2003Server,
not sure what Batik version (probably not the latest). Mike Weber [EMAIL PROTECTED] public static void
convertSVGtoJPG(String in_svgContent, String JPGfname) throws Exception { FileOutputStream
ostream = null; String
tmpJPGfname = tmpJPGfname = JPGfname + ".N";
try { JPEGTranscoder
jpegt = new JPEGTranscoder(); if(
jpegt == null ) {
throw new Exception("Can't perform SVG>JPG conversion, JPEGTranscoder()
object reference is null"); }
jpegt.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0)); RenderingHints
hints = new RenderingHints(null); hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); //
Create the transcoder input TranscoderInput
input = new TranscoderInput(new StringReader(in_svgContent)); if(
input == null ) {
throw new Exception("Can't perform SVG>JPG conversion,
TranscoderInput() object reference is null"); } //
Temp file used to store output. Do not overwrite existing file with //
transcoded output until process is completed File
fTmp = new File(tmpJPGfname); //
Create the transcoder output
ostream = new FileOutputStream(tmpJPGfname); TranscoderOutput
output = new TranscoderOutput(ostream); if(
ostream == null ) {
throw new Exception("Can't perform SVG>JPG conversion,
FileOutputStream() object is null"); } if(
output == null ) {
throw new Exception("Can't perform SVG>JPG conversion,
TranscoderOutput() object is null"); } //
Save the image jpegt.transcode(input,
output);
// Flush and close the stream try {
ostream.flush();
ostream.close();
} catch(
Exception e ) {
throw new Exception("SVG>JPG conversion ok but problem flushing/closing
output stream."+e.getMessage); } }
catch(Exception e) {
throw new Exception("convertSVGtoJPG() unsuccessful."+e.getMessage()); }
finally {
try
{
// delete source
if( !new File(JPGfname).delete() )
throw new Exception("SVG>JPG conversion performed but problem deleting
source file.");
// rename temp to source File
fTmp = new File(tmpJPGfname);
if( !fTmp.renameTo(new File(JPGfname)) )
throw new Exception("SVG>JPG conversion performed but problem renaming
temp to source file.");
}
catch( Exception e )
{
throw new Exception("SVG>JPG conversion performed but problem deleting
and/or renaming the files."+e.getMessage());
} } } |