Example usage for java.io DataOutputStream writeFloat

List of usage examples for java.io DataOutputStream writeFloat

Introduction

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

Prototype

public final void writeFloat(float v) throws IOException 

Source Link

Document

Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.

Usage

From source file:com.kitware.tangoproject.paraviewtangorecorder.PointCloudActivity.java

private void writePoseToFile(int numPoints) {

    File mainDir = new File(mMainDirPath);
    if (!mainDir.exists()) {
        boolean created = mainDir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mMainDirPath + "\" created\n");
        }/*from w w  w.j  a v a  2s.  c o m*/
    }

    File dir = new File(mSaveDirAbsPath);
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mSaveDirAbsPath + "\" created\n");
        }
    }
    String poseFileName = "pc_" + mNowTimeString + "_poses.vtk";
    mFilenameBuffer.add(mSaveDirAbsPath + poseFileName);
    File file = new File(dir, poseFileName);

    try {
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

        out.write(("# vtk DataFile Version 3.0\n" + "vtk output\n" + "BINARY\n" + "DATASET POLYDATA\n"
                + "POINTS " + numPoints + " float\n").getBytes());

        for (int i = 0; i < numPoints; i++) {
            out.writeFloat(mPosePositionBuffer.get(i)[0]);
            out.writeFloat(mPosePositionBuffer.get(i)[1]);
            out.writeFloat(mPosePositionBuffer.get(i)[2]);
        }

        out.write(("\nLINES 1 " + String.valueOf(numPoints + 1) + "\n").getBytes());
        out.writeInt(numPoints);
        for (int i = 0; i < numPoints; i++) {
            out.writeInt(i);
        }

        out.write(("\nPOINT_DATA " + String.valueOf(numPoints) + "\n" + "FIELD FieldData 2\n" + "orientation 4 "
                + String.valueOf(numPoints) + " float\n").getBytes());

        for (int i = 0; i < numPoints; i++) {
            out.writeFloat(mPoseOrientationBuffer.get(i)[0]);
            out.writeFloat(mPoseOrientationBuffer.get(i)[1]);
            out.writeFloat(mPoseOrientationBuffer.get(i)[2]);
            out.writeFloat(mPoseOrientationBuffer.get(i)[3]);
        }

        out.write(("\ntimestamp 1 " + String.valueOf(numPoints) + " float\n").getBytes());
        for (int i = 0; i < numPoints; i++) {
            out.writeFloat(mPoseTimestampBuffer.get(i));
        }

        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.kitware.tangoproject.paraviewtangorecorder.PointCloudActivity.java

private void writePointCloudToFile(TangoXyzIjData xyzIj, byte[] buffer,
        ArrayList<TangoCoordinateFramePair> framePairs) {

    ByteBuffer myBuffer = ByteBuffer.allocate(xyzIj.xyzCount * 3 * 4);
    myBuffer.order(ByteOrder.LITTLE_ENDIAN);
    myBuffer.put(buffer, xyzIj.xyzParcelFileDescriptorOffset, myBuffer.capacity());

    File mainDir = new File(mMainDirPath);
    if (!mainDir.exists()) {
        boolean created = mainDir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mMainDirPath + "\" created\n");
        }//from w  ww .ja v a 2 s. c  om
    }

    File dir = new File(mSaveDirAbsPath);
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mSaveDirAbsPath + "\" created\n");
        }
    }

    mFilename = "pc_" + mNowTimeString + "_" + String.format("%03d", mNumberOfFilesWritten) + ".vtk";
    mFilenameBuffer.add(mSaveDirAbsPath + mFilename);
    File file = new File(dir, mFilename);

    try {

        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

        out.write(("# vtk DataFile Version 3.0\n" + "vtk output\n" + "BINARY\n" + "DATASET POLYDATA\n"
                + "POINTS " + xyzIj.xyzCount + " float\n").getBytes());

        for (int i = 0; i < xyzIj.xyzCount; i++) {

            out.writeFloat(myBuffer.getFloat(3 * i * 4));
            out.writeFloat(myBuffer.getFloat((3 * i + 1) * 4));
            out.writeFloat(myBuffer.getFloat((3 * i + 2) * 4));
        }

        out.write(("\nVERTICES 1 " + String.valueOf(xyzIj.xyzCount + 1) + "\n").getBytes());
        out.writeInt(xyzIj.xyzCount);
        for (int i = 0; i < xyzIj.xyzCount; i++) {
            out.writeInt(i);
        }

        out.write(("\nFIELD FieldData 1\n" + "timestamp 1 1 float\n").getBytes());
        out.writeFloat((float) xyzIj.timestamp);

        out.close();
        mNumberOfFilesWritten++;
        mTimeToTakeSnap = false;

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.hadoop.io.TestArrayOutputStream.java

private void runComparison(ArrayOutputStream aos, DataOutputStream dos, ByteArrayOutputStream bos)
        throws IOException {
    Random r = new Random();
    // byte/*from   w w w  .  ja  v a  2  s .  c o m*/
    int b = r.nextInt(128);
    aos.write(b);
    dos.write(b);

    // byte[]
    byte[] bytes = new byte[10];
    r.nextBytes(bytes);
    aos.write(bytes, 0, 10);
    dos.write(bytes, 0, 10);

    // Byte
    aos.writeByte(b);
    dos.writeByte(b);

    // boolean
    boolean bool = r.nextBoolean();
    aos.writeBoolean(bool);
    dos.writeBoolean(bool);

    // short
    short s = (short) r.nextInt();
    aos.writeShort(s);
    dos.writeShort(s);

    // char
    int c = r.nextInt();
    aos.writeChar(c);
    dos.writeChar(c);

    // int
    int i = r.nextInt();
    aos.writeInt(i);
    dos.writeInt(i);

    // long
    long l = r.nextLong();
    aos.writeLong(l);
    dos.writeLong(l);

    // float
    float f = r.nextFloat();
    aos.writeFloat(f);
    dos.writeFloat(f);

    // double
    double d = r.nextDouble();
    aos.writeDouble(d);
    dos.writeDouble(d);

    // strings
    String str = RandomStringUtils.random(20);
    aos.writeBytes(str);
    aos.writeChars(str);
    aos.writeUTF(str);
    dos.writeBytes(str);
    dos.writeChars(str);
    dos.writeUTF(str);

    byte[] expected = bos.toByteArray();
    assertEquals(expected.length, aos.size());

    byte[] actual = new byte[aos.size()];
    System.arraycopy(aos.getBytes(), 0, actual, 0, actual.length);
    // serialized bytes should be the same
    assertTrue(Arrays.equals(expected, actual));
}

From source file:org.nd4j.linalg.util.ArrayUtil.java

public static void write(float[] data, DataOutputStream dos) throws IOException {
    for (int i = 0; i < data.length; i++)
        dos.writeFloat(data[i]);
}

From source file:ClassFile.java

public void write(DataOutputStream dos, ConstantPoolInfo pool[]) throws IOException, Exception {
    dos.write(type);/*  www.j  av  a  2s  .  c  o m*/
    switch (type) {
    case CLASS:
    case STRING:
        dos.writeShort(indexOf(arg1, pool));
        break;
    case FIELDREF:
    case METHODREF:
    case INTERFACE:
    case NAMEANDTYPE:
        dos.writeShort(indexOf(arg1, pool));
        dos.writeShort(indexOf(arg2, pool));
        break;
    case INTEGER:
        dos.writeInt(intValue);
        break;
    case FLOAT:
        dos.writeFloat(floatValue);
        break;
    case LONG:
        dos.writeLong(longValue);
        break;
    case DOUBLE:
        dos.writeDouble(doubleValue);
        break;
    case ASCIZ:
    case UNICODE:
        dos.writeShort(strValue.length());
        dos.writeBytes(strValue);
        break;
    default:
        throw new Exception("ConstantPoolInfo::write() - bad type.");
    }
}

From source file:org.apache.jmeter.protocol.mqtt.client.MqttPublisher.java

public byte[] createRandomPayload(String Seed, String min, String max, String type_random, String useTimeStamp,
        String useNumSeq, String type_value, String format, String charset)
        throws IOException, NumberFormatException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    DataOutputStream d = new DataOutputStream(b);
    // flags     
    byte flags = 0x00;
    if ("TRUE".equals(useTimeStamp))
        flags |= 0x80;/*  w  w w  .j  av  a2s .c o  m*/
    if ("TRUE".equals(useNumSeq))
        flags |= 0x40;
    if (MQTTPublisherGui.INT.equals(type_value))
        flags |= 0x20;
    if (MQTTPublisherGui.LONG.equals(type_value))
        flags |= 0x10;
    if (MQTTPublisherGui.FLOAT.equals(type_value))
        flags |= 0x08;
    if (MQTTPublisherGui.DOUBLE.equals(type_value))
        flags |= 0x04;
    if (MQTTPublisherGui.STRING.equals(type_value))
        flags |= 0x02;
    if (!"TEXT".equals(type_value)) {
        d.writeByte(flags);
    }
    // TimeStamp
    if ("TRUE".equals(useTimeStamp)) {
        Date date = new java.util.Date();
        d.writeLong(date.getTime());
    }
    // Number Sequence
    if ("TRUE".equals(useNumSeq)) {
        d.writeInt(numSeq++);

    }
    // Value

    if (MQTTPublisherGui.PSEUDO.equals(type_random)) {
        generator.setSeed(Long.parseLong(Seed));
        if (MQTTPublisherGui.INT.equals(type_value)) {
            d.writeInt(
                    generator.nextInt(Integer.parseInt(max) - Integer.parseInt(min)) + Integer.parseInt(min));
        } else if (MQTTPublisherGui.LONG.equals(type_value)) {
            long Max = Long.parseLong(max);
            long Min = Long.parseLong(min);
            d.writeLong((Math.abs(generator.nextLong() % (Max - Min)) + Min));
        } else if (MQTTPublisherGui.DOUBLE.equals(type_value)) {
            double Max = Double.parseDouble(max);
            double Min = Double.parseDouble(min);
            d.writeDouble((Min + (Max - Min) * generator.nextDouble()));
        } else if (MQTTPublisherGui.FLOAT.equals(type_value)) {
            float Max = Float.parseFloat(max);
            float Min = Float.parseFloat(min);
            d.writeFloat((Min + (Max - Min) * generator.nextFloat()));
        }
    } else if (MQTTPublisherGui.SECURE.equals(type_random)) {

        secureGenerator.setSeed(Long.parseLong(Seed));
        if (MQTTPublisherGui.INT.equals(type_value)) {
            d.writeInt(secureGenerator.nextInt(Integer.parseInt(max) - Integer.parseInt(min))
                    + Integer.parseInt(min));
        } else if (MQTTPublisherGui.LONG.equals(type_value)) {
            long Max = Long.parseLong(max);
            long Min = Long.parseLong(min);
            d.writeLong((Math.abs(secureGenerator.nextLong() % (Max - Min)) + Min));
        } else if (MQTTPublisherGui.DOUBLE.equals(type_value)) {
            double Max = Double.parseDouble(max);
            double Min = Double.parseDouble(min);
            d.writeDouble((Min + (Max - Min) * secureGenerator.nextDouble()));
        } else if (MQTTPublisherGui.FLOAT.equals(type_value)) {
            float Max = Float.parseFloat(max);
            float Min = Float.parseFloat(min);
            d.writeFloat((Min + (Max - Min) * secureGenerator.nextFloat()));
        }
    }

    // Format: Encoding        
    if (MQTTPublisherGui.BINARY.equals(format)) {
        BinaryCodec encoder = new BinaryCodec();
        return encoder.encode(b.toByteArray());
    } else if (MQTTPublisherGui.BASE64.equals(format)) {
        return Base64.encodeBase64(b.toByteArray());
    } else if (MQTTPublisherGui.BINHEX.equals(format)) {
        Hex encoder = new Hex();
        return encoder.encode(b.toByteArray());
    } else if (MQTTPublisherGui.PLAIN_TEXT.equals(format)) {
        String s = new String(b.toByteArray(), charset);
        return s.getBytes();

    } else
        return b.toByteArray();

}