Example usage for java.io DataInputStream readFloat

List of usage examples for java.io DataInputStream readFloat

Introduction

In this page you can find the example usage for java.io DataInputStream readFloat.

Prototype

public final float readFloat() throws IOException 

Source Link

Document

See the general contract of the readFloat method of DataInput.

Usage

From source file:Main.java

public static float[] readBinShapeArray(String dir, String fileName, int size) {
    float x;/*from w  w  w  .j  a  v  a 2  s  . com*/
    int i = 0;
    float[] tab = new float[size];

    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        //read first x
        x = in.readFloat();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readFloat();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

/**
 * Reads in model 83 points and returns a double array float containing the
 * coordinates x & y.//from   ww  w .  ja va2s. c  o m
 */
public static float[][] readBinModel83Pt2DFloat(String dir, String fileName) {
    float[][] array2D = new float[83][2];
    float x;
    int i = 0;
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        while (true) {
            x = in.readFloat();
            array2D[i][0] = x;
            x = in.readFloat();
            array2D[i][1] = x;
            i++;
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return array2D;
}

From source file:Main.java

public static float[] readBinAverageShapeArray(String dir, String fileName, int size) {
    float x;//w w  w.j a  v a 2 s.c  o m
    int i = 0;
    float[] tab = new float[size];

    // theses values was for 64140 points average shape
    //float minX = -90.3540f, minY = -22.1150f, minZ = -88.7720f, maxX = 101.3830f, maxY = 105.1860f, maxZ = 102.0530f;

    // values for average shape after simplification (8489 points)
    float maxX = 95.4549f, minX = -85.4616f, maxY = 115.0088f, minY = -18.0376f, maxZ = 106.7329f,
            minZ = -90.4051f;

    float deltaX = (maxX - minX) / 2.0f;
    float deltaY = (maxY - minY) / 2.0f;
    float deltaZ = (maxZ - minZ) / 2.0f;

    float midX = (maxX + minX) / 2.0f;
    float midY = (maxY + minY) / 2.0f;
    float midZ = (maxZ + minZ) / 2.0f;

    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        //read first x
        x = in.readFloat();
        //Log.d(TAG,"first Vertices = "+x);
        while (true) {

            tab[i] = (x - midX) / deltaX; //rescale x
            i++;

            //read y
            x = in.readFloat();
            tab[i] = (x - midY) / deltaY; //rescale y
            i++;

            //read z
            x = in.readFloat();
            tab[i] = (x - midZ) / deltaZ; //rescale z
            i++;

            //read x
            x = in.readFloat();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

/**
 * Read the featureVector_Shape.dat file.
 * Build the double array and return it.
 * k : number of rows/*from w  w  w  .jav a  2 s.  c om*/
 * n : number of columns
 */
public static float[][] readBinFloatDoubleArray(String dir, String fileName, int k, int n) {
    float[][] array2D = new float[k][n];
    float x;
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < n; j++) {
                x = in.readFloat();
                array2D[i][j] = x;
            }
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return array2D;
}

From source file:Main.java

/**
 * Reads in model 83 points and returns a double array float containing the
 * coordinates x & y./*from   ww  w . jav  a2  s .  c o  m*/
 */
public static float[][][] readBinSFSV(String dir, String fileName) {
    float[][][] array3D = new float[60][83][3];
    float x;
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        for (int k = 0; k < 3; k++) {
            for (int j = 0; j < 83; j++) {
                for (int i = 0; i < 60; i++) {
                    x = in.readFloat();
                    array3D[i][j][k] = x;
                }
            }
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return array3D;
}

From source file:Main.java

public static Object[] readSettings(String file) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from w  w  w.  j av a  2s .  c o  m*/
        Object[] res = new Object[in.readInt()];
        for (int i = 0; i < res.length; i++) {
            char cl = in.readChar();
            switch (cl) {
            case 'S':
                res[i] = in.readUTF();
                break;
            case 'F':
                res[i] = in.readFloat();
                break;
            case 'D':
                res[i] = in.readDouble();
                break;
            case 'I':
                res[i] = in.readInt();
                break;
            case 'L':
                res[i] = in.readLong();
                break;
            case 'B':
                res[i] = in.readBoolean();
                break;
            case 'Y':
                res[i] = in.readByte();
                break;
            default:
                throw new IllegalStateException("cannot read type " + cl + " from " + file);
            }
        }
        return res;
    } finally {
        in.close();
    }
}

From source file:jsave.Utils.java

static Matrix read_array(RandomAccessFile raf, int typecode, HashMap array_desc) throws Exception {

    //TODO : To be refactored to take into account typecode
    //        if (typecode == 1 || typecode == 3 || typecode == 4 ||
    //                typecode == 5 || typecode == 6 || typecode == 9 || typecode == 13 ||
    //                typecode == 14 || typecode == 15) {
    //            if (typecode == 1) {
    //                int nbBytes = read_int32(raf);
    //                if (nbBytes != (int) array_desc.get("nbytes")) {
    //                    throw new Exception("Error occurred while reading byte array");
    //                }
    //            }
    //            // read as numpy array
    //            byte[] data = new byte[(int) array_desc.get("nbytes")];
    //            raf.read(data);
    //            Array ok =  fromString(data, DTYPE_DICT.get(typecode));
    ////w  ww  .ja v a2  s. co  m
    //        } else if (typecode == 2 || typecode == 12) {
    //            // JC old version
    //            byte[] data = new byte[(int) array_desc.get("nbytes")];
    //            raf.read(data);
    //            ByteArrayInputStream bas = new ByteArrayInputStream(data);
    //            DataInputStream ds = new DataInputStream(bas);
    //            float[] fArr = new float[data.length / 4];  // 4 bytes per float
    //            for (int i = 0; i < fArr.length; i++) {
    //                fArr[i] = ds.readFloat();
    //            }
    //        }
    byte[] data = new byte[(int) array_desc.get("nbytes")];
    raf.read(data);
    ByteArrayInputStream bas = new ByteArrayInputStream(data);
    DataInputStream ds = new DataInputStream(bas);
    float[] fArr = new float[data.length / 4]; // 4 bytes per float
    for (int i = 0; i < fArr.length; i++) {
        fArr[i] = ds.readFloat();
    }

    Matrix cube;
    int nbDims = (int) array_desc.get("ndims");

    int[] dims = new int[nbDims];
    int[] tmpDims = (int[]) array_desc.get("dims");

    // Filtering useless '1' value in dimension tab
    int j = 0;
    for (int i = 0; i < tmpDims.length; i++) {
        if (tmpDims[i] != 1) {
            dims[j] = tmpDims[i];
            j++;
        }
    }
    reverseArray(dims);

    int xDim = (dims.length >= 1) ? dims[0] : 1;
    int yDim = (dims.length >= 2) ? dims[1] : 1;
    int zDim = (dims.length == 3) ? dims[2] : 1;
    cube = new Matrix(xDim, yDim, zDim); // 480, 120, 69 ==> 3974400

    int indfArr = 0;
    for (int d2 = 0; d2 < zDim; d2++) { // 69 plans
        for (int d0 = 0; d0 < xDim; d0++) { // 480 lignes
            for (int d1 = 0; d1 < yDim; d1++) { // 120 colonnes
                cube.setCubeValue(d0, d1, d2, fArr[indfArr]);
                indfArr++;
            }
        }
    }

    align_32(raf);

    return cube;
}

From source file:RealFunctionValidation.java

public static Object readAndWritePrimitiveValue(final DataInputStream in, final DataOutputStream out,
        final Class<?> type) throws IOException {

    if (!type.isPrimitive()) {
        throw new IllegalArgumentException("type must be primitive");
    }//w w w.java 2 s  . c om
    if (type.equals(Boolean.TYPE)) {
        final boolean x = in.readBoolean();
        out.writeBoolean(x);
        return Boolean.valueOf(x);
    } else if (type.equals(Byte.TYPE)) {
        final byte x = in.readByte();
        out.writeByte(x);
        return Byte.valueOf(x);
    } else if (type.equals(Character.TYPE)) {
        final char x = in.readChar();
        out.writeChar(x);
        return Character.valueOf(x);
    } else if (type.equals(Double.TYPE)) {
        final double x = in.readDouble();
        out.writeDouble(x);
        return Double.valueOf(x);
    } else if (type.equals(Float.TYPE)) {
        final float x = in.readFloat();
        out.writeFloat(x);
        return Float.valueOf(x);
    } else if (type.equals(Integer.TYPE)) {
        final int x = in.readInt();
        out.writeInt(x);
        return Integer.valueOf(x);
    } else if (type.equals(Long.TYPE)) {
        final long x = in.readLong();
        out.writeLong(x);
        return Long.valueOf(x);
    } else if (type.equals(Short.TYPE)) {
        final short x = in.readShort();
        out.writeShort(x);
        return Short.valueOf(x);
    } else {
        // This should never occur.
        throw new IllegalStateException();
    }
}

From source file:ClassFile.java

public boolean read(DataInputStream dis) throws IOException {
    int len;//from w  w w  . j ava 2 s  . c  o m
    char c;

    type = dis.readByte();
    switch (type) {
    case CLASS:
        name = "Class";
        index1 = dis.readShort();
        index2 = -1;
        break;
    case FIELDREF:
        name = "Field Reference";
        index1 = dis.readShort();
        index2 = dis.readShort();
        break;
    case METHODREF:
        name = "Method Reference";
        index1 = dis.readShort();
        index2 = dis.readShort();
        break;
    case INTERFACE:
        name = "Interface Method Reference";
        index1 = dis.readShort();
        index2 = dis.readShort();
        break;
    case NAMEANDTYPE:
        name = "Name and Type";
        index1 = dis.readShort();
        index2 = dis.readShort();
        break;
    case STRING:
        name = "String";
        index1 = dis.readShort();
        index2 = -1;
        break;
    case INTEGER:
        name = "Integer";
        intValue = dis.readInt();
        break;
    case FLOAT:
        name = "Float";
        floatValue = dis.readFloat();
        break;
    case LONG:
        name = "Long";
        longValue = dis.readLong();
        break;
    case DOUBLE:
        name = "Double";
        doubleValue = dis.readDouble();
        break;
    case ASCIZ:
    case UNICODE:
        if (type == ASCIZ)
            name = "ASCIZ";
        else
            name = "UNICODE";

        StringBuffer xxBuf = new StringBuffer();

        len = dis.readShort();
        while (len > 0) {
            c = (char) (dis.readByte());
            xxBuf.append(c);
            len--;
        }
        strValue = xxBuf.toString();
        break;
    default:
        System.out.println("Warning bad type.");
    }
    return (true);
}

From source file:org.apache.hama.monitor.ZKCollector.java

@Override
public MetricsRecord harvest() throws Exception {
    final String path = this.reference.get().path;
    final ZooKeeper zk = this.reference.get().zk;
    LOG.debug("Searching " + path + " in zookeeper.");
    Stat stat = zk.exists(path, false);/*from   ww w .  j ava 2 s  .co  m*/
    if (null == stat)
        return null; // no need to collect data.
    List<String> children = zk.getChildren(path, false);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Leaves size is " + children.size() + " total znodes in list: " + children);
    }

    // TODO: metrics record contains multiple metrics (1 to many)
    // data is stored under zk e.g. /path/to/metrics/jvm/...
    // within jvm folder metrics is stored in a form of name, value pair
    final MetricsRecord record = reference.get().record;
    if (null != children) {
        for (String child : children) {
            LOG.info("metrics -> " + child);
            // <metricsName_d> indicates data type is double
            String dataType = suffix(child);
            byte[] dataInBytes = zk.getData(path + "/" + child, false, stat);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Data length (in byte): " + dataInBytes.length);
            }
            DataInputStream input = null;
            try {
                String name = removeSuffix(child);
                input = new DataInputStream(new ByteArrayInputStream(dataInBytes));
                if ("d".equals(dataType)) {
                    double dv = input.readDouble();
                    LOG.info("metrics " + name + " value:" + dv);
                    record.add(new Metric<Double>(name, dv));
                } else if ("f".equals(dataType)) {
                    float fv = input.readFloat();
                    LOG.info("metrics " + name + " value:" + fv);
                    record.add(new Metric<Float>(name, fv));
                } else if ("i".equals(dataType)) {
                    int iv = input.readInt();
                    LOG.info("metrics " + name + " value:" + iv);
                    record.add(new Metric<Integer>(name, iv));
                } else if ("l".equals(dataType)) {
                    long lv = input.readLong();
                    LOG.info("metrics " + name + " value:" + lv);
                    record.add(new Metric<Long>(name, lv));
                } else if ("b".equals(dataType)) {
                    LOG.info("metrics" + name + " value:" + Arrays.toString(dataInBytes));
                    record.add(new Metric<byte[]>(name, dataInBytes));
                } else {
                    LOG.warn("Unkown data type for metrics name: " + child);
                }
            } finally {
                input.close();
            }
        }
    }
    return record;
}