### Question:
Py4jByteArrayConverter { public static boolean[] toBooleanArray(byte[] byteArray, int numberOfBooleansInArray) { BitSet bits = BitSet.valueOf(byteArray); boolean[] bools = new boolean[numberOfBooleansInArray]; for (int i = bits.nextSetBit(0); i != -1; i = bits.nextSetBit(i + 1)) { int position = getBigEndianPosition(i); if (position >= numberOfBooleansInArray) { throw new Py4jByteArrayConversionException("There are more encoded booleans than the number of booleans specified"); } bools[position] = true; } return bools; }  byte[] toByteArray(double[] doubleArray); byte[] toByteArray(int[] integerArray); byte[] toByteArray(boolean[] booleanArray); static double[] toDoubleArray(byte[] byteArray); static int[] toIntegerArray(byte[] byteArray); static long[] toLongArray(byte[] byteArray); static boolean[] toBooleanArray(byte[] byteArray, int numberOfBooleansInArray);  }### Answer:
@Test public void canCreateBooleanArrayFromByteArray() { byte byte1 = (byte) Integer.parseInt("10101010", 2); byte byte2 = (byte) Integer.parseInt("10000000", 2); byte[] bytes = new byte[]{byte1, byte2}; boolean[] bools = Py4jByteArrayConverter.toBooleanArray(bytes, bytes.length * 8); boolean[] desiredBools = new boolean[]{ true, false, true, false, true, false, true, false, true, false, false, false, false, false, false, false }; assertArrayEquals(bools, desiredBools); }
@Test public void doesntCreateTooManyBooleansFromPadding() { byte byte1 = (byte) Integer.parseInt("10101010", 2); byte byte2 = (byte) Integer.parseInt("10000000", 2); byte[] bytes = new byte[]{byte1, byte2}; boolean[] bools = Py4jByteArrayConverter.toBooleanArray(bytes, bytes.length * 8 - 4); boolean[] desiredBools = new boolean[]{ true, false, true, false, true, false, true, false, true, false, false, false }; assertArrayEquals(bools, desiredBools); }
@Test public void throwsIfTooManyBooleansAreEncoded() { byte byte1 = (byte) Integer.parseInt("10101010", 2); byte byte2 = (byte) Integer.parseInt("10000001", 2); byte[] bytes = new byte[]{byte1, byte2}; expectedException.expect(Py4jByteArrayConversionException.class); expectedException.expectMessage("There are more encoded booleans than the number of booleans specified"); Py4jByteArrayConverter.toBooleanArray(bytes, bytes.length * 8 - 4); }