Class IO

java.lang.Object
io.jenetics.util.IO

public abstract class IO extends Object
Class for object serialization. The following example shows how to write and reload a given population.

// Creating result population.
EvolutionResult<DoubleGene, Double> result = stream
    .collect(toBestEvolutionResult());

// Writing the population to disk.
final File file = new File("population.bin");
IO.object.write(result.getPopulation(), file);

// Reading the population from disk.
ISeq<Phenotype<G, C>> population = (ISeq<Phenotype<G, C>>)IO.object.read(file);
EvolutionStream<DoubleGene, Double> stream = Engine
    .build(ff, gtf)
    .stream(population, 1);
Since:
1.0
Version:
4.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final IO
    IO implementation for "native" Java serialization.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    IO()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    fromByteArray(byte[] bytes)
    Creates a, previously serialized, object from the given byte[] array.
    read(File file)
    Reads an object from the given file.
    Reads an object from the given input stream.
    <T> T
    read(Class<T> type, File file)
    Reads an object from the given file.
    abstract <T> T
    read(Class<T> type, InputStream in)
    Reads an object from the given input stream.
    <T> T
    read(Class<T> type, String path)
    Reads an object from the given file.
    <T> T
    read(Class<T> type, Path path)
    Reads an object from the given file.
    read(String path)
    Reads an object from the given file.
    read(Path path)
    Reads an object from the given file.
    byte[]
    Serializes the given object to a byte[] array.
    void
    write(Object object, File file)
    Write the (serializable) object to the given file.
    abstract void
    write(Object object, OutputStream out)
    Write the (serializable) object to the given output stream.
    void
    write(Object object, String path)
    Write the (serializable) object to the given path.
    void
    write(Object object, Path path)
    Write the (serializable) object to the given path.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • object

      public static final IO object
      IO implementation for "native" Java serialization.
  • Constructor Details

    • IO

      protected IO()
  • Method Details

    • toByteArray

      public byte[] toByteArray(Object object) throws IOException
      Serializes the given object to a byte[] array.
      Parameters:
      object - the object to serialize.
      Returns:
      the serialized object as byte[] array
      Throws:
      NullPointerException - if one of the objects is null.
      IOException - if the object could not be serialized.
      Since:
      4.1
    • write

      public void write(Object object, String path) throws IOException
      Write the (serializable) object to the given path.
      Parameters:
      object - the object to serialize.
      path - the path to write the object to.
      Throws:
      NullPointerException - if one of the arguments is null.
      IOException - if the object could not be serialized.
    • write

      public void write(Object object, Path path) throws IOException
      Write the (serializable) object to the given path.
      Parameters:
      object - the object to serialize.
      path - the path to write the object to.
      Throws:
      NullPointerException - if one of the arguments is null.
      IOException - if the object could not be serialized.
    • write

      public void write(Object object, File file) throws IOException
      Write the (serializable) object to the given file.
      Parameters:
      object - the object to serialize.
      file - the file to write the object to.
      Throws:
      NullPointerException - if one of the arguments is null.
      IOException - if the object could not be serialized.
    • write

      public abstract void write(Object object, OutputStream out) throws IOException
      Write the (serializable) object to the given output stream.
      Parameters:
      object - the object to serialize.
      out - the output stream to write the object to.
      Throws:
      NullPointerException - if one of the arguments is null.
      IOException - if the object could not be serialized.
    • fromByteArray

      public Object fromByteArray(byte[] bytes) throws IOException
      Creates a, previously serialized, object from the given byte[] array.
      Parameters:
      bytes - the serialized object.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input bytes is null.
      IOException - if the object could not be deserialized.
      Since:
      4.1
    • read

      public <T> T read(Class<T> type, String path) throws IOException
      Reads an object from the given file.
      Type Parameters:
      T - the type of the read object
      Parameters:
      type - the type of the read object.
      path - the path to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public Object read(String path) throws IOException
      Reads an object from the given file.
      Parameters:
      path - the path to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public <T> T read(Class<T> type, Path path) throws IOException
      Reads an object from the given file.
      Type Parameters:
      T - the type of the read object
      Parameters:
      type - the type of the read object.
      path - the path to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public Object read(Path path) throws IOException
      Reads an object from the given file.
      Parameters:
      path - the path to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public <T> T read(Class<T> type, File file) throws IOException
      Reads an object from the given file.
      Type Parameters:
      T - the type of the read object
      Parameters:
      type - the type of the read object.
      file - the file to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public Object read(File file) throws IOException
      Reads an object from the given file.
      Parameters:
      file - the file to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public abstract <T> T read(Class<T> type, InputStream in) throws IOException
      Reads an object from the given input stream.
      Type Parameters:
      T - the type of the read object
      Parameters:
      type - the type of the read object.
      in - the input stream to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.
    • read

      public Object read(InputStream in) throws IOException
      Reads an object from the given input stream.
      Parameters:
      in - the input stream to read from.
      Returns:
      the deserialized object.
      Throws:
      NullPointerException - if the input stream in is null.
      IOException - if the object could not be read.