Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/muffin/WebstartMuffin.java
1461 views
1
package org.newdawn.slick.muffin;
2
3
import java.io.DataInputStream;
4
import java.io.DataOutputStream;
5
import java.io.EOFException;
6
import java.io.IOException;
7
import java.net.URL;
8
import java.util.HashMap;
9
import java.util.Iterator;
10
import java.util.Set;
11
12
import javax.jnlp.BasicService;
13
import javax.jnlp.FileContents;
14
import javax.jnlp.PersistenceService;
15
import javax.jnlp.ServiceManager;
16
17
import org.newdawn.slick.util.Log;
18
19
/**
20
* A muffin load/save implementation based on using Webstart Muffins (a bit like cookies only
21
* for webstart)
22
*
23
* @author kappaOne
24
*/
25
public class WebstartMuffin implements Muffin {
26
27
/**
28
* @see org.newdawn.slick.muffin.Muffin#saveFile(java.util.HashMap, java.lang.String)
29
*/
30
public void saveFile(HashMap scoreMap, String fileName) throws IOException {
31
32
PersistenceService ps;
33
BasicService bs;
34
URL configURL;
35
36
try {
37
ps = (PersistenceService) ServiceManager
38
.lookup("javax.jnlp.PersistenceService");
39
bs = (BasicService) ServiceManager
40
.lookup("javax.jnlp.BasicService");
41
URL baseURL = bs.getCodeBase();
42
// System.out.println("CodeBase was " + baseURL);
43
configURL = new URL(baseURL, fileName);
44
} catch (Exception e) {
45
Log.error(e);
46
throw new IOException("Failed to save state: ");
47
}
48
49
try {
50
ps.delete(configURL);
51
} catch (Exception e) {
52
Log.info("No exisiting Muffin Found - First Save");
53
}
54
55
try {
56
ps.create(configURL, 1024); // 1024 bytes for our data
57
58
FileContents fc = ps.get(configURL);
59
DataOutputStream oos = new DataOutputStream(fc
60
.getOutputStream(false));
61
62
// scroll through hashMap and write key and value to file
63
Set keys = scoreMap.keySet(); // get the keys
64
65
// get values using keys
66
for (Iterator i = keys.iterator(); i.hasNext();) {
67
String key = (String) i.next();
68
69
oos.writeUTF(key);
70
71
if (fileName.endsWith("Number")) {
72
double value = ((Double) scoreMap.get(key)).doubleValue();
73
oos.writeDouble(value);
74
} else {
75
String value = (String) scoreMap.get(key);
76
oos.writeUTF(value);
77
}
78
}
79
80
oos.flush();
81
oos.close();
82
} catch (Exception e) {
83
Log.error(e);
84
throw new IOException("Failed to store map of state data");
85
}
86
}
87
88
/**
89
* @see org.newdawn.slick.muffin.Muffin#loadFile(java.lang.String)
90
*/
91
public HashMap loadFile(String fileName) throws IOException {
92
HashMap hashMap = new HashMap();
93
94
try {
95
PersistenceService ps = (PersistenceService) ServiceManager
96
.lookup("javax.jnlp.PersistenceService");
97
BasicService bs = (BasicService) ServiceManager
98
.lookup("javax.jnlp.BasicService");
99
URL baseURL = bs.getCodeBase();
100
URL configURL = new URL(baseURL, fileName);
101
FileContents fc = ps.get(configURL);
102
DataInputStream ois = new DataInputStream(fc.getInputStream());
103
104
// read in data from muffin
105
String key;
106
107
// load hashMap as <String, Int> or <String, String>
108
if (fileName.endsWith("Number")) {
109
double value;
110
// while not end of file
111
while ((key = ois.readUTF()) != null) {
112
value = ois.readDouble();
113
// load value into hashMap
114
hashMap.put(key, new Double(value));
115
}
116
} else {
117
String value;
118
// while not end of file
119
while ((key = ois.readUTF()) != null) {
120
value = ois.readUTF();
121
// load value into hashMap
122
hashMap.put(key, value);
123
}
124
}
125
126
ois.close();
127
} catch (EOFException e) {
128
// End of the file reached, do nothing
129
} catch (IOException e) {
130
// No data there - thats ok, just not saved before
131
} catch (Exception e) {
132
Log.error(e);
133
throw new IOException("Failed to load state from webstart muffin");
134
}
135
136
return hashMap;
137
}
138
}
139
140