Java Code Examples for java.io.ObjectOutputStream#writeDouble()

The following examples show how to use java.io.ObjectOutputStream#writeDouble() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jdk8u-jdk   File: Doubles.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run benchmark for given number of batches, with given number of cycles
 * for each batch.
 */
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
            StreamBuffer sbuf, int nbatches, int ncycles)
    throws Exception
{
    for (int i = 0; i < nbatches; i++) {
        sbuf.reset();
        for (int j = 0; j < ncycles; j++) {
            oout.writeDouble(0.0);
        }
        oout.flush();
        for (int j = 0; j < ncycles; j++) {
            oin.readDouble();
        }
    }
}
 
Example 2
/**
 * Run benchmark for given number of batches, with given number of cycles
 * for each batch.
 */
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
            StreamBuffer sbuf, int nbatches, int ncycles)
    throws Exception
{
    for (int i = 0; i < nbatches; i++) {
        sbuf.reset();
        for (int j = 0; j < ncycles; j++) {
            oout.writeDouble(0.0);
        }
        oout.flush();
        for (int j = 0; j < ncycles; j++) {
            oin.readDouble();
        }
    }
}
 
Example 3
/**
 * Run benchmark for given number of batches, with given number of cycles
 * for each batch.
 */
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
            StreamBuffer sbuf, int nbatches, int ncycles)
    throws Exception
{
    for (int i = 0; i < nbatches; i++) {
        sbuf.reset();
        for (int j = 0; j < ncycles; j++) {
            oout.writeDouble(0.0);
        }
        oout.flush();
        for (int j = 0; j < ncycles; j++) {
            oin.readDouble();
        }
    }
}
 
Example 4
private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeBoolean(z);
    out.writeByte(b);
    out.writeChar(c);
    out.writeShort(s);
    out.writeInt(i);
    out.writeFloat(f);
    out.writeLong(j);
    out.writeDouble(d);
    out.writeObject(str);
    out.writeObject(parent);
    out.writeObject(left);
    out.writeObject(right);
}
 
Example 5
/**
 * Writes a serializable object description to the given object output stream.
 *
 * @param o      the to be serialized object.
 * @param stream the outputstream that should receive the object.
 * @throws IOException if an I/O error occured.
 */
public void writeObject( final Object o, final ObjectOutputStream stream ) throws IOException {
  final Arc2D arc = (Arc2D) o;
  stream.writeDouble( arc.getX() );
  stream.writeDouble( arc.getY() );
  stream.writeDouble( arc.getWidth() );
  stream.writeDouble( arc.getHeight() );
  stream.writeDouble( arc.getAngleStart() );
  stream.writeDouble( arc.getAngleExtent() );
  stream.writeInt( arc.getArcType() );
}
 
Example 6
Source Project: sgdtk   File: CRFModel.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Save the weight vector, etc to a stream
 *
 * @param outputStream Stream to save to
 * @throws IOException
 */
@Override
public void save(OutputStream outputStream) throws IOException
{
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
    objectOutputStream.writeDouble(wscale);
    objectOutputStream.writeLong((long)numLabels);
    objectOutputStream.writeLong((long)weights.length);
    for (int i = 0; i < weights.length; ++i)
    {
        objectOutputStream.writeDouble(weights[i]);
    }
    objectOutputStream.close();
}
 
Example 7
/**
 * Writes a serializable object description to the given object output stream.
 *
 * @param o   the to be serialized object.
 * @param out the outputstream that should receive the object.
 * @throws IOException if an I/O error occured.
 */
public void writeObject( final Object o, final ObjectOutputStream out )
  throws IOException {
  final Line2D line = (Line2D) o;
  out.writeDouble( line.getX1() );
  out.writeDouble( line.getY1() );
  out.writeDouble( line.getX2() );
  out.writeDouble( line.getY2() );
}
 
Example 8
@Override
public final void write(ObjectOutputStream os, int[] indexes) throws IOException {
    for (int index : indexes) {
        os.writeDouble(values[index]);
    }
}
 
Example 9
/**
 * Serialises a <code>Shape</code> object.
 *
 * @param shape  the shape object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 */
public static void writeShape(Shape shape, ObjectOutputStream stream)
        throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (shape != null) {
        stream.writeBoolean(false);
        if (shape instanceof Line2D) {
            Line2D line = (Line2D) shape;
            stream.writeObject(Line2D.class);
            stream.writeDouble(line.getX1());
            stream.writeDouble(line.getY1());
            stream.writeDouble(line.getX2());
            stream.writeDouble(line.getY2());
        }
        else if (shape instanceof Rectangle2D) {
            Rectangle2D rectangle = (Rectangle2D) shape;
            stream.writeObject(Rectangle2D.class);
            stream.writeDouble(rectangle.getX());
            stream.writeDouble(rectangle.getY());
            stream.writeDouble(rectangle.getWidth());
            stream.writeDouble(rectangle.getHeight());
        }
        else if (shape instanceof Ellipse2D) {
            Ellipse2D ellipse = (Ellipse2D) shape;
            stream.writeObject(Ellipse2D.class);
            stream.writeDouble(ellipse.getX());
            stream.writeDouble(ellipse.getY());
            stream.writeDouble(ellipse.getWidth());
            stream.writeDouble(ellipse.getHeight());
        }
        else if (shape instanceof Arc2D) {
            Arc2D arc = (Arc2D) shape;
            stream.writeObject(Arc2D.class);
            stream.writeDouble(arc.getX());
            stream.writeDouble(arc.getY());
            stream.writeDouble(arc.getWidth());
            stream.writeDouble(arc.getHeight());
            stream.writeDouble(arc.getAngleStart());
            stream.writeDouble(arc.getAngleExtent());
            stream.writeInt(arc.getArcType());
        }
        else if (shape instanceof GeneralPath) {
            stream.writeObject(GeneralPath.class);
            PathIterator pi = shape.getPathIterator(null);
            float[] args = new float[6];
            stream.writeBoolean(pi.isDone());
            while (!pi.isDone()) {
                int type = pi.currentSegment(args);
                stream.writeInt(type);
                // TODO: could write this to only stream the values
                // required for the segment type
                for (int i = 0; i < 6; i++) {
                    stream.writeFloat(args[i]);
                }
                stream.writeInt(pi.getWindingRule());
                pi.next();
                stream.writeBoolean(pi.isDone());
            }
        }
        else {
            stream.writeObject(shape.getClass());
            stream.writeObject(shape);
        }
    }
    else {
        stream.writeBoolean(true);
    }
}
 
Example 10
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 11
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 12
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 13
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealVector}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealVector} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the vector (the {@link
 * RealVector} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real vector
 * should be written:
 * <pre><code>
 * public class NamedVector implements Serializable {
 *
 *     private final String name;
 *     private final transient RealVector coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealVector(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 * 
 * @param vector real vector to serialize
 * @param oos stream where the real vector should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealVector(Object, String, ObjectInputStream)
 */
public static void serializeRealVector(final RealVector vector,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = vector.getDimension();
    oos.writeInt(n);
    for (int i = 0; i < n; ++i) {
        oos.writeDouble(vector.getEntry(i));
    }
}
 
Example 14
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 15
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 16
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 17
/**
 * Writes a serializable object description to the given object output stream. This method writes the width and the
 * height of the dimension into the stream.
 *
 * @param o   the to be serialized object.
 * @param out the outputstream that should receive the object.
 * @throws IOException if an I/O error occured.
 */
public void writeObject( final Object o, final ObjectOutputStream out )
  throws IOException {
  final Dimension2D dim = (Dimension2D) o;
  out.writeDouble( dim.getWidth() );
  out.writeDouble( dim.getHeight() );
}
 
Example 18
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 * 
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 19
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealMatrix}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealMatrix} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the matrix (the {@link
 * RealMatrix} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real matrix
 * should be written:
 * <pre><code>
 * public class NamedMatrix implements Serializable {
 *
 *     private final String name;
 *     private final transient RealMatrix coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealMatrix(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param matrix real matrix to serialize
 * @param oos stream where the real matrix should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealMatrix(Object, String, ObjectInputStream)
 */
public static void serializeRealMatrix(final RealMatrix matrix,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = matrix.getRowDimension();
    final int m = matrix.getColumnDimension();
    oos.writeInt(n);
    oos.writeInt(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            oos.writeDouble(matrix.getEntry(i, j));
        }
    }
}
 
Example 20
Source Project: astor   File: MatrixUtils.java    License: GNU General Public License v2.0 3 votes vote down vote up
/** Serialize a {@link RealVector}.
 * <p>
 * This method is intended to be called from within a private
 * <code>writeObject</code> method (after a call to
 * <code>oos.defaultWriteObject()</code>) in a class that has a
 * {@link RealVector} field, which should be declared <code>transient</code>.
 * This way, the default handling does not serialize the vector (the {@link
 * RealVector} interface is not serializable by default) but this method does
 * serialize it specifically.
 * </p>
 * <p>
 * The following example shows how a simple class with a name and a real vector
 * should be written:
 * <pre><code>
 * public class NamedVector implements Serializable {
 *
 *     private final String name;
 *     private final transient RealVector coefficients;
 *
 *     // omitted constructors, getters ...
 *
 *     private void writeObject(ObjectOutputStream oos) throws IOException {
 *         oos.defaultWriteObject();  // takes care of name field
 *         MatrixUtils.serializeRealVector(coefficients, oos);
 *     }
 *
 *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
 *         ois.defaultReadObject();  // takes care of name field
 *         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
 *     }
 *
 * }
 * </code></pre>
 * </p>
 *
 * @param vector real vector to serialize
 * @param oos stream where the real vector should be written
 * @exception IOException if object cannot be written to stream
 * @see #deserializeRealVector(Object, String, ObjectInputStream)
 */
public static void serializeRealVector(final RealVector vector,
                                       final ObjectOutputStream oos)
    throws IOException {
    final int n = vector.getDimension();
    oos.writeInt(n);
    for (int i = 0; i < n; ++i) {
        oos.writeDouble(vector.getEntry(i));
    }
}