1 package be
.nikiroo
.utils
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.Closeable
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
9 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
12 * This class represents an image data.
16 public class Image
implements Closeable
{
17 static private File tempRoot
;
18 static private TempFiles tmpRepository
;
19 static private long count
= 0;
20 static private Object lock
= new Object();
25 * Do not use -- for serialisation purposes only.
27 @SuppressWarnings("unused")
32 * Create a new {@link Image} with the given data.
37 public Image(byte[] data
) {
38 ByteArrayInputStream in
= new ByteArrayInputStream(data
);
40 this.data
= getTemporaryFile();
41 IOUtils
.write(in
, this.data
);
42 } catch (IOException e
) {
43 throw new RuntimeException(e
);
47 } catch (IOException e
) {
48 throw new RuntimeException(e
);
54 * Create a new {@link Image} from a stream.
60 * in case of I/O error
62 public Image(InputStream in
) throws IOException
{
63 data
= getTemporaryFile();
64 IOUtils
.write(in
, data
);
68 * Generate an {@link InputStream} that you can {@link InputStream#reset()}
69 * for this {@link Image}.
71 * This {@link InputStream} will (always) be a new one, and <b>you</b> are
74 * Note: take care that the {@link InputStream} <b>must not</b> live past
75 * the {@link Image} life time!
80 * in case of I/O error
82 public InputStream
newInputStream() throws IOException
{
83 return new MarkableFileInputStream(data
);
87 * <b>Read</b> the actual image data, as a byte array.
89 * Note: if possible, prefer the {@link Image#newInputStream()} method, as
90 * it can be more efficient.
92 * @return the image data
94 public byte[] getData() {
96 InputStream in
= newInputStream();
98 return IOUtils
.toByteArray(in
);
102 } catch (IOException e
) {
103 throw new RuntimeException(e
);
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
) {