You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by Michael Weber <mi...@infospace.com> on 2004/09/15 01:26:01 UTC

JPEGTranscoder not releasing file handles - i suspect.

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

mweber@infospace.com

 

      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());

                }

          }

      }


Re: JPEGTranscoder not releasing file handles - i suspect.

Posted by Thomas DeWeese <Th...@Kodak.com>.
Michael Weber wrote:

> 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?  

    I don't really think this is possible.  We never see the source
file you delete, and you close the FileOutputStream, so even if we
kept a reference to it this would kill it.  There is something
tickling in the back of my head about this (in Java in general) but I
can't recall it.




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org