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

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

sonar - squid:S3052 - Fields should not be initialized to default values

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