From 4b7d32e77cf31ca44c5f93f08d2729357c86b93f Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 29 Nov 2017 21:58:09 +0100 Subject: [PATCH] Version 3.1.3: fix ImageUtils.fromRestableStream --- VERSION | 2 +- changelog.md | 4 + src/be/nikiroo/utils/ImageUtils.java | 89 +++++++++++-------- .../utils/serial/server/ServerObject.java | 3 - 4 files changed, 59 insertions(+), 39 deletions(-) diff --git a/VERSION b/VERSION index ef538c2..ff365e0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.2 +3.1.3 diff --git a/changelog.md b/changelog.md index f9aeeaf..810d0e1 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/be/nikiroo/utils/ImageUtils.java b/src/be/nikiroo/utils/ImageUtils.java index 9f1e06f..496d2ea 100644 --- a/src/be/nikiroo/utils/ImageUtils.java +++ b/src/be/nikiroo/utils/ImageUtils.java @@ -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. * diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index 1559ae4..fdbd2cd 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -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"); } } }; -- 2.27.0