ventis07 / lire

Automatically exported from code.google.com/p/lire
Other
0 stars 0 forks source link

Bug in de-serializing a float array #15

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
https://code.google.com/p/lire/source/browse/trunk/src/main/java/net/semanticmet
adata/lire/utils/SerializationUtils.java#245

In the 0.9.4-beta2 code, the function seems to shift the output array by 
offset, and chop off the rest of the float array at the end.

    public static float[] toFloatArray(byte[] in, int offset, int length) {
        float[] result = new float[length / 4];
        byte[] tmp = new byte[4];
        for (int i = offset; i < length / 4; i++) {
            System.arraycopy(in, (i - offset) * 4 + offset, tmp, 0, 4);
            result[i] = toFloat(tmp);
        }
        return result;
    }

For comparison, the code for converting to double array is correct:
    public static double[] toDoubleArray(byte[] data, int offset, int length) {
        double[] result = new double[length / 8];
        byte[] tmp = new byte[8];
        for (int i = 0; i < result.length; i++) {
            System.arraycopy(data, i * 8 + offset, tmp, 0, 8);
            result[i] = toDouble(tmp);
        }
        return result;
    }

Original issue reported on code.google.com by yutian...@gmail.com on 12 Aug 2014 at 9:49