IO.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-2.0.2).
003  * Copyright (c) 2007-2014 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.util;
021 
022 import static org.jenetics.internal.util.jaxb.context;
023 import static org.jenetics.internal.util.jaxb.adapterFor;
024 import static org.jenetics.internal.util.jaxb.marshal;
025 
026 import java.io.File;
027 import java.io.FileInputStream;
028 import java.io.FileOutputStream;
029 import java.io.IOException;
030 import java.io.InputStream;
031 import java.io.ObjectInputStream;
032 import java.io.ObjectOutputStream;
033 import java.io.OutputStream;
034 import java.nio.file.Path;
035 
036 import javax.xml.bind.Marshaller;
037 import javax.xml.bind.Unmarshaller;
038 import javax.xml.bind.annotation.adapters.XmlAdapter;
039 
040 
041 /**
042  * Class for object serialization. The following example shows how to write and
043  * reload a given population.
044  *
045  * [code]
046  * // Writing the population to disk.
047  * final File file = new File("population.xml");
048  * IO.jaxb.write(ga.getPopulation(), file);
049  *
050  * // Reading the population from disk.
051  * final Population<DoubleGene,Double> population =
052  *     (Population<DoubleGene, Double>)IO.jaxb.read(file);
053  * ga.setPopulation(population);
054  * [/code]
055  *
056  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
057  @since 1.0
058  @version 2.0 &mdash; <em>$Date: 2014-04-12 $</em>
059  */
060 public abstract class IO {
061 
062     protected IO() {
063     }
064 
065     /**
066      * JAXB for <i>XML</i> serialization.
067      */
068     public static final IO jaxb = new IO() {
069 
070         @Override
071         public void write(final Object object, final OutputStream out)
072             throws IOException
073         {
074             try {
075                 final Marshaller marshaller = context().createMarshaller();
076                 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
077                 marshaller.marshal(marshal(object), out);
078             catch (Exception e) {
079                 throw new IOException(e);
080             }
081         }
082 
083         @Override
084         public <T> T read(final Class<T> type, final InputStream in)
085             throws IOException
086         {
087             try {
088                 final Unmarshaller unmarshaller = context().createUnmarshaller();
089 
090                 //final XMLInputFactory factory = XMLInputFactory.newInstance();
091                 //final XMLStreamReader reader = factory.createXMLStreamReader(in);
092                 //try {
093                     final Object object = unmarshaller.unmarshal(in);
094                     final XmlAdapter<Object, Object> adapter = adapterFor(object);
095                     if (adapter != null) {
096                         return type.cast(adapter.unmarshal(object));
097                     else {
098                         return type.cast(object);
099                     }
100                 //} finally {
101                 //    reader.close();
102                 //}
103             catch (Exception e) {
104                 throw new IOException(e);
105             }
106         }
107     };
108 
109     /**
110      * IO implementation for "native" <i>Java</i> serialization.
111      */
112     public static final IO object = new IO() {
113 
114         @Override
115         public void write(final Object object, final OutputStream out)
116             throws IOException
117         {
118             final ObjectOutputStream oout = new ObjectOutputStream(out);
119             oout.writeObject(object);
120             out.flush();
121         }
122 
123         @Override
124         public <T> T read(final Class<T> type, final InputStream in)
125             throws IOException
126         {
127             final ObjectInputStream oin = new ObjectInputStream(in);
128             try {
129                 return type.cast(oin.readObject());
130             catch (ClassNotFoundException | ClassCastException e) {
131                 throw new IOException(e);
132             }
133         }
134     };
135 
136 
137     /**
138      * Write the (serializable) object to the given path.
139      *
140      @param object the object to serialize.
141      @param path the path to write the object to.
142      @throws NullPointerException if one of the arguments is {@code null}.
143      @throws IOException if the object could not be serialized.
144      */
145     public void write(final Object object, final String path)
146         throws IOException
147     {
148         write(object, new File(path));
149     }
150 
151     /**
152      * Write the (serializable) object to the given path.
153      *
154      @param object the object to serialize.
155      @param path the path to write the object to.
156      @throws NullPointerException if one of the arguments is {@code null}.
157      @throws IOException if the object could not be serialized.
158      */
159     public void write(final Object object, final Path path)
160         throws IOException
161     {
162         write(object, path.toFile());
163     }
164 
165     /**
166      * Write the (serializable) object to the given file.
167      *
168      @param object the object to serialize.
169      @param file the file to write the object to.
170      @throws NullPointerException if one of the arguments is {@code null}.
171      @throws IOException if the object could not be serialized.
172      */
173     public void write(final Object object, final File file)
174         throws IOException
175     {
176         try (final FileOutputStream out = new FileOutputStream(file)) {
177             write(object, out);
178         }
179     }
180 
181     /**
182      * Write the (serializable) object to the given output stream.
183      *
184      @param object the object to serialize.
185      @param out the output stream to write the object to.
186      @throws NullPointerException if one of the arguments is {@code null}.
187      @throws IOException if the object could not be serialized.
188      */
189     public abstract void write(final Object object, final OutputStream out)
190         throws IOException;
191 
192     /**
193      * Reads an object from the given file.
194      *
195      @param <T> the type of the read object
196      @param path the path to read from.
197      @param type the type of the read object.
198      @return the de-serialized object.
199      @throws NullPointerException if the input stream {@code in} is {@code null}.
200      @throws IOException if the object could not be read.
201      */
202     public <T> T read(final Class<T> type, final String path)
203         throws IOException
204     {
205         try (final FileInputStream in = new FileInputStream(new File(path))) {
206             return read(type, in);
207         }
208     }
209 
210     /**
211      * Reads an object from the given file.
212      *
213      @param path the path to read from.
214      @return the de-serialized object.
215      @throws NullPointerException if the input stream {@code in} is {@code null}.
216      @throws IOException if the object could not be read.
217      */
218     public Object read(final String paththrows IOException {
219         return read(Object.class, path);
220     }
221 
222     /**
223      * Reads an object from the given file.
224      *
225      @param <T> the type of the read object
226      @param path the path to read from.
227      @param type the type of the read object.
228      @return the de-serialized object.
229      @throws NullPointerException if the input stream {@code in} is {@code null}.
230      @throws IOException if the object could not be read.
231      */
232     public <T> T read(final Class<T> type, final Path path)
233         throws IOException
234     {
235         try (final FileInputStream in = new FileInputStream(path.toFile())) {
236             return read(type, in);
237         }
238     }
239 
240     /**
241      * Reads an object from the given file.
242      *
243      @param path the path to read from.
244      @return the de-serialized object.
245      @throws NullPointerException if the input stream {@code in} is {@code null}.
246      @throws IOException if the object could not be read.
247      */
248     public Object read(final Path paththrows IOException {
249         return read(Object.class, path);
250     }
251 
252     /**
253      * Reads an object from the given file.
254      *
255      @param <T> the type of the read object
256      @param file the file to read from.
257      @param type the type of the read object.
258      @return the de-serialized object.
259      @throws NullPointerException if the input stream {@code in} is {@code null}.
260      @throws IOException if the object could not be read.
261      */
262     public <T> T read(final Class<T> type, final File file)
263         throws IOException
264     {
265         try (final FileInputStream in = new FileInputStream(file)) {
266             return read(type, in);
267         }
268     }
269 
270     /**
271      * Reads an object from the given file.
272      *
273      @param file the file to read from.
274      @return the de-serialized object.
275      @throws NullPointerException if the input stream {@code in} is {@code null}.
276      @throws IOException if the object could not be read.
277      */
278     public Object read(final File filethrows IOException {
279         return read(Object.class, file);
280     }
281 
282     /**
283      * Reads an object from the given input stream.
284      *
285      @param <T> the type of the read object
286      @param in the input stream to read from.
287      @param type the type of the read object.
288      @return the de-serialized object.
289      @throws NullPointerException if the input stream {@code in} is {@code null}.
290      @throws IOException if the object could not be read.
291      */
292     public abstract <T> T read(final Class<T> type, final InputStream in)
293         throws IOException;
294 
295     /**
296      * Reads an object from the given input stream.
297      *
298      @param in the input stream to read from.
299      @return the de-serialized object.
300      @throws NullPointerException if the input stream {@code in} is {@code null}.
301      @throws IOException if the object could not be read.
302      */
303     public Object read(final InputStream inthrows IOException {
304         return read(Object.class, in);
305     }
306 }