1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.application.state.impl;
21
22 import com.cosylab.application.state.*;
23
24 import java.io.EOFException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.io.OutputStream;
30
31
32
33
34
35
36
37 public class SerialStateStorage extends DefaultStateStorage {
38
39
40
41 public SerialStateStorage() {
42 super();
43 }
44
45
46
47
48
49
50
51 public SerialStateStorage(StateStorage ss) {
52 super(ss);
53 }
54
55
56
57
58
59
60 public void load(String filePath, String applicationName)
61 throws IOException {
62 if (delegate != null) {
63 delegate.load(filePath, applicationName);
64 addAll(delegate.getStates());
65
66 return;
67 }
68
69
70 InputStream is = getInputStream(filePath, applicationName + ".bin");
71 load(is);
72 }
73
74
75
76
77
78
79 public void load(InputStream is) throws IOException {
80 try {
81 if (is == null) {
82 return;
83 }
84
85 ObjectInputStream ois = new ObjectInputStream(is);
86
87 while (true) {
88 states.add(ois.readObject());
89 }
90 } catch (ClassNotFoundException e) {
91 throw new IOException(e.getMessage());
92 } catch (EOFException e) {
93
94 }
95 }
96
97
98
99
100
101
102
103 public void store(String filePath, String applicationName)
104 throws IOException {
105 OutputStream os = getOutputStream(filePath, applicationName + ".bin");
106 store(os);
107 }
108
109
110
111
112
113
114 public void store(OutputStream os) throws IOException {
115 if (os == null) {
116 return;
117 }
118
119 ObjectOutputStream oos = new ObjectOutputStream(os);
120
121 for (int i = 0; i < states.size(); i++) {
122 oos.writeObject(states.get(i));
123 }
124
125 os.flush();
126 os.close();
127 }
128 }
129
130