f2b94991417ff922dba3a8c00713fe7202912657
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
;
10 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
13 * This class represents an image data.
17 public class Image
implements Closeable
{
18 static private File tempRoot
;
19 static private TempFiles tmpRepository
;
20 static private long count
= 0;
21 static private Object lock
= new Object();
26 * Do not use -- for serialisation purposes only.
28 @SuppressWarnings("unused")
33 * Create a new {@link Image} with the given data.
38 public Image(byte[] data
) {
39 ByteArrayInputStream in
= new ByteArrayInputStream(data
);
41 this.data
= getTemporaryFile();
42 IOUtils
.write(in
, this.data
);
43 } catch (IOException e
) {
44 throw new RuntimeException(e
);
48 } catch (IOException e
) {
49 throw new RuntimeException(e
);
55 * Create a new {@link Image} from a stream.
61 * in case of I/O error
63 public Image(InputStream in
) throws IOException
{
64 data
= getTemporaryFile();
65 IOUtils
.write(in
, data
);
69 * Generate an {@link InputStream} that you can {@link InputStream#reset()}
70 * for this {@link Image}.
72 * This {@link InputStream} will (always) be a new one, and <b>you</b> are
75 * Note: take care that the {@link InputStream} <b>must not</b> live past
76 * the {@link Image} life time!
81 * in case of I/O error
83 public InputStream
newInputStream() throws IOException
{
84 return new MarkableFileInputStream(new FileInputStream(data
));
88 * <b>Read</b> the actual image data, as a byte array.
90 * Note: if possible, prefer the {@link Image#newInputStream()} method, as
91 * it can be more efficient.
93 * @return the image data
95 public byte[] getData() {
97 InputStream in
= newInputStream();
99 return IOUtils
.toByteArray(in
);
103 } catch (IOException e
) {
104 throw new RuntimeException(e
);
109 * Closing the {@link Image} will delete the associated temporary file on
112 * Note that even if you don't, the program will still <b>try</b> to delete
113 * all the temporary files at JVM termination.
116 public void close() throws IOException
{
118 synchronized (lock
) {
122 tmpRepository
.close();
123 tmpRepository
= null;
129 protected void finalize() throws Throwable
{
138 * Return a newly created temporary file to work on.
142 * @throws IOException
143 * in case of I/O error
145 private File
getTemporaryFile() throws IOException
{
146 synchronized (lock
) {
147 if (tmpRepository
== null) {
148 tmpRepository
= new TempFiles(tempRoot
, "images");
154 return tmpRepository
.createTempFile("image");
159 * Change the temporary root directory used by the program.
161 * Caution: the directory will be <b>owned</b> by the system, all its files
162 * now belong to us (and will most probably be deleted).
164 * Note: it may take some time until the new temporary root is used, we
165 * first need to make sure the previous one is not used anymore (i.e., we
166 * must reach a point where no unclosed {@link Image} remains in memory) to
167 * switch the temporary root.
170 * the new temporary root, which will be <b>owned</b> by the
173 public static void setTemporaryFilesRoot(File root
) {