java - Writing a BufferedImage cache and save it to disk -
i'm developing java
application in load long lists containing images (downloaded web), added quick hashmap<string,bufferedimage>
cache, avoid redownloading same image multiple times.
this works fine , application way faster, nice let cache persist through various sessions, changed cache serialized.
bufferedimage
not serializable
, had wrote custom methods.
my file structure should like:
- (int) number of elements
- [(url) image's key
- (object) image written using imageio] n times
while file saving seems fine (at least have no exceptions), when try load url
throws java.io.optionaldataexception
length = 4
, don't understand why. first iteration goes fine, have exception try load second url
, suspect there's wrong in way load first image.
here's full code :
import java.awt.image.bufferedimage; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.url; import java.util.hashmap; import java.util.map.entry; import java.util.logging.level; import java.util.logging.logger; import javax.imageio.imageio; public class picturescache { private static hashmap<string, bufferedimage> picturescache; private static final string cachedisklocation = "pictures_cache.map"; private static void writecache(objectoutputstream oos, hashmap<string, bufferedimage> data) throws ioexception { // number of saved elements oos.writeint(data.size()); // let's write (url, image) each entry in cache (entry<string, bufferedimage> entry : data.entryset()) { oos.writeobject(new url(entry.getkey())); imageio.write(entry.getvalue(), "png", oos); } } private static hashmap<string, bufferedimage> readcache(objectinputstream ois) throws ioexception, classnotfoundexception { // number of saved elements int size = ois.readint(); // cache hashmap<string, bufferedimage> result = new hashmap<>(size); // let's read (url, image) , add them cache (int = 0; < size; i++) { string url = ((url) ois.readobject()).tostring(); // exception here bufferedimage image = imageio.read(ois); result.put(url, image); } return result; } public static void loadcache() { picturescache = new hashmap<>(); file file = new file(cachedisklocation); if (file.isfile()) { fileinputstream fis = null; objectinputstream ois = null; try { fis = new fileinputstream(file); ois = new objectinputstream(fis); picturescache = readcache(ois); } catch (ioexception | classnotfoundexception ex) { logger.getlogger(picturescache.class.getname()).log(level.severe, null, ex); } { try { ois.close(); fis.close(); } catch (ioexception ex) { logger.getlogger(picturescache.class.getname()).log(level.severe, null, ex); } } } system.out.println("cache loaded " + picturescache.size() + " elements"); } public static void savecache() { file file = new file(cachedisklocation); fileoutputstream fos = null; objectoutputstream oos = null; try { if (file.isfile()) { file.delete(); } file.createnewfile(); fos = new fileoutputstream(file); oos = new objectoutputstream(fos); writecache(oos, picturescache); } catch (ioexception ex) { logger.getlogger(picturescache.class.getname()).log(level.severe, null, ex); } { try { system.out.println("cache saved " + picturescache.size() + " elements"); oos.close(); fos.close(); } catch (ioexception ex) { logger.getlogger(picturescache.class.getname()).log(level.severe, null, ex); } } } public static boolean contains(string url) { return picturescache.containskey(url); } public static bufferedimage get(string url) { return picturescache.get(url); } public static void put(string url, bufferedimage image) { picturescache.put(url, image); } }
the error occurs because imageio.read(...)
doesn't read data written using imageio.write(...)
. can write image objectoutputstread
byte[]
. example:
private static void writecache(objectoutputstream oos, hashmap<string, bufferedimage> data) throws ioexception { // number of saved elements oos.writeint(data.size()); // let's write (url, image) each entry in cache (entry<string, bufferedimage> entry : data.entryset()) { oos.writeobject(new url(entry.getkey())); bytearrayoutputstream baos = new bytearrayoutputstream(); imageio.write(entry.getvalue(), "jpg", baos); byte[] bytes = baos.tobytearray(); oos.writeobject(bytes); } } private static hashmap<string, bufferedimage> readcache( objectinputstream ois) throws ioexception, classnotfoundexception { // number of saved elements int size = ois.readint(); // cache hashmap<string, bufferedimage> result = new hashmap<>(size); // let's read (url, image) , add them cache (int = 0; < size; i++) { string url = ((url) ois.readobject()).tostring(); // exception here bytearrayinputstream bais = new bytearrayinputstream( (byte[]) ois.readobject()); bufferedimage image = imageio.read(bais); result.put(url, image); } return result; }
Comments
Post a Comment