Slick Forums

Discuss the Slick 2D Library
It is currently Mon May 20, 2013 3:01 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Jul 14, 2012 2:46 am 
Offline

Joined: Sat Jul 14, 2012 2:35 am
Posts: 7
hey guys...

those methods in the class ImageOut create a FileOutputStream, but does not close it...

Code:
   public static void write(Image image, String format, String dest, boolean writeAlpha) throws SlickException {
      try {
         write(image, format, new FileOutputStream(dest), writeAlpha);
      } catch (IOException e) {
         throw new SlickException("Unable to write to the destination: "+dest, e);
      }
   }

   public static void write(Image image, String dest, boolean writeAlpha) throws SlickException {
      try {
         int ext = dest.lastIndexOf('.');
         if (ext < 0) {
            throw new SlickException("Unable to determine format from: "+dest);
         }
         
         String format = dest.substring(ext+1);
         write(image, format, new FileOutputStream(dest), writeAlpha);
      } catch (IOException e) {
         throw new SlickException("Unable to write to the destination: "+dest, e);
      }
   }


the ImageIOWriter implementation uses the javax.imageio.ImageIO.write() method to store the image. copy from javadoc:
Quote:
* <p> This method <em>does not</em> close the provided
* <code>OutputStream</code> after the write operation has completed;
* it is the responsibility of the caller to close the stream, if desired.


thus, the created image cannot be opened until the program is closed due to this bug...

And I would suggest to use a Thread for the IO operation. its not possible to use a thread outside the library because until the image is rendered, the GLRenderer is used in the same thread, but the IO operation is handled inside this method...

greetz ben


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 4:19 pm 
Offline

Joined: Sat Jul 14, 2012 2:35 am
Posts: 7
the code can easily be fixed with:

Code:
    public static void write(final Image image, final String format, final String dest, final boolean writeAlpha)
            throws SlickException {
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(dest));
            write(image, format, out, writeAlpha);
        } catch (final IOException e) {
            throw new SlickException("Unable to write to the destination: " + dest, e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (final IOException e) {
                    // noop
                }
            }
        }
    }


    public static void write(final Image image, final String dest, final boolean writeAlpha) throws SlickException {
        BufferedOutputStream out = null;
        try {
            final int ext = dest.lastIndexOf('.');
            if (ext < 0) throw new SlickException("Unable to determine format from: " + dest);

            final String format = dest.substring(ext + 1);
            out = new BufferedOutputStream(new FileOutputStream(dest));
            write(image, format, out, writeAlpha);
        } catch (final IOException e) {
            throw new SlickException("Unable to write to the destination: " + dest, e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (final IOException e) {
                    // noop
                }
            }
        }
    }


with using java 1.7 the code would like much easier :)

greetz Ben


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group