code: protected void sendBinaryData(byte[] binaryData, String contentType,
        SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
  
      // set content type and length
      response.setContentType(contentType);
      response.setContentLength(binaryData.length);
  
      // Handling of the "force download" selector
      if (RequestPath.hasSelector(request, SELECTOR_DOWNLOAD)) {
        // Overwrite MIME type with one suited for downloads
        response.setContentType(ContentType.DOWNLOAD);
  
        // Construct disposition header
        StringBuilder dispositionHeader = new StringBuilder("attachment;");
        String suffix = request.getRequestPathInfo().getSuffix();
        suffix = StringUtils.stripStart(suffix, "/");
        if (StringUtils.isNotEmpty(suffix)) {
          dispositionHeader.append("filename=\"").append(suffix).append('\"');
        }
  
        response.setHeader(HEADER_CONTENT_DISPOSITION, dispositionHeader.toString());
      }
  
      // write binary data
      OutputStream out = response.getOutputStream();
      out.write(binaryData);
      out.flush();
  
    }
docs: Send binary data to output stream. Respect optional content disposition header handling.
  @param binaryData Binary data array.
  @param contentType Content type
  @param request Request
  @param response Response
  @throws IOException