1 package be
.nikiroo
.utils
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.Closeable
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
11 * This class represents an image data.
15 public class Image
implements Closeable
{
16 static private File tempRoot
;
17 static private TempFiles tmpRepository
;
18 static private long count
= 0;
19 static private Object lock
= new Object();
24 * Do not use -- for serialisation purposes only.
26 @SuppressWarnings("unused")
31 * Create a new {@link Image} with the given data.
36 public Image(byte[] data
) {
37 ByteArrayInputStream in
= new ByteArrayInputStream(data
);
39 this.data
= getTemporaryFile();
40 IOUtils
.write(in
, this.data
);
41 } catch (IOException e
) {
42 throw new RuntimeException(e
);
46 } catch (IOException e
) {
47 throw new RuntimeException(e
);
53 * Create a new {@link Image} from its Base64 representation.
56 * the {@link Image} in Base64 format
59 * in case of I/O error
61 public Image(String base64
) throws IOException
{
62 this(Base64
.decode(base64
));
66 * Create a new {@link Image} from a stream.
72 * in case of I/O error
74 public Image(InputStream in
) throws IOException
{
75 data
= getTemporaryFile();
76 IOUtils
.write(in
, data
);
80 * <b>Read</b> the actual image data, as a byte array.
82 * @return the image data
84 public byte[] getData() {
86 FileInputStream in
= new FileInputStream(data
);
88 return IOUtils
.toByteArray(in
);
92 } catch (IOException e
) {
93 throw new RuntimeException(e
);
98 * Convert the given {@link Image} object into a Base64 representation of
99 * the same {@link Image} object.
101 * @return the Base64 representation
103 public String
toBase64() {
104 return Base64
.encodeBytes(getData());
108 * Closing the {@link Image} will delete the associated temporary file on
111 * Note that even if you don't, the program will still <b>try</b> to delete
112 * all the temporary files at JVM termination.
115 public void close() throws IOException
{
117 synchronized (lock
) {
121 tmpRepository
.close();
122 tmpRepository
= null;
128 protected void finalize() throws Throwable
{
137 * Return a newly created temporary file to work on.
141 * @throws IOException
142 * in case of I/O error
144 private File
getTemporaryFile() throws IOException
{
145 synchronized (lock
) {
146 if (tmpRepository
== null) {
147 tmpRepository
= new TempFiles(tempRoot
, "images");
153 return tmpRepository
.createTempFile("image");
158 * Change the temporary root directory used by the program.
160 * Caution: the directory will be <b>owned</b> by the system, all its files
161 * now belong to us (and will most probably be deleted).
163 * Note: it may take some time until the new temporary root is used, we
164 * first need to make sure the previous one is not used anymore (i.e., we
165 * must reach a point where no unclosed {@link Image} remains in memory) to
166 * switch the temporary root.
169 * the new temporary root, which will be <b>owned</b> by the
172 public static void setTemporaryFilesRoot(File root
) {