Java File Write via ByteBuffer writeFloat(OutputStream os, float val, ByteOrder bo)

Here you can find the source of writeFloat(OutputStream os, float val, ByteOrder bo)

Description

Write a float to the OutputStream pointer respectively.

License

Open Source License

Parameter

Parameter Description
os a parameter
val a parameter
bo a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void writeFloat(OutputStream os, float val, ByteOrder bo) throws IOException 

Method Source Code

//package com.java2s;
/*/* w  w  w. j a v  a2 s . c  om*/
   Copyright (c) 2009-2011
  Speech Group at Informatik 5, Univ. Erlangen-Nuremberg, GERMANY
  Korbinian Riedhammer
  Tobias Bocklet
    
   This file is part of the Java Speech Toolkit (JSTK).
    
   The JSTK is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   The JSTK is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with the JSTK. If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.BufferedWriter;
import java.io.IOException;

import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /** 
     * Write a float to the OutputStream
     * pointer respectively.
     * @param os
     * @param val
     * @param bo
     * @return
     * @throws IOException
     */
    public static void writeFloat(OutputStream os, float val, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(Float.SIZE / 8);
        bb.order(bo);
        bb.putFloat(val);
        os.write(bb.array());
    }

    /**
     * Write the given float array to the OutputStream using the specified 
     * ByteOrder
     * @param os
     * @param buf
     * @param bo
     * @throws IOException
     */
    public static void writeFloat(OutputStream os, float[] buf, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(buf.length * Float.SIZE / 8);
        bb.order(bo);
        for (float f : buf)
            bb.putFloat(f);
        os.write(bb.array());
    }

    /**
     * Write the given double array as floats to the OutputStream using the 
     * specified ByteOrder
     * @param os
     * @param buf
     * @param bo
     * @throws IOException
     */
    public static void writeFloat(OutputStream os, double[] buf, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(buf.length * Float.SIZE / 8);
        bb.order(bo);
        for (double d : buf) {
            // truncate to 3 digits
            int i = (int) Math.round(d * 1000);
            d = (double) i / 1000;
            bb.putFloat((float) d);
        }
        os.write(bb.array());
    }

    /**
     * Write a given float array to the ASCII stream
     * @param bw
     * @param buf
     * @throws IOException
     */
    public static void writeFloat(BufferedWriter bw, float[] buf) throws IOException {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length - 1; ++i)
            sb.append(Float.toString(buf[i]) + " ");
        sb.append(Float.toString(buf[buf.length - 1]) + "\n");
        bw.append(sb.toString());
    }
}

Related

  1. writeFile(File file, byte[] data)
  2. writeFile(String source, File outputFile)
  3. writeFileAsByteArray(File file, byte[] par2Data)
  4. writeFileAsStringArray(File file, String[] par2DataArray)
  5. writeFileNIO(String txt, File fyl)
  6. writeInt(OutputStream os, int val, ByteOrder bo)
  7. writeInt(OutputStream out, ByteOrder order, int i)
  8. writeInt(OutputStream out, int i)
  9. writeInt(WritableByteChannel channel, int val)