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

Last change on this file since 8510 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

File size: 3.6 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.IOException;
7
8import javax.imageio.ImageIO;
9
10
11/**
12 * Cache Entry that has methods to get the BufferedImage, that will be cached along in memory
13 * but will be not serialized when saved to the disk (to avoid duplication of data)
14 * @author Wiktor Niesiobędzki
15 *
16 */
17public class BufferedImageCacheEntry extends CacheEntry {
18 private static final long serialVersionUID = 1L; //version
19 // transient to avoid serialization, volatile to avoid synchronization of whole getImage() method
20 private transient volatile BufferedImage img = null;
21 private transient volatile boolean writtenToDisk = false;
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 = false;
25
26 /**
27 *
28 * @param content byte array containing image
29 */
30 public BufferedImageCacheEntry(byte[] content) {
31 super(content);
32 }
33
34 /**
35 * Returns BufferedImage from for the content. Subsequent calls will return the same instance,
36 * to reduce overhead of ImageIO
37 *
38 * @return BufferedImage of cache entry content
39 * @throws IOException if an error occurs during reading.
40 */
41 public BufferedImage getImage() throws IOException {
42 if (imageLoaded)
43 return img;
44 synchronized (this) {
45 if (imageLoaded)
46 return img;
47 byte[] content = getContent();
48 if (content != null && content.length > 0) {
49 img = ImageIO.read(new ByteArrayInputStream(content));
50 imageLoaded = true;
51
52 if (writtenToDisk)
53 content = null;
54 }
55
56 }
57 return img;
58 }
59
60 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
61 /*
62 * This method below will be needed, if Apache Commons JCS (or any other caching system), will update
63 * disk representation of object from memory, once it is put into the cache (for example - at closing the cache)
64 *
65 * For now it is not the case, as we use DiskUsagePattern.UPDATE, which on JCS shutdown doesn't write again memory
66 * contents to file, so the fact, that we've cleared never gets saved to the disk
67 *
68 * This method is commented out, as it will convert all cache entries to PNG files regardless of what was returned.
69 * It might cause recompression/change of format which may result in decreased quality of imagery
70 */
71 /* synchronized (this) {
72 if (content == null && img != null) {
73 ByteArrayOutputStream restoredData = new ByteArrayOutputStream();
74 ImageIO.write(img, "png", restoredData);
75 content = restoredData.toByteArray();
76 }
77 out.writeObject(this);
78 }
79 */
80 synchronized (this) {
81 if (content == null && img != null) {
82 throw new AssertionError("Trying to serialize (save to disk?) an BufferedImageCacheEntry " +
83 "that was converted to BufferedImage and no raw data is present anymore");
84 }
85 out.writeObject(this);
86 // ugly hack to wait till element will get to disk to clean the memory
87 writtenToDisk = true;
88
89 if (img != null) {
90 content = null;
91 }
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.