Clover icon

sunshower-base

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

File Bytes.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
16% of files have more coverage

Code metrics

10
19
10
1
66
54
15
0.79
1.9
10
1.5

Classes

Class Line # Actions
Bytes 7 19 15
0.897435989.7%
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    package io.sunshower.lang.primitives;
2   
3    import javax.annotation.InPlace;
4    import javax.annotation.Nonnegative;
5    import javax.annotation.Nonnull;
6   
 
7    public class Bytes {
8   
 
9  1 toggle private Bytes() {
10  1 throw new UnsupportedOperationException("No Bytes for you!");
11    }
12   
 
13  2 toggle @InPlace
14    public static void set(@Nonnull byte[] bytes, @Nonnegative int index, boolean value) {
15  2 set(bytes, index, value ? 1 : 0);
16    }
17   
 
18  4 toggle @InPlace
19    public static void set(@Nonnull byte[] bytes, @Nonnegative int index, int value) {
20  4 final int ind = index / 7;
21  4 final int aind = index % 8;
22  4 byte b = bytes[ind];
23  4 bytes[ind] = (value % 2 == 1) ? (b |= 1 << aind) : (b &= ~(1 << aind));
24    }
25   
 
26  28 toggle @InPlace
27    public static int get(@Nonnull byte[] bytes, @Nonnegative int index) {
28  28 return (bytes[index / 7] >> (index % 8) & 1);
29    }
30   
 
31  16 toggle public static int get(byte i, @Nonnegative int index) {
32  16 return (i >> index) & 1;
33    }
34   
 
35  6 toggle public static int set(byte i, @Nonnegative int index) {
36  6 return i | 1 << index;
37    }
38   
 
39  6 toggle public static int clear(byte i, @Nonnegative int index) {
40  6 return i & ~(1 << index);
41    }
42   
 
43  2 toggle @Nonnull
44    public static String toBitString(@Nonnegative byte[] bits) {
45  2 final StringBuilder b = new StringBuilder(bits.length * 8);
46  8 for (int i = 0; i < bits.length; ++i) {
47  54 for (int j = 0; j < 8; ++j) {
48  48 b.append((bits[i] >> j) & 1);
49    }
50    }
51  2 return b.toString();
52    }
53   
 
54  1 toggle public static byte[] toByteArray(int value) {
55  1 return new byte[] {
56    (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value
57    };
58    }
59   
 
60  1 toggle public static int fromByteArray(byte[] bytes) {
61  1 if (bytes.length != 4) {
62  0 throw new IllegalArgumentException("There must be exactly 4 values in a byte array");
63    }
64  1 return bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
65    }
66    }