1

I have a view, from which the user can first generate a PDF depending on some parameters, and then download and/or send it via mail.

Now, the method for generating the PDF file returns an InputStream, which I then store as field of the class, like this:

public class PDFWindow extends VerticalLayout { ... private InputStream pdfInputStream; ... private void createPDF() { this.pdfInputStream = pdfCreator.createPDF(); } 

My problem is, that the pdfInputStream is closed after it is consumed by the FileDownloader:

Button download = new Button("Download"); final FileDownloader fileDownloader = new FileDownloader( new StreamResource(() -> this.pdfInputStream, this.pdfFileName)); fileDownloader.extend(download); 

or the SpringEmailService I wrote:

SpringEmailService.send( "[email protected]", recipients, this.subject.getValue(), this.message.getValue(),this.pdfInputStream,"test.pdf", "application/pdf"); 

Is there any way, to stop the InputStream from being closed and then closing it manual, or should I look for a completely different way?

2
  • 3
    Just don't store an InputStream as a field. Ask the creator to create the stream every time you need one. If it's too expensive, then store the generated bytes in memory, or in a file, or in your database, or wherever you want. Commented Jul 6, 2018 at 8:51
  • Thanks a lot! I changed it so, that I store the byte Array as a field instead, and it works perfectly. Could you post an answer, so that I can accept it? Commented Jul 6, 2018 at 9:11

1 Answer 1

4

Just don't store an InputStream as a field.

Ask the creator to create the stream every time you need one.

If it's too expensive, then store the generated bytes in memory, or in a file, or in your database, or wherever you want, and create a stream over these bytes/file whenever you need one.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.