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 * Generate an {@link InputStream} for this {@link Image}.
82 * This {@link InputStream} will (always) be a new one, and <b>you</b> are
85 * Note: take care that the {@link InputStream} <b>must not</b> live past
86 * the {@link Image} life time!
91 * in case of I/O error
93 public InputStream
newInputStream() throws IOException
{
94 return new FileInputStream(data
);
98 * <b>Read</b> the actual image data, as a byte array.
100 * Note: if possible, prefer the {@link Image#newInputStream()} method, as
101 * it can be more efficient.
103 * @return the image data
105 public byte[] getData() {
107 InputStream in
= newInputStream();
109 return IOUtils
.toByteArray(in
);
113 } catch (IOException e
) {
114 throw new RuntimeException(e
);
119 * Convert the given {@link Image} object into a Base64 representation of
120 * the same {@link Image} object.
122 * Note: if possible, prefer the {@link Image#newInputStream()} method, as
123 * it can be more efficient.
125 * @return the Base64 representation
127 public String
toBase64() {
129 return StringUtils
.base64(getData(), false);
130 } catch (IOException e
) {
131 throw new RuntimeException(e
);
136 * Closing the {@link Image} will delete the associated temporary file on
139 * Note that even if you don't, the program will still <b>try</b> to delete
140 * all the temporary files at JVM termination.
143 public void close() throws IOException
{
145 synchronized (lock
) {
149 tmpRepository
.close();
150 tmpRepository
= null;
156 protected void finalize() throws Throwable
{
165 * Return a newly created temporary file to work on.
169 * @throws IOException
170 * in case of I/O error
172 private File
getTemporaryFile() throws IOException
{
173 synchronized (lock
) {
174 if (tmpRepository
== null) {
175 tmpRepository
= new TempFiles(tempRoot
, "images");
181 return tmpRepository
.createTempFile("image");
186 * Change the temporary root directory used by the program.
188 * Caution: the directory will be <b>owned</b> by the system, all its files
189 * now belong to us (and will most probably be deleted).
191 * Note: it may take some time until the new temporary root is used, we
192 * first need to make sure the previous one is not used anymore (i.e., we
193 * must reach a point where no unclosed {@link Image} remains in memory) to
194 * switch the temporary root.
197 * the new temporary root, which will be <b>owned</b> by the
200 public static void setTemporaryFilesRoot(File root
) {