Skip to content

Instantly share code, notes, and snippets.

@gturedi
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gturedi/bc6ac98f973b43d1b081 to your computer and use it in GitHub Desktop.
Save gturedi/bc6ac98f973b43d1b081 to your computer and use it in GitHub Desktop.
simple manager class to write/read (a)synchronously data object to file
import java.io.Serializable;
public interface ReadCallback<T extends Serializable> {
void done(Exception e, T result);
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SimpleStorageManager<T extends Serializable> {
private final String path;
public SimpleStorageManager(String path) {
this.path = path;
}
public T read()
throws IOException, ClassNotFoundException {
return readObject();
}
public void readAsync(final ReadCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
T result = read();
callback.done(null, result);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
callback.done(e, null);
}
}
}).start();
}
public void write(final T object)
throws IOException {
writeObject(object);
}
public void writeAsync(final T object, final WriteCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
write(object);
callback.done(null);
} catch (IOException e) {
e.printStackTrace();
callback.done(e);
}
}
}).start();
}
private synchronized void writeObject(T object)
throws IOException {
if (!new File(path).exists()) new File(path).createNewFile();
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(object);
fos.close();
os.close();
}
private synchronized T readObject()
throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(path);
ObjectInputStream is = new ObjectInputStream(fis);
T result = (T) is.readObject();
fis.close();
is.close();
return result;
}
}
import java.io.IOException;
public class Test {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
// synchronized usage
Car car = new Car(1, "title1");
String path = "sample.dat";
final SimpleStorageManager<Car> manager = new SimpleStorageManager<>(path);
manager.write(car);
Car copy = manager.read();
System.out.println(copy.title);
// asynchronized usage
car.title = "title2";
manager.writeAsync(car, new WriteCallback() {
@Override
public void done(Exception e) {
if (e != null) return;
manager.readAsync(new ReadCallback<Car>() {
@Override
public void done(Exception e, Car result) {
if (e != null) return;
System.out.println(result.title);
}
});
}
});
}
}
public interface WriteCallback {
void done(Exception e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment