@Test
public void testByteArrayMaxResultLength() throws IOException {

    final byte[] allBytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
    final Map<Integer, byte[]> testCases = new HashMap<>();
    testCases.put(0, new byte[]{});
    testCases.put(2, Arrays.copyOfRange(allBytes, 0, 2));
    testCases.put(allBytes.length, allBytes);
    testCases.put(Integer.MAX_VALUE, allBytes);
    for (final Map.Entry<Integer, byte[]> tc : testCases.entrySet()) {
        final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null);
        final byte[] bytes = EntityUtils.toByteArray(entity, tc.getKey());
        final byte[] expectedBytes = tc.getValue();
        Assert.assertNotNull(bytes);
        Assert.assertEquals(expectedBytes.length, bytes.length);
        for (int i = 0; i < expectedBytes.length; i++) {
            Assert.assertEquals(expectedBytes[i], bytes[i]);
        }
    }
}