public static byte[] toByteArray(long value, int length) {
        final byte[] buffer = ByteBuffer.allocate(8).putLong(value).array();
        for (int i = 0; i < 8 - length; i++) {
            if (buffer[i] != 0) {
                throw new IllegalArgumentException(
                    "Value is does not fit into byte array " + (8 - i) + " > " + length);
            }
        }
        return adjustLength(buffer, length);
    }
@Test
    public void toByteArrayBigIntegerShouldAddZeros() {
        assertArrayEquals(new byte[] { 0, 0, 0, 0, 1}, ByteArrayUtils.toByteArray(BigInteger.ONE, 5));
    }