1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| private boolean swap(K pKey, Serializable pValue) {
int pty = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
cleanup();
K key = pKey; Serializable value = pValue;
buffer(key); buffer(value);
File f = null;
try {
f = new File(cacheDisk_dir + File.separator + "_cache"+hashCode()+key+'.'+cacheDisk_ext);
buffer(f);
oos = new ObjectOutputStream(fos = new FileOutputStream(new RandomAccessFile(f, "rws").getFD())); buffer(oos); buffer(fos);
oos.writeObject(value);
fos.close();
oos.close();
oos = null;
fos = null;
} catch(IOException e) {
e.printStackTrace();
if(f != null)
f.delete();
return false;
} finally {
if(f != null)
cacheDisk.put(key, f);
Thread.currentThread().setPriority(pty);
return true;
}
}
private Object readSwap(K pKey) throws NullPointerException{
int pty = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
cleanup();
K key = pKey;
Object o = null;
try{
if(cacheDisk.containsKey(key)) {
System.out.print("Swap: Found Key " + key + " ");
File f = (File)cacheDisk.get(key);
System.out.println(f.getCanonicalPath());
fis = new FileInputStream(new RandomAccessFile(f, "rws").getFD());
System.out.println("opening...");
ois = new ObjectInputStream(fis); buffer(ois); buffer(fis);
o = ois.readObject(); buffer(o);
//cacheDisk.remove(key);
fis.close();
ois.close();
}
} catch(EOFException e) {
} catch(IOException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
} finally {
ois = null;
fis = null;
if(o == null)
throw new NullPointerException("Null CacheEntry: cannot read on swap!");
else
System.out.println("Swap: reading done.");
Thread.currentThread().setPriority(pty);
return o;
}
} |
Partager