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();
29 private Object instanceLock
= new Object();
34 * Do not use -- for serialisation purposes only.
36 @SuppressWarnings("unused")
41 * Create a new {@link Image} with the given data.
46 public Image(byte[] data
) {
47 ByteArrayInputStream in
= new ByteArrayInputStream(data
);
49 this.data
= getTemporaryFile();
50 size
= IOUtils
.write(in
, this.data
);
51 } catch (IOException e
) {
52 throw new RuntimeException(e
);
56 } catch (IOException e
) {
57 throw new RuntimeException(e
);
63 * Create an image from Base64 encoded data.
66 * Please use {@link Image#Image(InputStream)} when possible instead, with a
67 * {@link Base64InputStream}; it can be much more efficient.
69 * @param base64EncodedData
70 * the Base64 encoded data as a String
73 * in case of I/O error or badly formated Base64
75 public Image(String base64EncodedData
) throws IOException
{
76 this(new Base64InputStream(new ByteArrayInputStream(
77 StringUtils
.getBytes(base64EncodedData
)), false));
81 * Create a new {@link Image} from a stream.
87 * in case of I/O error
89 public Image(InputStream in
) throws IOException
{
90 data
= getTemporaryFile();
91 size
= IOUtils
.write(in
, data
);
95 * The size of the enclosed image in bytes.
99 public long getSize() {
104 * Generate an {@link InputStream} that you can {@link InputStream#reset()}
105 * for this {@link Image}.
107 * This {@link InputStream} will (always) be a new one, and <b>you</b> are
108 * responsible for it.
110 * Note: take care that the {@link InputStream} <b>must not</b> live past
111 * the {@link Image} life time!
115 * @throws IOException
116 * in case of I/O error
118 public InputStream
newInputStream() throws IOException
{
119 synchronized (instanceLock
) {
121 throw new IOException("Image was close()d");
124 return new MarkableFileInputStream(data
);
129 * <b>Read</b> the actual image data, as a byte array.
131 * @deprecated if possible, prefer the {@link Image#newInputStream()}
132 * method, as it can be more efficient
134 * @return the image data
137 public byte[] getData() {
139 InputStream in
= newInputStream();
141 return IOUtils
.toByteArray(in
);
145 } catch (IOException e
) {
146 throw new RuntimeException(e
);
151 * Convert the given {@link Image} object into a Base64 representation of
152 * the same {@link Image} object.
154 * @deprecated Please use {@link Image#newInputStream()} instead, it is more
157 * @return the Base64 representation
160 public String
toBase64() {
162 Base64InputStream stream
= new Base64InputStream(newInputStream(),
165 return IOUtils
.readSmallStream(stream
);
169 } catch (IOException e
) {
175 * Closing the {@link Image} will delete the associated temporary file on
178 * Note that even if you don't, the program will still <b>try</b> to delete
179 * all the temporary files at JVM termination.
182 public void close() throws IOException
{
183 synchronized (instanceLock
) {
189 synchronized (lock
) {
193 tmpRepository
.close();
194 tmpRepository
= null;
202 protected void finalize() throws Throwable
{
211 * Return a newly created temporary file to work on.
215 * @throws IOException
216 * in case of I/O error
218 private File
getTemporaryFile() throws IOException
{
219 synchronized (lock
) {
220 if (tmpRepository
== null) {
221 tmpRepository
= new TempFiles(tempRoot
, "images");
227 return tmpRepository
.createTempFile("image");
232 * Write this {@link Image} for serialization purposes; that is, write the
233 * content of the backing temporary file.
236 * the {@link OutputStream} to write to
238 * @throws IOException
239 * in case of I/O error
241 private void writeObject(ObjectOutputStream out
) throws IOException
{
242 InputStream in
= newInputStream();
244 IOUtils
.write(in
, out
);
251 * Read an {@link Image} written by
252 * {@link Image#writeObject(java.io.ObjectOutputStream)}; that is, create a
253 * new temporary file with the saved content.
256 * the {@link InputStream} to read from
257 * @throws IOException
258 * in case of I/O error
259 * @throws ClassNotFoundException
260 * will not be thrown by this method
262 @SuppressWarnings("unused")
263 private void readObject(ObjectInputStream in
) throws IOException
,
264 ClassNotFoundException
{
265 data
= getTemporaryFile();
266 IOUtils
.write(in
, data
);
270 * Change the temporary root directory used by the program.
272 * Caution: the directory will be <b>owned</b> by the system, all its files
273 * now belong to us (and will most probably be deleted).
275 * Note: it may take some time until the new temporary root is used, we
276 * first need to make sure the previous one is not used anymore (i.e., we
277 * must reach a point where no unclosed {@link Image} remains in memory) to
278 * switch the temporary root.
281 * the new temporary root, which will be <b>owned</b> by the
284 public static void setTemporaryFilesRoot(File root
) {