1 | package negotiator.boaframework.agent;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.BufferedOutputStream;
|
---|
5 | import java.io.ByteArrayInputStream;
|
---|
6 | import java.io.ByteArrayOutputStream;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.FileOutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.io.ObjectInputStream;
|
---|
13 | import java.io.ObjectOutputStream;
|
---|
14 | import java.io.OutputStream;
|
---|
15 |
|
---|
16 | import javax.xml.bind.DatatypeConverter;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Series of methods to (un)serialize an object to a string to store it as a
|
---|
20 | * file.
|
---|
21 | *
|
---|
22 | * @author Tim Baarslag
|
---|
23 | * @param <A>
|
---|
24 | * class of the object which is serialized.
|
---|
25 | */
|
---|
26 | public class Serializer<A> {
|
---|
27 |
|
---|
28 | /** Path to the file in which the serialized class must be stored. */
|
---|
29 | private final String fileName;
|
---|
30 | /** If it should be reported if the file cannot be found. */
|
---|
31 | private final boolean log;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create an object to serialize a class. The filename specifies the path in
|
---|
35 | * which the serialized class is stored. File not found exceptions are not
|
---|
36 | * reported.
|
---|
37 | *
|
---|
38 | * @param fileName
|
---|
39 | * path to file in which the serialized class is stored.
|
---|
40 | */
|
---|
41 | public Serializer(String fileName) {
|
---|
42 | this(fileName, false);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Create an object to serialize a class. The filename specifies the path in
|
---|
47 | * which the serialized class is stored.
|
---|
48 | *
|
---|
49 | * @param fileName
|
---|
50 | * path to file in which the serialized class is stored.
|
---|
51 | * @param log
|
---|
52 | * specifies if file not found exceptions should be reported.
|
---|
53 | */
|
---|
54 | public Serializer(String fileName, boolean log) {
|
---|
55 | super();
|
---|
56 | this.fileName = fileName;
|
---|
57 | this.log = log;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Read a serialized object from a file and restore it.
|
---|
62 | *
|
---|
63 | * @return unserialized object.
|
---|
64 | */
|
---|
65 | @SuppressWarnings("unchecked")
|
---|
66 | public A readFromDisk() {
|
---|
67 | InputStream is = null;
|
---|
68 | ObjectInputStream ois = null;
|
---|
69 | A obj = null;
|
---|
70 |
|
---|
71 | final String errorMsg = "Error opening (" + fileName + ").\n";
|
---|
72 | try {
|
---|
73 | is = new BufferedInputStream(new FileInputStream(fileName), 50000 * 1024);
|
---|
74 |
|
---|
75 | ois = new ObjectInputStream(is);
|
---|
76 |
|
---|
77 | final Object readObject = ois.readObject();
|
---|
78 | ois.close();
|
---|
79 | is.close();
|
---|
80 | obj = (A) readObject;
|
---|
81 | return obj;
|
---|
82 | } catch (FileNotFoundException e) {
|
---|
83 | if (log)
|
---|
84 | System.out.println(errorMsg + e);
|
---|
85 | } catch (IOException e) {
|
---|
86 | System.out.println(errorMsg + e);
|
---|
87 |
|
---|
88 | } catch (ClassNotFoundException e) {
|
---|
89 | System.out.println(errorMsg + e);
|
---|
90 | } catch (ClassCastException e) {
|
---|
91 | System.out.println(errorMsg + e);
|
---|
92 | }
|
---|
93 | System.out.println(fileName + " is old; It should be rebuilt.");
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Serializes an object to the specified file.
|
---|
99 | *
|
---|
100 | * @param a
|
---|
101 | * object to be serialized.
|
---|
102 | */
|
---|
103 | public void writeToDisk(A a) {
|
---|
104 | OutputStream os = null;
|
---|
105 | ObjectOutputStream oos = null;
|
---|
106 | try {
|
---|
107 | os = new BufferedOutputStream(new FileOutputStream(fileName));
|
---|
108 |
|
---|
109 | oos = new ObjectOutputStream(os);
|
---|
110 | oos.writeObject(a);
|
---|
111 | oos.close();
|
---|
112 | os.close();
|
---|
113 | } catch (IOException ex) {
|
---|
114 | ex.printStackTrace();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Serializes an object to a string encoded by using Base64 to avoid
|
---|
120 | * characterset problems.
|
---|
121 | *
|
---|
122 | * @param a
|
---|
123 | * object to serialize.
|
---|
124 | * @return serialized object.
|
---|
125 | */
|
---|
126 | public String writeToString(A a) {
|
---|
127 | String out = null;
|
---|
128 | if (a != null) {
|
---|
129 | try {
|
---|
130 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
---|
131 | ObjectOutputStream oos = new ObjectOutputStream(baos);
|
---|
132 | oos.writeObject(a);
|
---|
133 | out = DatatypeConverter.printBase64Binary(baos.toByteArray());
|
---|
134 | } catch (IOException e) {
|
---|
135 | e.printStackTrace();
|
---|
136 | return null;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | return out;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Converts a string back to an object.
|
---|
144 | *
|
---|
145 | * @param str
|
---|
146 | * serialized object.
|
---|
147 | * @return unserialized object.
|
---|
148 | */
|
---|
149 | @SuppressWarnings("unchecked")
|
---|
150 | public A readStringToObject(String str) {
|
---|
151 | Object out = null;
|
---|
152 | if (str != null) {
|
---|
153 | try {
|
---|
154 | ByteArrayInputStream bios = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(str));
|
---|
155 | ObjectInputStream ois = new ObjectInputStream(bios);
|
---|
156 | out = ois.readObject();
|
---|
157 | } catch (IOException e) {
|
---|
158 | e.printStackTrace();
|
---|
159 | return null;
|
---|
160 | } catch (ClassNotFoundException e) {
|
---|
161 | e.printStackTrace();
|
---|
162 | return null;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | return (A) out;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * @return filename in which the object should be/is serialized.
|
---|
170 | */
|
---|
171 | public String getFileName() {
|
---|
172 | return fileName;
|
---|
173 | }
|
---|
174 | } |
---|