View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.misc;
17  
18  import java.beans.XMLDecoder;
19  import java.beans.XMLEncoder;
20  import java.io.BufferedInputStream;
21  import java.io.BufferedOutputStream;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.io.OutputStream;
29  import java.util.zip.GZIPInputStream;
30  import java.util.zip.GZIPOutputStream;
31  
32  /**
33   * 
34   * Class that stores and loads serializable classes. Needed to store and load
35   * parameter files.
36   * 
37   * Created on February 12, 2007
38   * 
39   * @author Christina Bohk
40   * @author Roland Ewald
41   * 
42   */
43  public class Serializer {
44  
45  	/**
46  	 * Flag that indicates whether objects should be stored in XML or in binary
47  	 * encoding.
48  	 */
49  	private boolean usingXML;
50  
51  	/**
52  	 * Flag that indicates whether GZIP compression (LZW) is used.
53  	 */
54  	private boolean useCompression = true;
55  
56  	/**
57  	 * Loads object from file.
58  	 * 
59  	 * @param file
60  	 *          path to file with the object to be loaded
61  	 * @return object the object that has been loaded
62  	 * @throws IOException
63  	 *           if file was not found, file input failed, etc.
64  	 * @throws ClassNotFoundException
65  	 *           if class of persistent object could not be found
66  	 */
67  	public Object load(String file) throws IOException, ClassNotFoundException {
68  		if (usingXML) {
69  			return loadFromXML(file);
70  		}
71  		return loadFromBinary(file);
72  	}
73  
74  	/**
75  	 * Load object from a binary file.
76  	 * 
77  	 * @param file
78  	 *          path and file name
79  	 * @return deserialised object
80  	 * @throws IOException
81  	 *           if file was not found, etc.
82  	 * @throws ClassNotFoundException
83  	 *           if class of persistent object could not be found
84  	 */
85  	public Object loadFromBinary(String file) throws IOException,
86  	    ClassNotFoundException {
87  		ObjectInputStream input = new ObjectInputStream(getInputStream(file));
88  		Object o = input.readObject();
89  		input.close();
90  		return o;
91  	}
92  
93  	/**
94  	 * Load object from XML file.
95  	 * 
96  	 * @param file
97  	 *          path and file name
98  	 * @return deserialised object
99  	 * @throws IOException
100 	 *           if file was not found, a read error occurred, etc.
101 	 */
102 	public Object loadFromXML(String file) throws IOException {
103 		XMLDecoder xmlDecoder = new XMLDecoder(getInputStream(file));
104 		Object object = xmlDecoder.readObject();
105 		xmlDecoder.close();
106 		return object;
107 	}
108 
109 	/**
110 	 * Create an input stream.
111 	 * 
112 	 * @param file
113 	 *          source file
114 	 * @return input stream from file
115 	 * @throws IOException
116 	 *           if stream creation fails
117 	 */
118 	protected InputStream getInputStream(String file) throws IOException {
119 		InputStream in = new BufferedInputStream(new FileInputStream(file));
120 		if (useCompression) {
121 			in = new GZIPInputStream(in);
122 		}
123 		return in;
124 	}
125 
126 	/**
127 	 * Create an output stream.
128 	 * 
129 	 * @param file
130 	 *          target file
131 	 * @return output stream to file
132 	 * @throws IOException
133 	 *           if stream creation fails
134 	 */
135 	protected OutputStream getOutputStream(String file) throws IOException {
136 		OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
137 		if (useCompression) {
138 			out = new GZIPOutputStream(out);
139 		}
140 		return out;
141 	}
142 
143 	/**
144 	 * Save object to file.
145 	 * 
146 	 * @param object
147 	 *          the object to be saved in the file
148 	 * @param file
149 	 *          the file
150 	 * @throws IOException
151 	 *           if outputting went wrong
152 	 */
153 	public void save(Object object, String file) throws IOException {
154 		if (usingXML) {
155 			saveToXML(object, file);
156 		} else {
157 			saveToBinary(object, file);
158 		}
159 	}
160 
161 	/**
162 	 * Save object to binary file.
163 	 * 
164 	 * @param object
165 	 *          the object to be written
166 	 * @param file
167 	 *          the target file
168 	 * @throws IOException
169 	 *           if writing fails
170 	 */
171 	public void saveToBinary(Object object, String file) throws IOException {
172 		ObjectOutputStream output = new ObjectOutputStream(getOutputStream(file));
173 		output.writeObject(object);
174 		output.close();
175 	}
176 
177 	/**
178 	 * Save object to XML file.
179 	 * 
180 	 * @param object
181 	 *          the object to be written
182 	 * @param file
183 	 *          the target file
184 	 * @throws IOException
185 	 *           if writing fails
186 	 */
187 	public void saveToXML(Object object, String file) throws IOException {
188 		XMLEncoder xmlEncoder = new XMLEncoder(getOutputStream(file));
189 		xmlEncoder.writeObject(object);
190 		xmlEncoder.close();
191 	}
192 
193 	public boolean isUsingXML() {
194 		return usingXML;
195 	}
196 
197 	public void setUsingXML(boolean usingXML) {
198 		this.usingXML = usingXML;
199 	}
200 
201 }