I am trying to write a controller to download binary file. 

Source code follows:

```
  public @ResponseBody byte[] getOpenedEventsInPdf(HttpServletResponse response)
        throws IOException, URISyntaxException {

    ClassPathResource pdfFile = new ClassPathResource(SRC_FILE);
    HttpHeaders headers = new HttpHeaders();
    response.addHeader("Content-Type", "application/pdf");
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
    response.addHeader("Access-Control-Allow-Headers", "Content-Type");
    response.addHeader("Content-Disposition", "filename=" + FILE_NAME);
    response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    response.addHeader("Pragma", "no-cache");
    response.addHeader("Expires", "0");

    headers.setContentLength(pdfFile.contentLength());

    byte[] toRet = toByteArray(new DataInputStream(
            new FileInputStream(new File(this.getClass().getClassLoader().getResource(SRC_FILE).toURI()))));
    byte[] bte = { (byte) 128, 65, (byte) 129, 65, (byte) 130 };
    System.out.println(toRet[0] & 0xFF);
    System.out.println(toRet[0]);
    System.out.println(toRet.length);
//        return toRet ;
    return bte;
}

```

But the problem is if I return bte, i am getting exactly 5 bytes
If I return toRet, i get 509 bytes. But the source FILE\_NAME contains 256 bytes, starting from 0-255.

I am guessing following line has something to do.

```
byte[] toRet = toByteArray(new DataInputStream(
        new FileInputStream(new File(this.getClass().getClassLoader().getResource(SRC_FILE).toURI()))));

```

Because values above 127 got modified. What am I missing?

-- requested code from comment --

```
private byte[] listToByteArray(List<byte[]> input) {
    if (input == null || input.size() == 0) {
        return new byte[] {};
    }
    byte[] toRet = {};
    for (byte[] b : input) {
        toRet = ArrayUtils.addAll(toRet, b);
    }
    return toRet;
}

private byte[] toByteArray(InputStream is) throws IOException {
    List<byte[]> byteArrayList = new ArrayList<byte[]>();
    while (true) {
        byte[] buff = new byte[READ_BYTE_LENGTH];
        int r = is.read(buff);
        if (r == -1) {
            break;
        } else if (r == READ_BYTE_LENGTH) {
            byteArrayList.add(buff);
        } else {
            byteArrayList.add(Arrays.copyOf(buff, r));
        }
    }
    return listToByteArray(byteArrayList);
}

```

If you wander why I did this madness when guava or apache common lang offer all the available? It's because I am going nuts over this