Last active
August 29, 2015 14:22
-
-
Save gturedi/bc6ac98f973b43d1b081 to your computer and use it in GitHub Desktop.
simple manager class to write/read (a)synchronously data object to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
public interface ReadCallback<T extends Serializable> { | |
void done(Exception e, T result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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