title: Spring REST - Damaged/Blank File being downloaded
body: <p>I am trying to create a endpoint to render/serve PDF file.
  I have gone through the following links to build the API, but still facing some issues.</p>
  
  <p><a href="https://stackoverflow.com/questions/41415602/spring-rest-swagger-postman-damaged-blank-file-being-dowloaded?answertab=active#tab-top">link 1</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16652760/return-generated-pdf-using-spring-mvc">link 2</a></p>
  
  <p>Following is my code :</p>
  
  <pre><code>byte[] targetArray = null;
  
  InputStream is = null;
  
  InputStream objectData = object.getObjectContent();
  
  BufferedReader reader = new BufferedReader(new InputStreamReader(objectData));
  
  char[] charArray = new char[8 * 1024];
  StringBuilder builder = new StringBuilder();
  int numCharsRead;
  while ((numCharsRead = reader.read(charArray, 0, charArray.length)) != -1) {
  
      builder.append(charArray, 0, numCharsRead);
  }
  reader.close();
  
  objectData.close();
  object.close();
  targetArray = builder.toString().getBytes();
  
  is = new ByteArrayInputStream(targetArray);
  
  
  return ResponseEntity.ok().contentLength(targetArray.length).contentType(MediaType.APPLICATION_PDF)
                  .cacheControl(CacheControl.noCache()).header("Content-Disposition", "attachment; filename=" + "testing.pdf")
                  .body(new InputStreamResource(is));
  </code></pre>
  
  <p>When I hit my API using postman, I am able to download PDF file but the problem is it is totally blank. What might be the issue ?</p>