1 |
package appl.data; |
2 |
|
3 |
import java.io.BufferedInputStream; |
4 |
import java.io.BufferedOutputStream; |
5 |
import java.io.File; |
6 |
import java.io.FileInputStream; |
7 |
import java.io.FileNotFoundException; |
8 |
import java.io.FileOutputStream; |
9 |
import java.io.ObjectInputStream; |
10 |
import java.io.ObjectOutputStream; |
11 |
|
12 |
import schmitzm.data.WritableGrid; |
13 |
|
14 |
/** |
15 |
* @author Dominik Appl |
16 |
*/ |
17 |
public class SerializedDataLoader implements DataLoader { |
18 |
|
19 |
protected File loadFile; |
20 |
|
21 |
private boolean del_after_loading; |
22 |
|
23 |
private SerializedDataLoader() { |
24 |
} |
25 |
|
26 |
public SerializedDataLoader(File loadFile, |
27 |
boolean DELETE_SOURCE_AFTER_LOADING) { |
28 |
this.loadFile = loadFile; |
29 |
del_after_loading = DELETE_SOURCE_AFTER_LOADING; |
30 |
} |
31 |
|
32 |
public SerializedDataLoader(File loadFile) { |
33 |
this(loadFile, false); |
34 |
} |
35 |
|
36 |
/* |
37 |
* (non-Javadoc) |
38 |
* |
39 |
* @see appl.data.DataLoader#load() |
40 |
*/ |
41 |
public Object load() throws LoadingException { |
42 |
Object loadedObject = null; |
43 |
try { |
44 |
BufferedInputStream fis = new BufferedInputStream( |
45 |
new FileInputStream(loadFile)); |
46 |
ObjectInputStream ois = new ObjectInputStream(fis); |
47 |
loadedObject = ois.readObject(); |
48 |
ois.close(); |
49 |
fis.close(); |
50 |
// remove File from Filesystem |
51 |
if (del_after_loading) |
52 |
loadFile.delete(); |
53 |
|
54 |
} catch (FileNotFoundException e) { |
55 |
throw new LoadingException("Could not find temporary unload-file:" |
56 |
+ loadFile, loadFile); |
57 |
|
58 |
} catch (Exception e) { |
59 |
e.printStackTrace(); |
60 |
throw new LoadingException("Could not load from temporary file " |
61 |
+ loadFile, loadFile); |
62 |
|
63 |
} |
64 |
return loadedObject; |
65 |
|
66 |
} |
67 |
|
68 |
public static void SerializeToFile(Object o, File destFile) |
69 |
throws LoadingException { |
70 |
try { |
71 |
destFile.createNewFile(); |
72 |
BufferedOutputStream fos = new BufferedOutputStream( |
73 |
new FileOutputStream(destFile)); |
74 |
ObjectOutputStream oos = new ObjectOutputStream(fos); |
75 |
oos.writeObject(o); |
76 |
oos.close(); |
77 |
fos.close(); |
78 |
} catch (Exception e) { |
79 |
e.printStackTrace(); |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
/* |
85 |
* (non-Javadoc) |
86 |
* |
87 |
* @see appl.data.DataLoader#getLoadInfo() |
88 |
*/ |
89 |
public String getLoadInfo() { |
90 |
return "SerializedDataLoader|" + loadFile.getPath(); |
91 |
} |
92 |
|
93 |
} |