Clover icon

sunshower-base

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

File Doubles.java

 

Coverage histogram

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

Code metrics

6
23
2
1
47
43
5
0.22
11.5
2
2.5

Classes

Class Line # Actions
Doubles 6 23 5
0.935483993.5%
 

Contributing tests

This file is covered by 1 test. .

Source view

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