source: josm/trunk/src/org/openstreetmap/josm/data/cache/BufferedImageCacheEntry.java@ 17425

Last change on this file since 17425 was 17363, checked in by simon04, 3 years ago

see #20141 - Extract BufferedImageCacheEntry.pngEncoded

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import java.awt.image.BufferedImage;
5import java.io.ByteArrayInputStream;
6import java.io.ByteArrayOutputStream;
7import java.io.IOException;
8import java.io.UncheckedIOException;
9
10import javax.imageio.ImageIO;
11
12/**
13 * Cache Entry that has methods to get the BufferedImage, that will be cached along in memory
14 * but will be not serialized when saved to the disk (to avoid duplication of data)
15 *
16 * @author Wiktor Niesiobędzki
17 */
18public class BufferedImageCacheEntry extends CacheEntry {
19 private static final long serialVersionUID = 1L; //version
20 // transient to avoid serialization, volatile to avoid synchronization of whole getImage() method
21 private transient volatile BufferedImage img;
22 // we need to have separate control variable, to know, if we already tried to load the image, as img might be null
23 // after we loaded image, as for example, when image file is malformed (eg. HTML file)
24 private transient volatile boolean imageLoaded;
25
26 /**
27 *
28 * @param content byte array containing image
29 */
30 public BufferedImageCacheEntry(byte[] content) {
31 super(content);
32 }
33
34 /**
35 * Encodes the given image as PNG and returns a cache entry
36 * @param img the image
37 * @return a cache entry for the PNG encoded image
38 * @throws UncheckedIOException if an I/O error occurs
39 */
40 public static BufferedImageCacheEntry pngEncoded(BufferedImage img) {
41 try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
42 ImageIO.write(img, "png", output);
43 return new BufferedImageCacheEntry(output.toByteArray());
44 } catch (IOException e) {
45 throw new UncheckedIOException(e);
46 }
47 }
48
49 /**
50 * Returns BufferedImage from for the content. Subsequent calls will return the same instance,
51 * to reduce overhead of ImageIO
52 *
53 * @return BufferedImage of cache entry content
54 * @throws IOException if an error occurs during reading.
55 */
56 public BufferedImage getImage() throws IOException {
57 if (imageLoaded)
58 return img;
59 synchronized (this) {
60 if (imageLoaded)
61 return img;
62 byte[] content = getContent();
63 if (content.length > 0) {
64 img = ImageIO.read(new ByteArrayInputStream(content));
65 imageLoaded = true;
66 }
67 }
68 return img;
69 }
70
71 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
72 /*
73 * This method below will be needed, if Apache Commons JCS (or any other caching system), will update
74 * disk representation of object from memory, once it is put into the cache (for example - at closing the cache)
75 *
76 * For now it is not the case, as we use DiskUsagePattern.UPDATE, which on JCS shutdown doesn't write again memory
77 * contents to file, so the fact, that we've cleared never gets saved to the disk
78 *
79 * This method is commented out, as it will convert all cache entries to PNG files regardless of what was returned.
80 * It might cause recompression/change of format which may result in decreased quality of imagery
81 */
82 /* synchronized (this) {
83 if (content == null && img != null) {
84 ByteArrayOutputStream restoredData = new ByteArrayOutputStream();
85 ImageIO.write(img, "png", restoredData);
86 content = restoredData.toByteArray();
87 }
88 out.writeObject(this);
89 }
90 */
91 synchronized (this) {
92 if (content == null && img != null) {
93 throw new AssertionError("Trying to serialize (save to disk?) an BufferedImageCacheEntry " +
94 "that was converted to BufferedImage and no raw data is present anymore");
95 }
96 out.writeObject(this);
97
98 if (img != null) {
99 content = null;
100 }
101 }
102 }
103}
Note: See TracBrowser for help on using the repository browser.