title: Convert Inputstream from an UploadedFile from Primefaces to a String
body: <p>Iam trying to save an InputStream from Primefaces <code>UploadedFile</code> into a String. 
  The reason for this is, that I cant figure out how to persist an inputstream with hibernate.</p>
  
  <p>Following Code is given:</p>
  
  <pre><code>public String saveDocumentInDatabase(final UploadedFile pFile) throws IOException{
      InputStream inputStream = pFile.getInputstream();
  
      byte[] buffer = new byte[8192];
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      int bytesRead;
      while ((bytesRead = inputStream.read(buffer)) != -1)
      {
          baos.write(buffer, 0, bytesRead);
      }
      byte[] tmp = baos.toByteArray();
      return new String(tmp, StandardCharsets.UTF_8);
  }
  
  
  public StreamedContent downloadUploadedFile(final JoinExam pJoinExam){
      assertNotNull(pJoinExam.getSavedDocument());
  
      String tmp = pJoinExam.getSavedDocument().trim();
      java.io.InputStream inputStream = null;
      try {
          inputStream = new ByteArrayInputStream(tmp.getBytes("UTF-8"));
      } catch (UnsupportedEncodingException ex) {
          ex.printStackTrace(System.err);
      }
  
      return new DefaultStreamedContent(inputStream, "application/pdf",
              "Protocol" + pJoinExam.getPruefling().getMatrNr()+ ".pdf");
  }
  </code></pre>
  
  <p>In my JoinExam I have following Attribute.</p>
  
  <pre><code>@Lob
  @Column
  private String savedDocument;
  </code></pre>
  
  <p>My Attention is to save the uploadedFile´s inputStream into my String <code>savedDocument</code>, which should "represent" my uploaded File.
  When I want to download my "uploaded File" I want to get my <code>savedDocument</code> String and convert it back an inputStream.
  The achieved inputStream will be used to create an <code>DefaultStreamedContent</code>
  so I can download the File with primefaces <code>&lt;p:fileDownload /&gt;</code> -Tag.</p>
  
  <p>My problem is, that the inputstream seems to lose some data while converting into a String or the String seems to loose some data while converting back to the inputstream.
  When I download the File and try to open it, an error pops up, saying that the filed could not be opened. </p>
  
  <p>Thank you for any adivce</p>
  
  <p>EDIT:  When I upload and download .txt-Files it works, but it is not working with pdf-Files.</p>