64f7b5a18f583ca2df5ebb055f3ac081f36ea9eb
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
.ObjectInputStream
;
9 import java
.io
.ObjectOutputStream
;
10 import java
.io
.OutputStream
;
11 import java
.io
.Serializable
;
13 import be
.nikiroo
.utils
.streams
.Base64InputStream
;
14 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
17 * This class represents an image data.
21 public class Image
implements Closeable
, Serializable
{
22 static private final long serialVersionUID
= 1L;
24 static private File tempRoot
;
25 static private TempFiles tmpRepository
;
26 static private long count
= 0;
27 static private Object lock
= new Object();
32 * Do not use -- for serialisation purposes only.
34 @SuppressWarnings("unused")
39 * Create a new {@link Image} with the given data.
44 public Image(byte[] data
) {
45 ByteArrayInputStream in
= new ByteArrayInputStream(data
);
47 this.data
= getTemporaryFile();
48 IOUtils
.write(in
, this.data
);
49 } catch (IOException e
) {
50 throw new RuntimeException(e
);
54 } catch (IOException e
) {
55 throw new RuntimeException(e
);
61 * Create an image from Base64 encoded data.
63 * @deprecated Please use {@link Image#Image(InputStream)} instead, with a
64 * {@link Base64InputStream}
66 * @param base64EncodedData
67 * the Base64 encoded data as a String
70 * in case of I/O error or badly formated Base64
73 public Image(String base64EncodedData
) throws IOException
{
74 this(new Base64InputStream(new ByteArrayInputStream(
75 StringUtils
.getBytes(base64EncodedData
)), false));
79 * Create a new {@link Image} from a stream.
85 * in case of I/O error
87 public Image(InputStream in
) throws IOException
{
88 data
= getTemporaryFile();
89 IOUtils
.write(in
, data
);
93 * Generate an {@link InputStream} that you can {@link InputStream#reset()}
94 * for this {@link Image}.
96 * This {@link InputStream} will (always) be a new one, and <b>you</b> are
99 * Note: take care that the {@link InputStream} <b>must not</b> live past
100 * the {@link Image} life time!
104 * @throws IOException
105 * in case of I/O error
107 public InputStream
newInputStream() throws IOException
{
108 return new MarkableFileInputStream(data
);
112 * <b>Read</b> the actual image data, as a byte array.
114 * @deprecated if possible, prefer the {@link Image#newInputStream()}
115 * method, as it can be more efficient
117 * @return the image data
120 public byte[] getData() {
122 InputStream in
= newInputStream();
124 return IOUtils
.toByteArray(in
);
128 } catch (IOException e
) {
129 throw new RuntimeException(e
);
134 * Convert the given {@link Image} object into a Base64 representation of
135 * the same {@link Image} object.
137 * @deprecated Please use {@link Image#newInputStream()} instead, it is more
140 * @return the Base64 representation
143 public String
toBase64() {
145 Base64InputStream stream
= new Base64InputStream(newInputStream(),
148 return IOUtils
.readSmallStream(stream
);
152 } catch (IOException e
) {
158 * Closing the {@link Image} will delete the associated temporary file on
161 * Note that even if you don't, the program will still <b>try</b> to delete
162 * all the temporary files at JVM termination.
165 public void close() throws IOException
{
167 synchronized (lock
) {
171 tmpRepository
.close();
172 tmpRepository
= null;
178 protected void finalize() throws Throwable
{
187 * Return a newly created temporary file to work on.
191 * @throws IOException
192 * in case of I/O error
194 private File
getTemporaryFile() throws IOException
{
195 synchronized (lock
) {
196 if (tmpRepository
== null) {
197 tmpRepository
= new TempFiles(tempRoot
, "images");
203 return tmpRepository
.createTempFile("image");
208 * Write this {@link Image} for serialization purposes; that is, write the
209 * content of the backing temporary file.
212 * the {@link OutputStream} to write to
214 * @throws IOException
215 * in case of I/O error
217 private void writeObject(ObjectOutputStream out
) throws IOException
{
218 InputStream in
= newInputStream();
220 IOUtils
.write(in
, out
);
227 * Read an {@link Image} written by
228 * {@link Image#writeObject(java.io.ObjectOutputStream)}; that is, create a
229 * new temporary file with the saved content.
232 * the {@link InputStream} to read from
233 * @throws IOException
234 * in case of I/O error
235 * @throws ClassNotFoundException
236 * will not be thrown by this method
238 private void readObject(ObjectInputStream in
) throws IOException
,
239 ClassNotFoundException
{
240 data
= getTemporaryFile();
241 IOUtils
.write(in
, data
);
245 * Change the temporary root directory used by the program.
247 * Caution: the directory will be <b>owned</b> by the system, all its files
248 * now belong to us (and will most probably be deleted).
250 * Note: it may take some time until the new temporary root is used, we
251 * first need to make sure the previous one is not used anymore (i.e., we
252 * must reach a point where no unclosed {@link Image} remains in memory) to
253 * switch the temporary root.
256 * the new temporary root, which will be <b>owned</b> by the
259 public static void setTemporaryFilesRoot(File root
) {