Java Reference
In-Depth Information
L ISTING 11.5 Continued
11
*/
public class PersistentStorage {
private PersistenceService persistenceService;
private long DEFAULT_SIZE = 2048L;
public PersistentStorage() {
persistenceService =
(PersistenceService)Utilities.getService(“javax.jnlp.PersistenceService”);
}
public PersistentStorage(PersistenceService ps) {
persistenceService = ps;
}
public void write(String keyString, Object value) {
write(keyString, value, DEFAULT_SIZE);
}
public void write(String keyString, Object value, long maxLength) {
write(getUrl(keyString), value, DEFAULT_SIZE);
}
public void write(URL url, Object value, long maxLength) {
if (exists(url))
removeEntry(url);
try {
persistenceService.create(url, maxLength);
FileContents fc = persistenceService.get(url);
ObjectOutputStream oos =
new ObjectOutputStream(fc.getOutputStream(false));
oos.writeObject(value);
oos.close();
} catch (IOException ioe) {
System.out.println(getClass()+”.write(“+url+”, “+value+”): “+ioe);
}
}
public Object read(String keyString) {
return read(getUrl(keyString));
}
public Object read(URL url) {
if (!exists(url))
return null;
Object object = null;
try {
FileContents fc = persistenceService.get(url);
ObjectInputStream ois = new ObjectInputStream(fc.getInputStream());
object = ois.readObject();
} catch (Exception e) {
Search WWH ::




Custom Search