title: How to convert InputStream to a PDF in Java, without damaging the file?
body: <p>I have an InputStream which I would like to convert to a PDF, and save that PDF in a directory. Currently, my code is able to convert the InputStream to a PDF and the PDF does show up in the correct directory. However, when I try to open it, the file is damaged. </p>
  
  <p>Here is the current code:</p>
  
  <pre><code> InputStream pAdESStream = signingServiceConnector.getDirectClient().getPAdES(this.statusReader.getStatusResponse().getpAdESUrl()); 
              byte[] buffer = new byte[pAdESStream.available()];
              pAdESStream.read(buffer);
  
              File targetFile = new File(System.getProperty("user.dir") + "targetFile2.pdf");
              OutputStream outStream = new FileOutputStream(targetFile);
              outStream.write(buffer);
  </code></pre>
  
  <p>Originally, the InputStream was a pAdES-file (<a href="https://en.wikipedia.org/wiki/PAdES" rel="nofollow">https://en.wikipedia.org/wiki/PAdES</a>). However, it should be able to be read as just a regular PDF.</p>
  
  <p>Does anyone know how to convert the InputStream to a PDF, without getting a damaged PDF as a result?</p>