org.jenetics.util.IO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.jenetics Show documentation
Show all versions of org.jenetics Show documentation
Jenetics - Java Genetic Algorithm Library
/*
* Java Genetic Algorithm Library (jenetics-3.1.0).
* Copyright (c) 2007-2015 Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter ([email protected])
*/
package org.jenetics.util;
import static org.jenetics.internal.util.jaxb.adapterFor;
import static org.jenetics.internal.util.jaxb.context;
import static org.jenetics.internal.util.jaxb.marshal;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
* Class for object serialization. The following example shows how to write and
* reload a given population.
*
* {@code
* // Creating result population.
* EvolutionResult result = stream
* .collect(toBestEvolutionResult());
*
* // Writing the population to disk.
* final File file = new File("population.xml");
* IO.jaxb.write(result.getPopulation(), file);
*
* // Reading the population from disk.
* Population population =
* (Population)IO.jaxb.read(file);
* EvolutionStream stream = Engine
* .build(ff, gtf)
* .stream(population, 1);
* }
*
* @author Franz Wilhelmstötter
* @since 1.0
* @version 2.0
*/
public abstract class IO {
protected IO() {
}
/**
* JAXB for XML serialization.
*/
public static final IO jaxb = new IO() {
@Override
public void write(final Object object, final OutputStream out)
throws IOException
{
try {
final Marshaller marshaller = context().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(marshal(object), out);
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public T read(final Class type, final InputStream in)
throws IOException
{
try {
final Unmarshaller unmarshaller = context().createUnmarshaller();
//final XMLInputFactory factory = XMLInputFactory.newInstance();
//final XMLStreamReader reader = factory.createXMLStreamReader(in);
//try {
final Object object = unmarshaller.unmarshal(in);
final XmlAdapter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy