Path: blob/master/SLICK_HOME/src/org/newdawn/slick/muffin/WebstartMuffin.java
1461 views
package org.newdawn.slick.muffin;12import java.io.DataInputStream;3import java.io.DataOutputStream;4import java.io.EOFException;5import java.io.IOException;6import java.net.URL;7import java.util.HashMap;8import java.util.Iterator;9import java.util.Set;1011import javax.jnlp.BasicService;12import javax.jnlp.FileContents;13import javax.jnlp.PersistenceService;14import javax.jnlp.ServiceManager;1516import org.newdawn.slick.util.Log;1718/**19* A muffin load/save implementation based on using Webstart Muffins (a bit like cookies only20* for webstart)21*22* @author kappaOne23*/24public class WebstartMuffin implements Muffin {2526/**27* @see org.newdawn.slick.muffin.Muffin#saveFile(java.util.HashMap, java.lang.String)28*/29public void saveFile(HashMap scoreMap, String fileName) throws IOException {3031PersistenceService ps;32BasicService bs;33URL configURL;3435try {36ps = (PersistenceService) ServiceManager37.lookup("javax.jnlp.PersistenceService");38bs = (BasicService) ServiceManager39.lookup("javax.jnlp.BasicService");40URL baseURL = bs.getCodeBase();41// System.out.println("CodeBase was " + baseURL);42configURL = new URL(baseURL, fileName);43} catch (Exception e) {44Log.error(e);45throw new IOException("Failed to save state: ");46}4748try {49ps.delete(configURL);50} catch (Exception e) {51Log.info("No exisiting Muffin Found - First Save");52}5354try {55ps.create(configURL, 1024); // 1024 bytes for our data5657FileContents fc = ps.get(configURL);58DataOutputStream oos = new DataOutputStream(fc59.getOutputStream(false));6061// scroll through hashMap and write key and value to file62Set keys = scoreMap.keySet(); // get the keys6364// get values using keys65for (Iterator i = keys.iterator(); i.hasNext();) {66String key = (String) i.next();6768oos.writeUTF(key);6970if (fileName.endsWith("Number")) {71double value = ((Double) scoreMap.get(key)).doubleValue();72oos.writeDouble(value);73} else {74String value = (String) scoreMap.get(key);75oos.writeUTF(value);76}77}7879oos.flush();80oos.close();81} catch (Exception e) {82Log.error(e);83throw new IOException("Failed to store map of state data");84}85}8687/**88* @see org.newdawn.slick.muffin.Muffin#loadFile(java.lang.String)89*/90public HashMap loadFile(String fileName) throws IOException {91HashMap hashMap = new HashMap();9293try {94PersistenceService ps = (PersistenceService) ServiceManager95.lookup("javax.jnlp.PersistenceService");96BasicService bs = (BasicService) ServiceManager97.lookup("javax.jnlp.BasicService");98URL baseURL = bs.getCodeBase();99URL configURL = new URL(baseURL, fileName);100FileContents fc = ps.get(configURL);101DataInputStream ois = new DataInputStream(fc.getInputStream());102103// read in data from muffin104String key;105106// load hashMap as <String, Int> or <String, String>107if (fileName.endsWith("Number")) {108double value;109// while not end of file110while ((key = ois.readUTF()) != null) {111value = ois.readDouble();112// load value into hashMap113hashMap.put(key, new Double(value));114}115} else {116String value;117// while not end of file118while ((key = ois.readUTF()) != null) {119value = ois.readUTF();120// load value into hashMap121hashMap.put(key, value);122}123}124125ois.close();126} catch (EOFException e) {127// End of the file reached, do nothing128} catch (IOException e) {129// No data there - thats ok, just not saved before130} catch (Exception e) {131Log.error(e);132throw new IOException("Failed to load state from webstart muffin");133}134135return hashMap;136}137}138139140