Java SQL Blob blob2FloatArray(Blob blob)

Here you can find the source of blob2FloatArray(Blob blob)

Description

Convert a blob that we wrote a float array back into a usable array

License

Open Source License

Parameter

Parameter Description
blob from the database

Exception

Parameter Description
SQLException an exception

Declaration

public static float[] blob2FloatArray(Blob blob) 

Method Source Code

//package com.java2s;
/*/*ww w . j a v a 2  s .c o  m*/
 * Copyright (C) 2012 Joseph Areeda <joseph.areeda at ligo.org>
 *
 * 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 3 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 program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.DataInputStream;

import java.sql.Blob;

public class Main {
    /**
     * Convert a blob that we wrote a float array back into a usable array
     * @param blob from the database
     * @return
     * @throws SQLException 
     */
    public static float[] blob2FloatArray(Blob blob) {
        float[] ret = null;
        try {
            int blen = (int) blob.length();
            int nbytes = Float.SIZE / 8;
            int olen = (int) (blen / nbytes);
            ret = new float[olen];
            DataInputStream dis = new DataInputStream(blob.getBinaryStream());

            for (int o = 0; o < olen; o++) {
                ret[o] = dis.readFloat();
            }
        } catch (Exception ex) {
            ret = null;
        }
        return ret;
    }
}

Related

  1. blobToBytes(Blob blob)
  2. blobToBytes(Blob blob)
  3. getAsString(Blob blob)
  4. getBlobType(byte[] image)