下面列出了java.io.ObjectOutputStream#writeDouble ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
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);
}
/**
* 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() );
}
/**
* 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();
}
/**
* 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() );
}
@Override
public final void write(ObjectOutputStream os, int[] indexes) throws IOException {
for (int index : indexes) {
os.writeDouble(values[index]);
}
}
/**
* 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);
}
}
/** 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));
}
}
}
/** 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));
}
}
}
/** 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));
}
}
}
/** 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));
}
}
/** 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));
}
}
}
/** 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));
}
}
}
/** 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));
}
}
}
/**
* 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() );
}
/** 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));
}
}
}
/** 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));
}
}
}
/** 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));
}
}