Example usage for java.io ObjectOutputStream writeFloat

List of usage examples for java.io ObjectOutputStream writeFloat

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeFloat.

Prototype

public void writeFloat(float val) throws IOException 

Source Link

Document

Writes a 32 bit float.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    float f = 1.23456f;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeFloat(f);
    oout.writeFloat(1234.56789f);// www  .j  a v  a  2s  .c om
    oout.flush();
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    System.out.println(ois.readFloat());

    System.out.println(ois.readFloat());
    ois.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.writeFloat(1.234F);
    out.close();//from   www.  ja  v a 2  s.  c  o  m

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:Main.java

public static void writePath(GeneralPath path, ObjectOutputStream out) throws IOException {
    PathIterator i = path.getPathIterator(null);
    float[] data = new float[6];

    while (!i.isDone()) {
        switch (i.currentSegment(data)) {
        case PathIterator.SEG_MOVETO:
            out.writeInt(PathIterator.SEG_MOVETO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);/*  w  ww .jav  a2  s. c  om*/
            break;

        case PathIterator.SEG_LINETO:
            out.writeInt(PathIterator.SEG_LINETO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            break;

        case PathIterator.SEG_QUADTO:
            out.writeInt(PathIterator.SEG_QUADTO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            out.writeFloat(data[2]);
            out.writeFloat(data[3]);
            break;

        case PathIterator.SEG_CUBICTO:
            out.writeInt(PathIterator.SEG_CUBICTO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            out.writeFloat(data[2]);
            out.writeFloat(data[3]);
            out.writeFloat(data[4]);
            out.writeFloat(data[5]);
            break;

        case PathIterator.SEG_CLOSE:
            out.writeInt(PathIterator.SEG_CLOSE);
            break;

        default:
            throw new IOException();
        }

        i.next();
    }

    out.writeInt(PATH_IS_DONE);
}

From source file:Main.java

/**
 * Serialises a <code>Stroke</code> object.  This code handles the
 * <code>BasicStroke</code> class which is the only <code>Stroke</code>
 * implementation provided by the JDK (and isn't directly
 * <code>Serializable</code>).
 *
 * @param stroke  the stroke object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *///from   w  w w .java  2  s.  c o m
public static void writeStroke(final Stroke stroke, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (stroke != null) {
        stream.writeBoolean(false);
        if (stroke instanceof BasicStroke) {
            final BasicStroke s = (BasicStroke) stroke;
            stream.writeObject(BasicStroke.class);
            stream.writeFloat(s.getLineWidth());
            stream.writeInt(s.getEndCap());
            stream.writeInt(s.getLineJoin());
            stream.writeFloat(s.getMiterLimit());
            stream.writeObject(s.getDashArray());
            stream.writeFloat(s.getDashPhase());
        } else {
            stream.writeObject(stroke.getClass());
            stream.writeObject(stroke);
        }
    } else {
        stream.writeBoolean(true);
    }
}

From source file:Main.java

/**
 * Serialises a <code>Paint</code> object.
 *
 * @param paint  the paint object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*from w  ww .  ja  va 2  s .c  o  m*/
public static void writePaint(final Paint paint, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (paint != null) {
        stream.writeBoolean(false);
        stream.writeObject(paint.getClass());
        if (paint instanceof Serializable) {
            stream.writeObject(paint);
        } else if (paint instanceof GradientPaint) {
            final GradientPaint gp = (GradientPaint) paint;
            stream.writeFloat((float) gp.getPoint1().getX());
            stream.writeFloat((float) gp.getPoint1().getY());
            stream.writeObject(gp.getColor1());
            stream.writeFloat((float) gp.getPoint2().getX());
            stream.writeFloat((float) gp.getPoint2().getY());
            stream.writeObject(gp.getColor2());
            stream.writeBoolean(gp.isCyclic());
        }
    } else {
        stream.writeBoolean(true);
    }

}

From source file:Main.java

public static void writeStroke(Stroke aStroke, ObjectOutputStream out) throws IOException {
    if (aStroke instanceof Serializable) {
        out.writeBoolean(true);//from  ww  w .  ja v a2  s .  c  o  m
        out.writeBoolean(true);
        out.writeObject(aStroke);
    } else if (aStroke instanceof BasicStroke) {
        out.writeBoolean(true);
        out.writeBoolean(false);
        BasicStroke s = (BasicStroke) aStroke;

        float[] dash = s.getDashArray();

        if (dash == null) {
            out.write(0);
        } else {
            out.write(dash.length);
            for (int i = 0; i < dash.length; i++) {
                out.writeFloat(dash[i]);
            }
        }

        out.writeFloat(s.getLineWidth());
        out.writeInt(s.getEndCap());
        out.writeInt(s.getLineJoin());
        out.writeFloat(s.getMiterLimit());
        out.writeFloat(s.getDashPhase());
    } else {
        out.writeBoolean(false);
    }
}

From source file:FloatArrayList.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();// w ww .  j  av  a  2  s  .com
    out.writeInt(array.length);
    for (int i = 0; i < size; i++) {
        out.writeFloat(array[i]);
    }
}

From source file:Main.java

/**
 * 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.
 *///from w w w  .j a  v a  2 s .  c o  m
public static void writeShape(final Shape shape, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (shape != null) {
        stream.writeBoolean(false);
        if (shape instanceof Line2D) {
            final 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) {
            final 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) {
            final 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) {
            final 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);
            final PathIterator pi = shape.getPathIterator(null);
            final float[] args = new float[6];
            stream.writeBoolean(pi.isDone());
            while (!pi.isDone()) {
                final 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);
    }
}

From source file:org.dishevelled.multimap.impl.AbstractHashedMap.java

/**
 * Writes the map data to the stream. This method must be overridden if a
 * subclass must be setup before <code>put()</code> is used.
 *
 * Serialization is not one of the JDK's nicest topics. Normal serialization will
 * initialise the superclass before the subclass. Sometimes however, this isn't
 * what you want, as in this case the <code>put()</code> method on read can be
 * affected by subclass state./*from  w w w  .  ja  v  a2  s  .c  o  m*/
 *
 * The solution adopted here is to serialize the state data of this class in
 * this protected method. This method must be called by the
 * <code>writeObject()</code> of the first serializable subclass.
 *
 * Subclasses may override if they have a specific field that must be present
 * on read before this implementation will work. Generally, the read determines
 * what must be serialized here, if anything.
 *
 * @param out the output stream
 * @throws IOException if an I/O error occurs
 */
protected void doWriteObject(ObjectOutputStream out) throws IOException {
    out.writeFloat(loadFactor);
    out.writeInt(data.length);
    out.writeInt(size);
    for (Iterator<Map.Entry<K, V>> it = createEntrySetIterator(); it.hasNext();) {
        Map.Entry entry = it.next();
        out.writeObject(entry.getKey());
        out.writeObject(entry.getValue());
    }
}

From source file:org.globus.examples.services.filebuy.seller.impl.FileResource.java

public synchronized void store() throws ResourceException {
    /* We will use these two variables to write the resource to disk */
    FileOutputStream fos = null;/*from w  w w .  java2  s .co m*/
    File tmpFile = null;

    logger.info("Attempting to store resource " + this.getID());
    try {
        /* We start by creating a temporary file */
        tmpFile = File.createTempFile("math", ".tmp", getPersistenceHelper().getStorageDirectory());
        /* We open the file for writing */
        fos = new FileOutputStream(tmpFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        /* We write the RPs in the file */
        oos.writeFloat(this.price);
        oos.writeUTF(this.name);
        oos.writeUTF(this.location);
        oos.flush();
        logger.info("Successfully stored resource with Name=" + name);
    } catch (Exception e) {
        /* Delete the temporary file if something goes wrong */
        tmpFile.delete();
        throw new ResourceException("Failed to store resource", e);
    } finally {
        /* Clean up */
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception ee) {
            }
        }
    }

    /*
     * We have successfully created a temporary file with our resource's
     * RPs. Now, if there is a previous copy of our resource on disk, we
     * first have to delete it. Next, we rename the temporary file to the
     * file representing our resource.
     */
    File file = getKeyAsFile(this.key);
    if (file.exists()) {
        file.delete();
    }
    if (!tmpFile.renameTo(file)) {
        tmpFile.delete();
        throw new ResourceException("Failed to store resource");
    }
}