| 1 | package be.nikiroo.utils; |
| 2 | |
| 3 | import java.io.ByteArrayInputStream; |
| 4 | import java.io.Closeable; |
| 5 | import java.io.File; |
| 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; |
| 12 | |
| 13 | import be.nikiroo.utils.streams.Base64InputStream; |
| 14 | import be.nikiroo.utils.streams.MarkableFileInputStream; |
| 15 | |
| 16 | /** |
| 17 | * This class represents an image data. |
| 18 | * |
| 19 | * @author niki |
| 20 | */ |
| 21 | public class Image implements Closeable, Serializable { |
| 22 | static private final long serialVersionUID = 1L; |
| 23 | |
| 24 | static private File tempRoot; |
| 25 | static private TempFiles tmpRepository; |
| 26 | static private long count = 0; |
| 27 | static private Object lock = new Object(); |
| 28 | |
| 29 | private File data; |
| 30 | |
| 31 | /** |
| 32 | * Do not use -- for serialisation purposes only. |
| 33 | */ |
| 34 | @SuppressWarnings("unused") |
| 35 | private Image() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Create a new {@link Image} with the given data. |
| 40 | * |
| 41 | * @param data |
| 42 | * the data |
| 43 | */ |
| 44 | public Image(byte[] data) { |
| 45 | ByteArrayInputStream in = new ByteArrayInputStream(data); |
| 46 | try { |
| 47 | this.data = getTemporaryFile(); |
| 48 | IOUtils.write(in, this.data); |
| 49 | } catch (IOException e) { |
| 50 | throw new RuntimeException(e); |
| 51 | } finally { |
| 52 | try { |
| 53 | in.close(); |
| 54 | } catch (IOException e) { |
| 55 | throw new RuntimeException(e); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Create an image from Base64 encoded data. |
| 62 | * |
| 63 | * @deprecated Please use {@link Image#Image(InputStream)} instead, with a |
| 64 | * {@link Base64InputStream} |
| 65 | * |
| 66 | * @param base64EncodedData |
| 67 | * the Base64 encoded data as a String |
| 68 | * |
| 69 | * @throws IOException |
| 70 | * in case of I/O error or badly formated Base64 |
| 71 | */ |
| 72 | @Deprecated |
| 73 | public Image(String base64EncodedData) throws IOException { |
| 74 | this(new Base64InputStream(new ByteArrayInputStream( |
| 75 | StringUtils.getBytes(base64EncodedData)), false)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Create a new {@link Image} from a stream. |
| 80 | * |
| 81 | * @param in |
| 82 | * the stream |
| 83 | * |
| 84 | * @throws IOException |
| 85 | * in case of I/O error |
| 86 | */ |
| 87 | public Image(InputStream in) throws IOException { |
| 88 | data = getTemporaryFile(); |
| 89 | IOUtils.write(in, data); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Generate an {@link InputStream} that you can {@link InputStream#reset()} |
| 94 | * for this {@link Image}. |
| 95 | * <p> |
| 96 | * This {@link InputStream} will (always) be a new one, and <b>you</b> are |
| 97 | * responsible for it. |
| 98 | * <p> |
| 99 | * Note: take care that the {@link InputStream} <b>must not</b> live past |
| 100 | * the {@link Image} life time! |
| 101 | * |
| 102 | * @return the stream |
| 103 | * |
| 104 | * @throws IOException |
| 105 | * in case of I/O error |
| 106 | */ |
| 107 | public InputStream newInputStream() throws IOException { |
| 108 | return new MarkableFileInputStream(data); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * <b>Read</b> the actual image data, as a byte array. |
| 113 | * |
| 114 | * @deprecated if possible, prefer the {@link Image#newInputStream()} |
| 115 | * method, as it can be more efficient |
| 116 | * |
| 117 | * @return the image data |
| 118 | */ |
| 119 | @Deprecated |
| 120 | public byte[] getData() { |
| 121 | try { |
| 122 | InputStream in = newInputStream(); |
| 123 | try { |
| 124 | return IOUtils.toByteArray(in); |
| 125 | } finally { |
| 126 | in.close(); |
| 127 | } |
| 128 | } catch (IOException e) { |
| 129 | throw new RuntimeException(e); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Convert the given {@link Image} object into a Base64 representation of |
| 135 | * the same {@link Image} object. |
| 136 | * |
| 137 | * @deprecated Please use {@link Image#newInputStream()} instead, it is more |
| 138 | * efficient |
| 139 | * |
| 140 | * @return the Base64 representation |
| 141 | */ |
| 142 | @Deprecated |
| 143 | public String toBase64() { |
| 144 | try { |
| 145 | Base64InputStream stream = new Base64InputStream(newInputStream(), |
| 146 | true); |
| 147 | try { |
| 148 | return IOUtils.readSmallStream(stream); |
| 149 | } finally { |
| 150 | stream.close(); |
| 151 | } |
| 152 | } catch (IOException e) { |
| 153 | return null; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Closing the {@link Image} will delete the associated temporary file on |
| 159 | * disk. |
| 160 | * <p> |
| 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. |
| 163 | */ |
| 164 | @Override |
| 165 | public void close() throws IOException { |
| 166 | data.delete(); |
| 167 | synchronized (lock) { |
| 168 | count--; |
| 169 | if (count <= 0) { |
| 170 | count = 0; |
| 171 | tmpRepository.close(); |
| 172 | tmpRepository = null; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void finalize() throws Throwable { |
| 179 | try { |
| 180 | close(); |
| 181 | } finally { |
| 182 | super.finalize(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Return a newly created temporary file to work on. |
| 188 | * |
| 189 | * @return the file |
| 190 | * |
| 191 | * @throws IOException |
| 192 | * in case of I/O error |
| 193 | */ |
| 194 | private File getTemporaryFile() throws IOException { |
| 195 | synchronized (lock) { |
| 196 | if (tmpRepository == null) { |
| 197 | tmpRepository = new TempFiles(tempRoot, "images"); |
| 198 | count = 0; |
| 199 | } |
| 200 | |
| 201 | count++; |
| 202 | |
| 203 | return tmpRepository.createTempFile("image"); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Write this {@link Image} for serialization purposes; that is, write the |
| 209 | * content of the backing temporary file. |
| 210 | * |
| 211 | * @param out |
| 212 | * the {@link OutputStream} to write to |
| 213 | * |
| 214 | * @throws IOException |
| 215 | * in case of I/O error |
| 216 | */ |
| 217 | private void writeObject(ObjectOutputStream out) throws IOException { |
| 218 | InputStream in = newInputStream(); |
| 219 | try { |
| 220 | IOUtils.write(in, out); |
| 221 | } finally { |
| 222 | in.close(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 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. |
| 230 | * |
| 231 | * @param in |
| 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 |
| 237 | */ |
| 238 | private void readObject(ObjectInputStream in) throws IOException, |
| 239 | ClassNotFoundException { |
| 240 | data = getTemporaryFile(); |
| 241 | IOUtils.write(in, data); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Change the temporary root directory used by the program. |
| 246 | * <p> |
| 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). |
| 249 | * <p> |
| 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. |
| 254 | * |
| 255 | * @param root |
| 256 | * the new temporary root, which will be <b>owned</b> by the |
| 257 | * system |
| 258 | */ |
| 259 | public static void setTemporaryFilesRoot(File root) { |
| 260 | tempRoot = root; |
| 261 | } |
| 262 | } |