Java File Write via ByteBuffer writeDouble(BufferedWriter bw, double[] buf)

Here you can find the source of writeDouble(BufferedWriter bw, double[] buf)

Description

Write a given double array to the ASCII stream

License

Open Source License

Parameter

Parameter Description
bw a parameter
buf a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void writeDouble(BufferedWriter bw, double[] buf) throws IOException 

Method Source Code

//package com.java2s;
/*/*w  w  w . j  a v  a 2  s. c o m*/
   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 double to the OutputStream
     * pointer respectively.
     * @param os
     * @param val
     * @param bo
     * @return
     * @throws IOException
     */
    public static void writeDouble(OutputStream os, double val, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(Double.SIZE / 8);
        bb.order(bo);
        bb.putDouble(val);
        os.write(bb.array());
    }

    /**
     * Write the given double array to the OutputStream using the specified 
     * ByteOrder
     * @param os
     * @param buf
     * @param bo
     * @throws IOException
     */
    public static void writeDouble(OutputStream os, double[] buf, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(buf.length * Double.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.putDouble(d);
        }
        os.write(bb.array());
    }

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

Related

  1. writeByte(WritableByteChannel channel, byte value)
  2. writeBytes(Path file, byte[] bytes)
  3. writeComment(File zipFile, String comment)
  4. writeDataLengthsToHeader(FileOutputStream fpo)
  5. writeDelimitedToOutputStream(byte[] bytes, OutputStream outputStream)
  6. writeEmpty()
  7. writeFC(String fname, float[] res)
  8. writeFile(File file, byte[] bytes)
  9. writeFile(File file, byte[] data)