@Test
public void binaryResponse() throws Exception {

  ODataResponse response = new ODataResponse();
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_OCTET_STREAM.toContentTypeString());
  // binary content, not a valid UTF-8 representation of a string
  byte[] content = new byte[Byte.MAX_VALUE - Byte.MIN_VALUE + 1];
  for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
    content[i - Byte.MIN_VALUE] = (byte) i;
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream(Byte.MAX_VALUE - Byte.MIN_VALUE + 1);
  out.write(content);
  response.setContent(new ByteArrayInputStream(out.toByteArray()));
  InputStream batchResponse = new BatchResponseSerializer().serialize(
      Collections.singletonList(new ODataResponsePart(Collections.singletonList(response), false)),
      BOUNDARY);
  assertNotNull(batchResponse);
  final String beforeExpected = "--" + BOUNDARY + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + CRLF
      + "HTTP/1.1 200 OK" + CRLF
      + "Content-Type: application/octet-stream" + CRLF
      + "Content-Length: 256" + CRLF
      + CRLF;
  byte[] beforeContent = new byte[beforeExpected.length()];
  batchResponse.read(beforeContent, 0, beforeExpected.length());
  assertArrayEquals(beforeExpected.getBytes(CS_ISO_8859_1), beforeContent);
  byte[] binaryContent = new byte[Byte.MAX_VALUE - Byte.MIN_VALUE + 1];
  batchResponse.read(binaryContent, 0, binaryContent.length);
  assertArrayEquals(content, binaryContent);
  final String afterExpected = CRLF
      + "--" + BOUNDARY + "--" + CRLF;
  byte[] afterContent = new byte[afterExpected.length()];
  batchResponse.read(afterContent, 0, afterExpected.length());
  assertArrayEquals(afterExpected.getBytes(CS_ISO_8859_1), afterContent);
  assertEquals(-1, batchResponse.read());
}