From a2c1d5fed2dca110c7cd5968b78baa3e1a6a16e9 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 18 May 2019 13:40:35 +0200 Subject: [PATCH] Improve ImageUtilsAndroid and ImageUtils check() system --- src/be/nikiroo/utils/IOUtils.java | 14 ++++++-- src/be/nikiroo/utils/Image.java | 36 ++++++++++++++----- .../utils/android/ImageUtilsAndroid.java | 28 ++++++++------- src/be/nikiroo/utils/ui/ImageUtilsAwt.java | 5 ++- 4 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index e7ab4033..e3837e17 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -29,13 +29,15 @@ public class IOUtils { * @param target * the target {@link File} * + * @return the number of bytes written + * * @throws IOException * in case of I/O error */ - public static void write(InputStream in, File target) throws IOException { + public static long write(InputStream in, File target) throws IOException { OutputStream out = new FileOutputStream(target); try { - write(in, out); + return write(in, out); } finally { out.close(); } @@ -49,17 +51,23 @@ public class IOUtils { * @param out * the target {@link OutputStream} * + * @return the number of bytes written + * * @throws IOException * in case of I/O error */ - public static void write(InputStream in, OutputStream out) + public static long write(InputStream in, OutputStream out) throws IOException { + long written = 0; byte buffer[] = new byte[4096]; int len = in.read(buffer); while (len > -1) { out.write(buffer, 0, len); + written += len; len = in.read(buffer); } + + return written; } /** diff --git a/src/be/nikiroo/utils/Image.java b/src/be/nikiroo/utils/Image.java index 3e65364b..4518577d 100644 --- a/src/be/nikiroo/utils/Image.java +++ b/src/be/nikiroo/utils/Image.java @@ -26,7 +26,9 @@ public class Image implements Closeable, Serializable { static private long count = 0; static private Object lock = new Object(); + private Object instanceLock = new Object(); private File data; + private long size; /** * Do not use -- for serialisation purposes only. @@ -45,7 +47,7 @@ public class Image implements Closeable, Serializable { ByteArrayInputStream in = new ByteArrayInputStream(data); try { this.data = getTemporaryFile(); - IOUtils.write(in, this.data); + size = IOUtils.write(in, this.data); } catch (IOException e) { throw new RuntimeException(e); } finally { @@ -86,7 +88,16 @@ public class Image implements Closeable, Serializable { */ public Image(InputStream in) throws IOException { data = getTemporaryFile(); - IOUtils.write(in, data); + size = IOUtils.write(in, data); + } + + /** + * The size of the enclosed image in bytes. + * + * @return the size + */ + public long getSize() { + return size; } /** @@ -163,13 +174,20 @@ public class Image implements Closeable, Serializable { */ @Override public void close() throws IOException { - data.delete(); - synchronized (lock) { - count--; - if (count <= 0) { - count = 0; - tmpRepository.close(); - tmpRepository = null; + synchronized (instanceLock) { + if (size >= 0) { + size = -1; + data.delete(); + data = null; + + synchronized (lock) { + count--; + if (count <= 0) { + count = 0; + tmpRepository.close(); + tmpRepository = null; + } + } } } } diff --git a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java index 4b3c1ea6..d55e602c 100644 --- a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java +++ b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java @@ -1,11 +1,14 @@ package be.nikiroo.utils.android; import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.util.stream.Stream; import javax.imageio.ImageIO; @@ -23,11 +26,10 @@ public class ImageUtilsAndroid extends ImageUtils { @Override protected boolean check() { // If we can get the class, it means we have access to it - @SuppressWarnings("unused") - Object test = Bitmap.class; + Config.ALPHA_8; return true; } - + @Override public void saveAsImage(Image img, File target, String format) throws IOException { @@ -72,16 +74,18 @@ public class ImageUtilsAndroid extends ImageUtils { * in case of IO error */ static public Bitmap fromImage(Image img) throws IOException { - byte[] array = img.getData(); - int size = array.length; - // TODO: check if we can use a stream, too - Bitmap image = BitmapFactory.decodeByteArray(array, 0, size); - if (image == null) { - String ssize = StringUtils.formatNumber(size); - throw new IOException( - "Failed to convert input to image, size was: " + ssize); + InputStream stream = img.newInputStream(); + try { + Bitmap image = BitmapFactory.decodeStream(stream); + if (image == null) { + String ssize = StringUtils.formatNumber(img.getSize()); + throw new IOException( + "Failed to convert input to image, size was: " + ssize); + } + } finally { + stream.close(); } - + return image; } } diff --git a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java index 367e757a..981ec247 100644 --- a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java +++ b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java @@ -20,9 +20,8 @@ import be.nikiroo.utils.ImageUtils; public class ImageUtilsAwt extends ImageUtils { @Override protected boolean check() { - // If we can get the class, it means we have access to it - @SuppressWarnings("unused") - Object test = ImageIO.class; + // Will not work if ImageIO is not available + ImageIO.getCacheDirectory(); return true; } -- 2.27.0