Java File Read via ByteBuffer readDouble(BufferedReader br, double[] buf)

Here you can find the source of readDouble(BufferedReader br, double[] buf)

Description

Reads doubles from ASCII stream

License

Open Source License

Parameter

Parameter Description
br allocated BufferedReader
buf (output) buffer

Exception

Parameter Description
IOException an exception

Return

true on success, false else.

Declaration

public static boolean readDouble(BufferedReader br, double[] buf) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w  w  w  . j a va2 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.BufferedReader;

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**
     * Read a single Double from the InputStream
     * @param is
     * @param bo
     * @return
     * @throws IOException
     */
    public static double readDouble(InputStream is, ByteOrder bo) throws IOException {
        byte[] bbuf = new byte[Double.SIZE / 8];
        int read = is.read(bbuf);

        if (read < bbuf.length)
            throw new IOException("could not read required bytes");

        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

        return bb.getDouble();
    }

    /**
     * Reads double array from a binary InputStream
     * 
     * @param is binary InputStream
     * @param buf (output) buffer
     * @return true on success, false else.
     * @throws IOException
     */
    public static boolean readDouble(InputStream is, double[] buf, ByteOrder bo) throws IOException {
        byte[] bbuf = new byte[buf.length * Double.SIZE / 8];
        int read = is.read(bbuf);

        if (read < bbuf.length)
            return false;

        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

        for (int i = 0; i < buf.length; ++i)
            buf[i] = bb.getDouble();

        return true;
    }

    /**
     * Reads doubles from ASCII stream
     * 
     * @param br allocated BufferedReader 
     * @param buf (output) buffer
     * @return true on success, false else.
     * @throws IOException
     */
    public static boolean readDouble(BufferedReader br, double[] buf) throws IOException {
        String line = br.readLine();

        if (line == null)
            return false;

        String[] tokens = line.trim().split("\\s+");

        if (tokens.length != buf.length)
            return false;

        for (int i = 0; i < tokens.length; ++i)
            buf[i] = Double.valueOf(tokens[i]);

        return true;
    }

    /**
     * Read a double array from the stream without knowing its size ahead of time
     * @param br
     * @return
     */
    public double[] readDouble(BufferedReader br) throws IOException {
        String line = br.readLine();

        if (line == null)
            return null;

        String[] tokens = line.trim().split("\\s+");

        double[] buf = new double[tokens.length];

        for (int i = 0; i < tokens.length; ++i)
            buf[i] = Double.valueOf(tokens[i]);

        return buf;
    }
}

Related

  1. readColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData)
  2. readContent(File file)
  3. readContentsNIO(FileInputStream input, String encoding, boolean decodeNIO)
  4. readData(String fileName, int size)
  5. readDelimitedFromInputStream(InputStream inputStream)
  6. readDouble(byte[] src, int pointer)
  7. readDouble(FileChannel fileChannel, ByteOrder byteOrder)
  8. readDouble(final byte[] bytes, final int length, final int offset)
  9. readEntireFile(java.io.File file)