WritableUtils { public static void convertByteToFloat(byte[] byteArray, float[] floatArray) { Assert.argument(4 * floatArray.length == byteArray.length, "4 * floatArray.length == byteArray.length"); for (int i = 0; i < floatArray.length; i++) { floatArray[i] = WritableComparator.readFloat(byteArray, i * 4); } }  static void convertByteToFloat(byte[] byteArray, float[] floatArray); static void convertFloatToByte(float[] floatArray, byte[] byteArray);  }
@Test(expected = IllegalArgumentException.class) public void testConvertByteToFloat_wrongSize() throws Exception { byte[] byteArray = new byte[7]; float[] floatArray = new float[2]; WritableUtils.convertByteToFloat(byteArray, floatArray); }
@Test public void testConvertByteToFloat() throws Exception { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); dataOutputStream.writeFloat(0.12f); dataOutputStream.writeFloat(0.42f); dataOutputStream.writeFloat(5.67f); byte[] byteArray = byteArrayOutputStream.toByteArray(); float[] floatArray = new float[3]; WritableUtils.convertByteToFloat(byteArray, floatArray); assertEquals(0.12f, floatArray[0], 0.000001f); assertEquals(0.42f, floatArray[1], 0.000001f); assertEquals(5.67f, floatArray[2], 0.000001f); }