1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of Java-Common.
5 *
6 * Java-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Java-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>.
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 * This class implements the state storage and saves states into a binary file
33 * using simple JAVA serialization.
34 *
35 * @author dvitas
36 */
37 public class SerialStateStorage extends DefaultStateStorage {
38 /**
39 *
40 */
41 public SerialStateStorage() {
42 super();
43 }
44
45 /**
46 * Creates a new SerialStateStorage object.
47 *
48 * @param ss
49 * DOCUMENT ME!
50 */
51 public SerialStateStorage(StateStorage ss) {
52 super(ss);
53 }
54
55 /*
56 * (non-Javadoc)
57 *
58 * @see com.cosylab.application.state.StateStorage#load(java.io.InputStream)
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 // get the InputStream if file exists
70 InputStream is = getInputStream(filePath, applicationName + ".bin");
71 load(is);
72 }
73
74 /*
75 * (non-Javadoc)
76 *
77 * @see com.cosylab.application.state.StateStorage#load(java.io.InputStream)
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 // end of stream
94 }
95 }
96
97 /*
98 * (non-Javadoc)
99 *
100 * @see com.cosylab.application.state.StateStorage#store(java.io.OutputStream,
101 * com.cosylab.application.state.State[])
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 * (non-Javadoc)
111 *
112 * @see com.cosylab.application.state.StateStorage#store(java.io.OutputStream)
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 /* __oOo__ */