You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Amin Ahmad <am...@hotmail.com> on 2005/12/04 15:35:50 UTC

PNG renderer not closing output streams properly.

Hi Team FOP,

I was playing around with the PNG Renderer, and I think I noticed a problem 
-- when it produces multiple files (one per page) it does not appear to be 
explicity closing the files it creates. Java GC takes care of closing the 
files automatically, but, running Java 5, the final file is left open until 
the JVM terminates.

In the code below from PNGRenderer, on the last line of the for loop, the 
output stream is flushed but never closed. At the top of the loop, os is 
reassigned to a new output stream for the next page.

thanks,
Amin


    public void stopRenderer() throws IOException {

        super.stopRenderer();

        for (int i = 0; i < pageViewportList.size(); i++) {

            OutputStream os = getCurrentOutputStream(i);
            if (os == null) {
                log.warn("No filename information available."
                        + " Stopping early after the first page.");
                break;
            }
            // Do the rendering: get the image for this page
            RenderedImage image = (RenderedImage) 
getPageImage((PageViewport) pageViewportList
                    .get(i));

            // Encode this image
            log.debug("Encoding page " + (i + 1));
            renderParams = PNGEncodeParam.getDefaultEncodeParam(image);

            // Set resolution
            float pixSzMM = userAgent.getPixelUnitToMillimeter();
            // num Pixs in 1 Meter
            int numPix = (int)((1000 / pixSzMM) + 0.5);
            renderParams.setPhysicalDimension(numPix, numPix, 1); // 1 means 
'pix/meter'

            // Encode PNG image
            PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams);
            encoder.encode(image);
            os.flush();
        }
    }



Re: PNG renderer not closing output streams properly.

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
Thanks for spotting that one. I've just fixed it in Subversion:
http://svn.apache.org/viewcvs?rev=354537&view=rev

On 04.12.2005 15:35:50 Amin Ahmad wrote:
> Hi Team FOP,
> 
> I was playing around with the PNG Renderer, and I think I noticed a problem 
> -- when it produces multiple files (one per page) it does not appear to be 
> explicity closing the files it creates. Java GC takes care of closing the 
> files automatically, but, running Java 5, the final file is left open until 
> the JVM terminates.
> 
> In the code below from PNGRenderer, on the last line of the for loop, the 
> output stream is flushed but never closed. At the top of the loop, os is 
> reassigned to a new output stream for the next page.
> 
> thanks,
> Amin
> 
> 
>     public void stopRenderer() throws IOException {
> 
>         super.stopRenderer();
> 
>         for (int i = 0; i < pageViewportList.size(); i++) {
> 
>             OutputStream os = getCurrentOutputStream(i);
>             if (os == null) {
>                 log.warn("No filename information available."
>                         + " Stopping early after the first page.");
>                 break;
>             }
>             // Do the rendering: get the image for this page
>             RenderedImage image = (RenderedImage) 
> getPageImage((PageViewport) pageViewportList
>                     .get(i));
> 
>             // Encode this image
>             log.debug("Encoding page " + (i + 1));
>             renderParams = PNGEncodeParam.getDefaultEncodeParam(image);
> 
>             // Set resolution
>             float pixSzMM = userAgent.getPixelUnitToMillimeter();
>             // num Pixs in 1 Meter
>             int numPix = (int)((1000 / pixSzMM) + 0.5);
>             renderParams.setPhysicalDimension(numPix, numPix, 1); // 1 means 
> 'pix/meter'
> 
>             // Encode PNG image
>             PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams);
>             encoder.encode(image);
>             os.flush();
>         }
>     }



Jeremias Maerki