Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.lang.primitives

File Floats.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
19
2
1
39
35
5
0.26
9.5
2
2.5

Classes

Class Line # Actions
Floats 6 19 5
0.925925992.6%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package io.sunshower.lang.primitives;
2   
3    import static java.lang.Float.floatToIntBits;
4    import static java.lang.Float.intBitsToFloat;
5   
 
6    public class Floats {
7   
 
8  1 toggle public static final byte[] toByteArray(float[] floats) {
9  1 int length = floats.length;
10  1 byte[] result = new byte[length * 4];
11  10 for (int i = 0, j = 0; i < length; i++) {
12  9 int data = floatToIntBits(floats[i]);
13  9 result[j] = (byte) (data >>> 24);
14  9 result[j + 1] = (byte) (data >>> 16);
15  9 result[j + 2] = (byte) (data >>> 8);
16  9 result[j + 3] = (byte) (data);
17  9 j += 4;
18    }
19  1 return result;
20    }
21   
 
22  1 toggle public static final float[] fromByteArray(byte[] floats) {
23  1 final int len = floats.length;
24  1 if (len % 4 != 0) {
25  0 throw new IllegalArgumentException("Byte array must be divisible by 4");
26    }
27  1 final float[] result = new float[len / 4];
28  10 for (int i = 0, j = 0; i < result.length; i++) {
29  9 int r =
30    floats[j] << 24
31    | (floats[j + 1] & 0xFF) << 16
32    | (floats[j + 2] & 0xFF) << 8
33    | (floats[j + 3] & 0xFF);
34  9 result[i] = intBitsToFloat(r);
35  9 j += 4;
36    }
37  1 return result;
38    }
39    }