Fixed classpaths. Changed storage to use deflate compression for disk storage
This commit is contained in:
parent
6a83e33644
commit
ca642c81ce
@ -5,7 +5,7 @@
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||
<classpathentry exported="true" kind="lib" path="/Dependencies/logging/slf4j-api-1.7.5.jar" sourcepath="/Dependencies/logging/slf4j-api-1.7.5-sources.zip"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry kind="lib" path="/Dependencies/kryo/kryo2-debug.jar" sourcepath="/Dependencies/kryo/kryo2-source.jar"/>
|
||||
<classpathentry kind="lib" path="/Dependencies/kryo/kryo2-debug.jar" sourcepath="/Dependencies/kryo/kryo2-source.zip"/>
|
||||
<classpathentry kind="lib" path="/Dependencies/kryo/objenesis-1.2.jar" sourcepath="/Dependencies/kryo/objenesis-1.2-sources.jar"/>
|
||||
<classpathentry kind="lib" path="/Dependencies/logging/logback-classic-1.0.13.jar" sourcepath="/Dependencies/logging/logback-classic-1.0.13-sources.zip"/>
|
||||
<classpathentry kind="lib" path="/Dependencies/logging/logback-core-1.0.13.jar" sourcepath="/Dependencies/logging/logback-core-1.0.13-sources.zip"/>
|
||||
|
@ -60,7 +60,7 @@ public class DelayTimer {
|
||||
@Override
|
||||
public void run() {
|
||||
DelayTimer.this.listener.execute();
|
||||
cancel();
|
||||
DelayTimer.this.cancel();
|
||||
}
|
||||
};
|
||||
this.timer.schedule(t, delay);
|
||||
|
@ -4,12 +4,15 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -34,6 +37,8 @@ public class Storage {
|
||||
private DelayTimer timer;
|
||||
private WeakReference<?> objectReference;
|
||||
|
||||
private Kryo kryo;
|
||||
|
||||
@SuppressWarnings({"rawtypes","unchecked"})
|
||||
public static Storage load(File file, Object loadIntoObject) {
|
||||
if (file == null) {
|
||||
@ -58,7 +63,7 @@ public class Storage {
|
||||
// why load it from disk again? just copy out the values!
|
||||
synchronized (storage) {
|
||||
// have to load from disk!
|
||||
Object source = load(file, loadIntoObject.getClass());
|
||||
Object source = storage.load(file, loadIntoObject.getClass());
|
||||
|
||||
Object orig = storage.objectReference.get();
|
||||
if (orig != null) {
|
||||
@ -81,7 +86,7 @@ public class Storage {
|
||||
storages.put(file, storage);
|
||||
|
||||
// have to load from disk!
|
||||
Object source = load(file, loadIntoObject.getClass());
|
||||
Object source = storage.load(file, loadIntoObject.getClass());
|
||||
if (source != null) {
|
||||
copyFields(source, loadIntoObject);
|
||||
}
|
||||
@ -106,6 +111,9 @@ public class Storage {
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
|
||||
this.kryo = new Kryo();
|
||||
this.kryo.setRegistrationRequired(false);
|
||||
|
||||
this.objectReference = new WeakReference(loadIntoObject);
|
||||
this.timer = new DelayTimer("Storage Writer", new DelayTimer.Callback() {
|
||||
@Override
|
||||
@ -185,18 +193,20 @@ public class Storage {
|
||||
return;
|
||||
}
|
||||
|
||||
Class<? extends Object> class1 = object.getClass();
|
||||
|
||||
RandomAccessFile raf = null;
|
||||
Output output = null;
|
||||
try {
|
||||
raf = new RandomAccessFile(this.file, "rw");
|
||||
FileOutputStream outputStream = new FileOutputStream(raf.getFD());
|
||||
OutputStream outputStream = new DeflaterOutputStream(new FileOutputStream(raf.getFD()));
|
||||
output = new Output(outputStream, 1024); // write 1024 at a time
|
||||
|
||||
|
||||
Kryo kryo = new Kryo();
|
||||
kryo.setRegistrationRequired(false);
|
||||
kryo.writeObject(output, object);
|
||||
this.kryo.writeObject(output, object);
|
||||
output.flush();
|
||||
|
||||
load(this.file, class1);
|
||||
|
||||
} catch (Exception e) {
|
||||
Storage.logger.error("Error saving the data!", e);
|
||||
} finally {
|
||||
@ -214,7 +224,7 @@ public class Storage {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T load(File file, Class<? extends Object> clazz) {
|
||||
private synchronized <T> T load(File file, Class<? extends Object> clazz) {
|
||||
if (file.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
@ -223,11 +233,9 @@ public class Storage {
|
||||
Input input = null;
|
||||
try {
|
||||
raf = new RandomAccessFile(file, "r");
|
||||
input = new Input(new FileInputStream(raf.getFD()), 1024); // read 1024 at a time
|
||||
input = new Input(new InflaterInputStream(new FileInputStream(raf.getFD())), 1024); // read 1024 at a time
|
||||
|
||||
Kryo kryo = new Kryo();
|
||||
kryo.setRegistrationRequired(false);
|
||||
Object readObject = kryo.readObject(input, clazz);
|
||||
Object readObject = this.kryo.readObject(input, clazz);
|
||||
return (T) readObject;
|
||||
} catch (Exception e) {
|
||||
logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath());
|
||||
|
Loading…
Reference in New Issue
Block a user