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

Last change on this file since 10308 was 10247, checked in by Don-vip, 8 years ago

findbugs - DLS_DEAD_LOCAL_STORE_OF_NULL

  • Property svn:eol-style set to native
File size: 3.5 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 }
52 return img;
53 }
54
55 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
56 /*
57 * This method below will be needed, if Apache Commons JCS (or any other caching system), will update
58 * disk representation of object from memory, once it is put into the cache (for example - at closing the cache)
59 *
60 * For now it is not the case, as we use DiskUsagePattern.UPDATE, which on JCS shutdown doesn't write again memory
61 * contents to file, so the fact, that we've cleared never gets saved to the disk
62 *
63 * This method is commented out, as it will convert all cache entries to PNG files regardless of what was returned.
64 * It might cause recompression/change of format which may result in decreased quality of imagery
65 */
66 /* synchronized (this) {
67 if (content == null && img != null) {
68 ByteArrayOutputStream restoredData = new ByteArrayOutputStream();
69 ImageIO.write(img, "png", restoredData);
70 content = restoredData.toByteArray();
71 }
72 out.writeObject(this);
73 }
74 */
75 synchronized (this) {
76 if (content == null && img != null) {
77 throw new AssertionError("Trying to serialize (save to disk?) an BufferedImageCacheEntry " +
78 "that was converted to BufferedImage and no raw data is present anymore");
79 }
80 out.writeObject(this);
81 // ugly hack to wait till element will get to disk to clean the memory
82 writtenToDisk = true;
83
84 if (img != null) {
85 content = null;
86 }
87 }
88 }
89}
Note: See TracBrowser for help on using the repository browser.