Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeFaces 8.0 DefaultStreamedContent.builder().stream() asks for SerializableSupplier<InputStream>

In PrimeFaces 8.0 the DefaultStreamedContent cannot be initialized like new DefaultStreamedContent(inputStream, contentType, name) because it has been deprecated, instead you shound use DefaultStreamedContent.builder().

Although while doing .stream() it asks for a SerializableSupplier<InputStream> instead of an InputStream like in the version before 8.0.

DefaultStreamedContent.builder().contentType(contentType).name(name).stream(is).build();
                                                                            ^^

How can I convert a InputStream to a SerializableSupplier?

like image 475
Paolo Forgia Avatar asked Jan 03 '20 10:01

Paolo Forgia


1 Answers

Everthing is in the migration guide here: https://github.com/primefaces/primefaces/wiki/Migration-Guide.

in general the following will work:

DefaultStreamedContent.builder().contentType(contentType).name(name).stream(() -> is).build();

But the idea behind the change is a different.
If you use a RequestScoped bean to build the StreamedContent, your bean and therefore the StreamedContent will be created twice:

  1. when rendering the view
  2. when streaming the resource (this is a new browser request!)

In this case, your is will probably created 2 times. Most of the times this results in 1 useless IO access or DB call.

To only create the is one time, you should lazy initialize it via the supplier lambda:

DefaultStreamedContent.builder().contentType(contentType).name(name).stream(() -> new FileInputStream(....)).build();
like image 158
tandraschko Avatar answered Sep 29 '22 11:09

tandraschko