Java File Read via ByteBuffer readFloat(BufferedReader br)

Here you can find the source of readFloat(BufferedReader br)

Description

Read a float array from the stream without knowing its size ahead of time

License

Open Source License

Parameter

Parameter Description
br a parameter

Return

float array

Declaration

public float[] readFloat(BufferedReader br) throws IOException 

Method Source Code


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

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

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

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

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

        // decode the double
        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

        return bb.getFloat();
    }

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

        // complete frame?
        if (read < bbuf.length)
            return false;

        // decode the double
        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

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

        return true;
    }

    /**
     * Reads floats from a binary InputStream and store them in the double buffer
     * using given ByteOrder
     * @param is binary InputStream
     * @param buf (output) buffer
     * @return true on success, false else.
     * @throws IOException
     */
    public static boolean readFloat(InputStream is, double[] buf, ByteOrder bo) throws IOException {
        byte[] bbuf = new byte[buf.length * Float.SIZE / 8];
        int read = is.read(bbuf);

        // complete frame?
        if (read < bbuf.length)
            return false;

        // decode the double
        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

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

        return true;
    }

    /**
     * Reads float array from ASCII stream
     * 
     * @param br allocated BufferedReader 
     * @param buf (output) buffer
     * @return true on success, false else.
     * @throws IOException
     */
    public static boolean readFloat(BufferedReader br, float[] 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] = Float.valueOf(tokens[i]);

        return true;
    }

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

        if (line == null)
            return null;

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

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

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

        return buf;
    }
}

Related

  1. readFileNIO(String path, StringBuilder builder)
  2. readFileToBuffer(java.io.File file)
  3. readFileToString(String path)
  4. readFileToString(String path)
  5. readFlashString(DataInputStream s)
  6. ReadFloat(InputStream is)
  7. readFromBuffer(byte[] buffer, int start, int end)
  8. readFromFile(Path path)
  9. readFromFile(String fileName)