Iam trying to save an InputStream from Primefaces UploadedFile into a String. 
The reason for this is, that I cant figure out how to persist an inputstream with hibernate.
Following Code is given:
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");
}
In my JoinExam I have following Attribute.
@Lob
@Column
private String savedDocument;
My Attention is to save the uploadedFile´s inputStream into my String savedDocument, which should "represent" my uploaded File.
When I want to download my "uploaded File" I want to get my savedDocument String and convert it back an inputStream.
The achieved inputStream will be used to create an DefaultStreamedContent
so I can download the File with primefaces <p:fileDownload /> -Tag.
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.
Thank you for any adivce
EDIT: When I upload and download .txt-Files it works, but it is not working with pdf-Files.
 
    