Improve ImageUtilsAndroid and ImageUtils check() system
authorNiki Roo <niki@nikiroo.be>
Sat, 18 May 2019 11:40:35 +0000 (13:40 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 18 May 2019 11:40:35 +0000 (13:40 +0200)
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/Image.java
src/be/nikiroo/utils/android/ImageUtilsAndroid.java
src/be/nikiroo/utils/ui/ImageUtilsAwt.java

index e7ab4033a8e15933ee6b38673a57efb6f074feb6..e3837e17ed76c8b4bdb9274edf9524182856542a 100644 (file)
@@ -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;
        }
 
        /**
index 3e65364bcfee3544f3dd09d7c374a2ee3884c2fc..4518577d640b684bee939381e02b9aa745b2d3a2 100644 (file)
@@ -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;
+                                       }
+                               }
                        }
                }
        }
index 4b3c1ea631d94689039b225925aff20e4ed57cc0..d55e602cb89d247e6056caec1f49c9b484de7ed4 100644 (file)
@@ -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;
        }
 }
index 367e757a81ede843a9273f68cafbe6f816374297..981ec247f87b0579d852a143632074bddf9b036c 100644 (file)
@@ -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;
        }