2e9c9f83a39bfd4fcdeef8d351071d8284e9af61
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
;
8 import java
.io
.OutputStream
;
9 import java
.io
.Serializable
;
11 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
14 * This class represents an image data.
18 public class Image
implements Closeable
, Serializable
{
19 static private final long serialVersionUID
= 1L;
21 static private File tempRoot
;
22 static private TempFiles tmpRepository
;
23 static private long count
= 0;
24 static private Object lock
= new Object();
29 * Do not use -- for serialisation purposes only.
31 @SuppressWarnings("unused")
36 * Create a new {@link Image} with the given data.
41 public Image(byte[] data
) {
42 ByteArrayInputStream in
= new ByteArrayInputStream(data
);
44 this.data
= getTemporaryFile();
45 IOUtils
.write(in
, this.data
);
46 } catch (IOException e
) {
47 throw new RuntimeException(e
);
51 } catch (IOException e
) {
52 throw new RuntimeException(e
);
58 * Create a new {@link Image} from a stream.
64 * in case of I/O error
66 public Image(InputStream in
) throws IOException
{
67 data
= getTemporaryFile();
68 IOUtils
.write(in
, data
);
72 * Generate an {@link InputStream} that you can {@link InputStream#reset()}
73 * for this {@link Image}.
75 * This {@link InputStream} will (always) be a new one, and <b>you</b> are
78 * Note: take care that the {@link InputStream} <b>must not</b> live past
79 * the {@link Image} life time!
84 * in case of I/O error
86 public InputStream
newInputStream() throws IOException
{
87 return new MarkableFileInputStream(data
);
91 * <b>Read</b> the actual image data, as a byte array.
93 * Note: if possible, prefer the {@link Image#newInputStream()} method, as
94 * it can be more efficient.
96 * @return the image data
98 public byte[] getData() {
100 InputStream in
= newInputStream();
102 return IOUtils
.toByteArray(in
);
106 } catch (IOException e
) {
107 throw new RuntimeException(e
);
112 * Closing the {@link Image} will delete the associated temporary file on
115 * Note that even if you don't, the program will still <b>try</b> to delete
116 * all the temporary files at JVM termination.
119 public void close() throws IOException
{
121 synchronized (lock
) {
125 tmpRepository
.close();
126 tmpRepository
= null;
132 protected void finalize() throws Throwable
{
141 * Return a newly created temporary file to work on.
145 * @throws IOException
146 * in case of I/O error
148 private File
getTemporaryFile() throws IOException
{
149 synchronized (lock
) {
150 if (tmpRepository
== null) {
151 tmpRepository
= new TempFiles(tempRoot
, "images");
157 return tmpRepository
.createTempFile("image");
162 * Write this {@link Image} for serialization purposes; that is, write the
163 * content of the backing temporary file.
166 * the {@link OutputStream} to write to
168 * @throws IOException
169 * in case of I/O error
171 private void writeObject(java
.io
.ObjectOutputStream out
) throws IOException
{
172 InputStream in
= newInputStream();
174 IOUtils
.write(in
, out
);
181 * Read an {@link Image} written by
182 * {@link Image#writeObject(java.io.ObjectOutputStream)}; that is, create a
183 * new temporary file with the saved content.
186 * the {@link InputStream} to read from
187 * @throws IOException
188 * in case of I/O error
189 * @throws ClassNotFoundException
190 * will not be thrown by this method
192 @SuppressWarnings("unused")
193 private void readObject(java
.io
.ObjectInputStream in
) throws IOException
,
194 ClassNotFoundException
{
195 data
= getTemporaryFile();
196 IOUtils
.write(in
, data
);
200 * Change the temporary root directory used by the program.
202 * Caution: the directory will be <b>owned</b> by the system, all its files
203 * now belong to us (and will most probably be deleted).
205 * Note: it may take some time until the new temporary root is used, we
206 * first need to make sure the previous one is not used anymore (i.e., we
207 * must reach a point where no unclosed {@link Image} remains in memory) to
208 * switch the temporary root.
211 * the new temporary root, which will be <b>owned</b> by the
214 public static void setTemporaryFilesRoot(File root
) {