Java RandomAccessFile Read LoadFloatFile(String filename)

Here you can find the source of LoadFloatFile(String filename)

Description

Get an array of float values from a file of bytes, in PC order.

License

Open Source License

Parameter

Parameter Description
filename The name of the file of binary floats to read

Return

An array containing float values obtained by converting sequences of four bytes into the corresponding float value.

Declaration

public static float[] LoadFloatFile(String filename) 

Method Source Code


//package com.java2s;
/* //from   www.j a v a2  s  .  c o  m
 * File: FileUtil.java
 *
 * Copyright (C) 2010, Dennis Mikkelson
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the Spallation Neutron Source Division
 * of Oak Ridge National Laboratory, Oak Ridge, TN, USA.
 *
 *  Last Modified:
 * 
 *  $Author:$
 *  $Date:$            
 *  $Revision:$
 */

import java.io.*;

public class Main {
    private static int BUFFER_SIZE = 32768;

    /**
     *  Get an array of float values from a file of bytes, in PC order.  
     *  The number of floats returned in the array is the integer part 
     *  of filesize/4.
     *
     *  @param filename  The name of the file of binary floats to read
     *
     *  @return An array containing float values obtained by converting
     *          sequences of four bytes into the corresponding float value.
     */
    public static float[] LoadFloatFile(String filename) {
        try {
            RandomAccessFile r_file = new RandomAccessFile(filename, "r");

            long num_bytes = r_file.length();

            if (num_bytes >= 4l * (long) (Integer.MAX_VALUE))
                throw new IllegalArgumentException("File has more than " + Integer.MAX_VALUE
                        + " float values and can't be loaded into an array");

            int num_floats = (int) (num_bytes / 4);
            float[] list = new float[num_floats];

            r_file.seek(0);
            long num_loaded = 0;
            long seg_size = BUFFER_SIZE;
            long bytes_read;
            byte[] buffer = new byte[BUFFER_SIZE];
            while (num_loaded < num_floats) {
                bytes_read = r_file.read(buffer);
                seg_size = Math.min(seg_size, bytes_read);

                seg_size = (seg_size / 4) * 4; // make sure it's a multiple of 4

                if (bytes_read > 0) {
                    UnpackFloatBuffer(buffer, seg_size, num_loaded, list);
                    num_loaded += bytes_read / 4;
                } else {
                    System.out.println("ERROR: Unexpected end of float file: " + filename + " after reading "
                            + num_loaded + " out of " + num_floats + " floats.");
                    num_loaded = num_floats;
                }
            }
            r_file.close();
            return list;
        } catch (IOException ex) {
            throw new IllegalArgumentException("Failed to load floats from file " + filename);
        }
    }

    /**
     *  Get the float values stored in the input file buffer and put the
     *  values into the proper positions in the list[] array.  
     *
     *  @param buffer      The array of bytes as read in one segment from the
     *                     float data file.
     *  @param bytes_read  The number of bytes that were read in from the file
     *                     and are to be extracted and placed in the list[]
     *                     array.
     *  @param num_loaded  The number of floats that have already been loaded. 
     *                     This provides the position where the floats from 
     *                     the buffer should be stored.
     *  @param list        The list being filled with integers from the file.
     */
    public static void UnpackFloatBuffer(byte[] buffer, long bytes_read, long num_loaded, float[] list) {
        int index = (int) (num_loaded);
        for (int i = 0; i < bytes_read; i += 4)
            list[index++] = getFloat_32(buffer, i);
    }

    /**
     * Decode the float value stored in a sequence of four bytes in 
     * the buffer.  The four bytes determining the float value are 
     * stored in the file and buffer in the sequence: b0,...,b3, with 
     * the lowest order byte, b0, first and the the highest order byte, 
     * b3, last.
     * NOTE: This method reverses the action of setFloat_32.
     * 
     * @param i  The index of the first byte in the buffer
     *                    
     * @return The float value represented by four successive bytes from
     *         the file. 
     */
    public static float getFloat_32(byte[] buffer, int i) {
        int int_val = 0;

        for (int shift = 0; shift < 32; shift += 8)
            int_val |= ((int) buffer[i++] & 0xFF) << shift;

        return Float.intBitsToFloat(int_val);
    }
}

Related

  1. appendFile(RandomAccessFile main, RandomAccessFile extra)
  2. load(String f)
  3. load(String fn, int type, int m, int n)
  4. LoadDspaceMapFile(String filename)
  5. LoadGhostMapFile(String filename, int n_ids, int n_ghosts)
  6. read_arraydesc(final RandomAccessFile raf)
  7. read_int32(RandomAccessFile raf)
  8. read_long(final RandomAccessFile raf)