ByteArrayUtil { public static byte[] toByteArray(BitSet bitSet) { byte[] array = new byte[bitSet.size() / 8 + 1]; for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { array[i / 8] |= byteMask(i); } return array; }  static void put(byte[] source, byte[] target, int offset); static byte[] get(byte[] array, int offset); static byte[] get(byte[] array, int offset, int length); static void putInt(int value, byte[] array, int offset); static int getInt(byte[] array, int offset); static void putLong(long value, byte[] array, int offset); static long getLong(byte[] array, int offset); static int find(byte[] a, int fromIndex, int toIndex, byte key); static int find(byte[] a, int fromIndex, int toIndex, byte[] key); static boolean matchesPattern(byte[] value, byte[] mask, byte[] pattern); static boolean regionMatches(byte[] subValue, byte[] superValue, int offset); static int compareRegion(byte[] array1, int startIdx1, byte[] array2, int startIdx2, int length); static BitSet toBitSet(byte[] array); static byte[] toByteArray(BitSet bitSet); static String toHexString(byte[] array);  }
@Test public void testToByteArray() { final BitSet bitSet = new BitSet(); assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ByteArrayUtil.toByteArray(bitSet)); bitSet.set(1); assertArrayEquals(new byte[] { 64, 0, 0, 0, 0, 0, 0, 0, 0 }, ByteArrayUtil.toByteArray(bitSet)); bitSet.set(4); assertArrayEquals(new byte[] { 72, 0, 0, 0, 0, 0, 0, 0, 0 }, ByteArrayUtil.toByteArray(bitSet)); }