Image: make it Serializable
[nikiroo-utils.git] / src / be / nikiroo / utils / Image.java
index 58c0e1019204ea2e3a35647754164b57653e7684..2e9c9f83a39bfd4fcdeef8d351071d8284e9af61 100644 (file)
@@ -1,15 +1,29 @@
 package be.nikiroo.utils;
 
+import java.io.ByteArrayInputStream;
+import java.io.Closeable;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+
+import be.nikiroo.utils.streams.MarkableFileInputStream;
 
 /**
  * This class represents an image data.
  * 
  * @author niki
  */
-public class Image {
-       private byte[] data;
+public class Image implements Closeable, Serializable {
+       static private final long serialVersionUID = 1L;
+
+       static private File tempRoot;
+       static private TempFiles tmpRepository;
+       static private long count = 0;
+       static private Object lock = new Object();
+
+       private File data;
 
        /**
         * Do not use -- for serialisation purposes only.
@@ -25,54 +39,179 @@ public class Image {
         *            the data
         */
        public Image(byte[] data) {
-               this.data = data;
+               ByteArrayInputStream in = new ByteArrayInputStream(data);
+               try {
+                       this.data = getTemporaryFile();
+                       IOUtils.write(in, this.data);
+               } catch (IOException e) {
+                       throw new RuntimeException(e);
+               } finally {
+                       try {
+                               in.close();
+                       } catch (IOException e) {
+                               throw new RuntimeException(e);
+                       }
+               }
        }
 
        /**
-        * Create a new {@link Image} from its Base64 representation.
+        * Create a new {@link Image} from a stream.
         * 
-        * @param base64
-        *            the {@link Image} in Base64 format
+        * @param in
+        *            the stream
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       public Image(String base64) throws IOException {
-               this(Base64.decode(base64));
+       public Image(InputStream in) throws IOException {
+               data = getTemporaryFile();
+               IOUtils.write(in, data);
        }
 
        /**
-        * Create a new {@link Image} from a stream.
+        * Generate an {@link InputStream} that you can {@link InputStream#reset()}
+        * for this {@link Image}.
+        * <p>
+        * This {@link InputStream} will (always) be a new one, and <b>you</b> are
+        * responsible for it.
+        * <p>
+        * Note: take care that the {@link InputStream} <b>must not</b> live past
+        * the {@link Image} life time!
         * 
-        * @param in
-        *            the stream
+        * @return the stream
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       public Image(InputStream in) throws IOException {
-               this.data = IOUtils.toByteArray(in);
+       public InputStream newInputStream() throws IOException {
+               return new MarkableFileInputStream(data);
        }
 
        /**
-        * The actual image data.
+        * <b>Read</b> the actual image data, as a byte array.
         * <p>
-        * This is the actual data, not a copy, so any change made here will be
-        * reflected into the {@link Image} and vice-versa.
+        * Note: if possible, prefer the {@link Image#newInputStream()} method, as
+        * it can be more efficient.
         * 
         * @return the image data
         */
        public byte[] getData() {
-               return data;
+               try {
+                       InputStream in = newInputStream();
+                       try {
+                               return IOUtils.toByteArray(in);
+                       } finally {
+                               in.close();
+                       }
+               } catch (IOException e) {
+                       throw new RuntimeException(e);
+               }
        }
 
        /**
-        * Convert the given {@link Image} object into a Base64 representation of
-        * the same {@link Image} object.
+        * Closing the {@link Image} will delete the associated temporary file on
+        * disk.
+        * <p>
+        * Note that even if you don't, the program will still <b>try</b> to delete
+        * all the temporary files at JVM termination.
+        */
+       @Override
+       public void close() throws IOException {
+               data.delete();
+               synchronized (lock) {
+                       count--;
+                       if (count <= 0) {
+                               count = 0;
+                               tmpRepository.close();
+                               tmpRepository = null;
+                       }
+               }
+       }
+
+       @Override
+       protected void finalize() throws Throwable {
+               try {
+                       close();
+               } finally {
+                       super.finalize();
+               }
+       }
+
+       /**
+        * Return a newly created temporary file to work on.
+        * 
+        * @return the file
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       private File getTemporaryFile() throws IOException {
+               synchronized (lock) {
+                       if (tmpRepository == null) {
+                               tmpRepository = new TempFiles(tempRoot, "images");
+                               count = 0;
+                       }
+
+                       count++;
+
+                       return tmpRepository.createTempFile("image");
+               }
+       }
+
+       /**
+        * Write this {@link Image} for serialization purposes; that is, write the
+        * content of the backing temporary file.
+        * 
+        * @param out
+        *            the {@link OutputStream} to write to
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       private void writeObject(java.io.ObjectOutputStream out) throws IOException {
+               InputStream in = newInputStream();
+               try {
+                       IOUtils.write(in, out);
+               } finally {
+                       in.close();
+               }
+       }
+
+       /**
+        * Read an {@link Image} written by
+        * {@link Image#writeObject(java.io.ObjectOutputStream)}; that is, create a
+        * new temporary file with the saved content.
+        * 
+        * @param in
+        *            the {@link InputStream} to read from
+        * @throws IOException
+        *             in case of I/O error
+        * @throws ClassNotFoundException
+        *             will not be thrown by this method
+        */
+       @SuppressWarnings("unused")
+       private void readObject(java.io.ObjectInputStream in) throws IOException,
+                       ClassNotFoundException {
+               data = getTemporaryFile();
+               IOUtils.write(in, data);
+       }
+
+       /**
+        * Change the temporary root directory used by the program.
+        * <p>
+        * Caution: the directory will be <b>owned</b> by the system, all its files
+        * now belong to us (and will most probably be deleted).
+        * <p>
+        * Note: it may take some time until the new temporary root is used, we
+        * first need to make sure the previous one is not used anymore (i.e., we
+        * must reach a point where no unclosed {@link Image} remains in memory) to
+        * switch the temporary root.
         * 
-        * @return the Base64 representation
+        * @param root
+        *            the new temporary root, which will be <b>owned</b> by the
+        *            system
         */
-       public String toBase64() {
-               return new String(Base64.encodeBytes(getData()));
+       public static void setTemporaryFilesRoot(File root) {
+               tempRoot = root;
        }
 }