001 /*
002 * Java Genetic Algorithm Library (jenetics-4.0.0).
003 * Copyright (c) 2007-2017 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@gmail.com)
019 */
020 package io.jenetics.util;
021
022 import java.io.File;
023 import java.io.FileInputStream;
024 import java.io.FileOutputStream;
025 import java.io.IOException;
026 import java.io.InputStream;
027 import java.io.ObjectInputStream;
028 import java.io.ObjectOutputStream;
029 import java.io.OutputStream;
030 import java.nio.file.Path;
031
032 /**
033 * Class for object serialization. The following example shows how to write and
034 * reload a given population.
035 *
036 * <pre>{@code
037 * // Creating result population.
038 * EvolutionResult<DoubleGene, Double> result = stream
039 * .collect(toBestEvolutionResult());
040 *
041 * // Writing the population to disk.
042 * final File file = new File("population.bin");
043 * IO.object.write(result.getPopulation(), file);
044 *
045 * // Reading the population from disk.
046 * ISeq<Phenotype<G, C>> population = (ISeq<Phenotype<G, C>>)IO.object.read(file);
047 * EvolutionStream<DoubleGene, Double> stream = Engine
048 * .build(ff, gtf)
049 * .stream(population, 1);
050 * }</pre>
051 *
052 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
053 * @since 1.0
054 * @version 4.0
055 */
056 public abstract class IO {
057
058 protected IO() {
059 }
060
061 /**
062 * IO implementation for "native" <i>Java</i> serialization.
063 */
064 public static final IO object = new IO() {
065
066 @Override
067 public void write(final Object object, final OutputStream out)
068 throws IOException
069 {
070 final ObjectOutputStream oout = new ObjectOutputStream(out);
071 oout.writeObject(object);
072 out.flush();
073 }
074
075 @Override
076 public <T> T read(final Class<T> type, final InputStream in)
077 throws IOException
078 {
079 final ObjectInputStream oin = new ObjectInputStream(in);
080 try {
081 return type.cast(oin.readObject());
082 } catch (ClassNotFoundException | ClassCastException e) {
083 throw new IOException(e);
084 }
085 }
086 };
087
088
089 /**
090 * Write the (serializable) object to the given path.
091 *
092 * @param object the object to serialize.
093 * @param path the path to write the object to.
094 * @throws NullPointerException if one of the arguments is {@code null}.
095 * @throws IOException if the object could not be serialized.
096 */
097 public void write(final Object object, final String path)
098 throws IOException
099 {
100 write(object, new File(path));
101 }
102
103 /**
104 * Write the (serializable) object to the given path.
105 *
106 * @param object the object to serialize.
107 * @param path the path to write the object to.
108 * @throws NullPointerException if one of the arguments is {@code null}.
109 * @throws IOException if the object could not be serialized.
110 */
111 public void write(final Object object, final Path path)
112 throws IOException
113 {
114 write(object, path.toFile());
115 }
116
117 /**
118 * Write the (serializable) object to the given file.
119 *
120 * @param object the object to serialize.
121 * @param file the file to write the object to.
122 * @throws NullPointerException if one of the arguments is {@code null}.
123 * @throws IOException if the object could not be serialized.
124 */
125 public void write(final Object object, final File file)
126 throws IOException
127 {
128 try (final FileOutputStream out = new FileOutputStream(file)) {
129 write(object, out);
130 }
131 }
132
133 /**
134 * Write the (serializable) object to the given output stream.
135 *
136 * @param object the object to serialize.
137 * @param out the output stream to write the object to.
138 * @throws NullPointerException if one of the arguments is {@code null}.
139 * @throws IOException if the object could not be serialized.
140 */
141 public abstract void write(final Object object, final OutputStream out)
142 throws IOException;
143
144 /**
145 * Reads an object from the given file.
146 *
147 * @param <T> the type of the read object
148 * @param path the path to read from.
149 * @param type the type of the read object.
150 * @return the de-serialized object.
151 * @throws NullPointerException if the input stream {@code in} is {@code null}.
152 * @throws IOException if the object could not be read.
153 */
154 public <T> T read(final Class<T> type, final String path)
155 throws IOException
156 {
157 try (final FileInputStream in = new FileInputStream(new File(path))) {
158 return read(type, in);
159 }
160 }
161
162 /**
163 * Reads an object from the given file.
164 *
165 * @param path the path to read from.
166 * @return the de-serialized object.
167 * @throws NullPointerException if the input stream {@code in} is {@code null}.
168 * @throws IOException if the object could not be read.
169 */
170 public Object read(final String path) throws IOException {
171 return read(Object.class, path);
172 }
173
174 /**
175 * Reads an object from the given file.
176 *
177 * @param <T> the type of the read object
178 * @param path the path to read from.
179 * @param type the type of the read object.
180 * @return the de-serialized object.
181 * @throws NullPointerException if the input stream {@code in} is {@code null}.
182 * @throws IOException if the object could not be read.
183 */
184 public <T> T read(final Class<T> type, final Path path)
185 throws IOException
186 {
187 try (final FileInputStream in = new FileInputStream(path.toFile())) {
188 return read(type, in);
189 }
190 }
191
192 /**
193 * Reads an object from the given file.
194 *
195 * @param path the path to read from.
196 * @return the de-serialized object.
197 * @throws NullPointerException if the input stream {@code in} is {@code null}.
198 * @throws IOException if the object could not be read.
199 */
200 public Object read(final Path path) throws IOException {
201 return read(Object.class, path);
202 }
203
204 /**
205 * Reads an object from the given file.
206 *
207 * @param <T> the type of the read object
208 * @param file the file to read from.
209 * @param type the type of the read object.
210 * @return the de-serialized object.
211 * @throws NullPointerException if the input stream {@code in} is {@code null}.
212 * @throws IOException if the object could not be read.
213 */
214 public <T> T read(final Class<T> type, final File file)
215 throws IOException
216 {
217 try (final FileInputStream in = new FileInputStream(file)) {
218 return read(type, in);
219 }
220 }
221
222 /**
223 * Reads an object from the given file.
224 *
225 * @param file the file to read from.
226 * @return the de-serialized object.
227 * @throws NullPointerException if the input stream {@code in} is {@code null}.
228 * @throws IOException if the object could not be read.
229 */
230 public Object read(final File file) throws IOException {
231 return read(Object.class, file);
232 }
233
234 /**
235 * Reads an object from the given input stream.
236 *
237 * @param <T> the type of the read object
238 * @param in the input stream to read from.
239 * @param type the type of the read object.
240 * @return the de-serialized object.
241 * @throws NullPointerException if the input stream {@code in} is {@code null}.
242 * @throws IOException if the object could not be read.
243 */
244 public abstract <T> T read(final Class<T> type, final InputStream in)
245 throws IOException;
246
247 /**
248 * Reads an object from the given input stream.
249 *
250 * @param in the input stream to read from.
251 * @return the de-serialized object.
252 * @throws NullPointerException if the input stream {@code in} is {@code null}.
253 * @throws IOException if the object could not be read.
254 */
255 public Object read(final InputStream in) throws IOException {
256 return read(Object.class, in);
257 }
258 }
|