Version 3.1.3: fix ImageUtils.fromRestableStream nikiroo-utils-3.1.3
authorNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 20:58:09 +0000 (21:58 +0100)
committerNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 20:58:09 +0000 (21:58 +0100)
VERSION
changelog.md
src/be/nikiroo/utils/ImageUtils.java
src/be/nikiroo/utils/serial/server/ServerObject.java

diff --git a/VERSION b/VERSION
index ef538c2810938c03ced86f0380977b308a55b37b..ff365e06b9577c96dfeadddb8c4cccb09b85665c 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.1.2
+3.1.3
index f9aeeafa98c3286b289ca4f8907ef2399988d8f6..810d0e19463e310950dc4fe66af9a71be8d6048f 100644 (file)
@@ -1,5 +1,9 @@
 # nikiroo-utils
 
+## Version 3.1.3
+
+- Fix ImageUtils.fromStream with non-resetable streams
+
 ## Version 3.1.2
 
 - Fix Server regarding the client version passed to the handler
index 9f1e06f36806d57c3876dc38b6400d4d4b371412..496d2ead7155b8cd21f0989a17e9e98e43737faf 100644 (file)
@@ -120,13 +120,32 @@ public class ImageUtils {
                return fromStream(in);
        }
 
+       /**
+        * A shorthand method to create an {@link ImageText} and return its output.
+        * 
+        * @param image
+        *            the source {@link Image}
+        * @param size
+        *            the final text size to target
+        * @param mode
+        *            the mode of conversion
+        * @param invert
+        *            TRUE to invert colours rendering
+        * 
+        * @return the text image
+        */
+       static public String toAscii(Image image, Dimension size, Mode mode,
+                       boolean invert) {
+               return new ImageText(image, size, mode, invert).toString();
+       }
+
        /**
         * Convert the given {@link InputStream} (which should allow calls to
         * {@link InputStream#reset()} for better perfs) into an {@link Image}
         * object, respecting the EXIF transformations if any.
         * 
         * @param in
-        *            the 'resetable' {@link InputStream}
+        *            the {@link InputStream}
         * 
         * @return the {@link Image} object
         * 
@@ -137,21 +156,49 @@ public class ImageUtils {
                MarkableFileInputStream tmpIn = null;
                File tmp = null;
 
-               boolean repack = !in.markSupported();
-               if (!repack) {
+               boolean resetable = in.markSupported();
+               if (resetable) {
                        try {
                                in.reset();
                        } catch (IOException e) {
-                               repack = true;
+                               resetable = false;
                        }
                }
 
-               if (repack) {
-                       tmp = File.createTempFile(".tmp-image", ".tmp");
-                       tmp.deleteOnExit();
+               if (resetable) {
+                       return fromResetableStream(in);
+               }
+
+               tmp = File.createTempFile(".tmp-image", ".tmp");
+               try {
                        IOUtils.write(in, tmp);
                        tmpIn = new MarkableFileInputStream(new FileInputStream(tmp));
+                       return fromResetableStream(tmpIn);
+               } finally {
+                       try {
+                               if (tmpIn != null) {
+                                       tmpIn.close();
+                               }
+                       } finally {
+                               tmp.delete();
+                       }
                }
+       }
+
+       /**
+        * Convert the given resetable {@link InputStream} into an {@link Image}
+        * object, respecting the EXIF transformations if any.
+        * 
+        * @param in
+        *            the 'resetable' (this is mandatory) {@link InputStream}
+        * 
+        * @return the {@link Image} object
+        * 
+        * @throws IOException
+        *             in case of IO error
+        */
+       static private BufferedImage fromResetableStream(InputStream in)
+                       throws IOException {
 
                int orientation;
                try {
@@ -165,10 +212,6 @@ public class ImageUtils {
                BufferedImage image = ImageIO.read(in);
 
                if (image == null) {
-                       if (tmp != null) {
-                               tmp.delete();
-                               tmpIn.close();
-                       }
                        throw new IOException("Failed to convert input to image");
                }
 
@@ -230,33 +273,9 @@ public class ImageUtils {
                }
                //
 
-               if (tmp != null) {
-                       tmp.delete();
-                       tmpIn.close();
-               }
-
                return image;
        }
 
-       /**
-        * A shorthand method to create an {@link ImageText} and return its output.
-        * 
-        * @param image
-        *            the source {@link Image}
-        * @param size
-        *            the final text size to target
-        * @param mode
-        *            the mode of conversion
-        * @param invert
-        *            TRUE to invert colours rendering
-        * 
-        * @return the text image
-        */
-       static public String toAscii(Image image, Dimension size, Mode mode,
-                       boolean invert) {
-               return new ImageText(image, size, mode, invert).toString();
-       }
-
        /**
         * Return the EXIF transformation flag of this image if any.
         * 
index 1559ae455b27c9300424b349ddf188cc8a55ef15..fdbd2cd4d93fe2bcb2b14fa6e44d9814e27c6400 100644 (file)
@@ -68,9 +68,6 @@ abstract public class ServerObject extends Server {
                                        }
                                } catch (NullPointerException e) {
                                        // Client has no data any more, we quit
-                                       getTraceHandler()
-                                                       .trace(getName()
-                                                                       + ": client has data no more, stopping connection");
                                }
                        }
                };