Serving a file (PipedStream and Job)

158 views
Skip to first unread message

cyrille giquello

unread,
Apr 23, 2011, 2:10:52 PM4/23/11
to play-fr...@googlegroups.com
Hi,

I've writing this code to serve a file export. I did not used await()
and family, but a piped stream and a Job.
I just try on my localhost and with single request, and it works ...
I'm happy ;-) But I'm afraid to follow a bad way ...

Could you please tell me if it's the right way ? Do you know a better
way to serve a file ?

public static void downloadExcel() {
try {
final PipedOutputStream pos = new PipedOutputStream();
final PipedInputStream pis = new PipedInputStream(pos);

new ExcelExportJob(pos).now();

response.contentType = "application/excel";

// doesn't work: java.io.IOException: Already connected
// response.direct = new PipedInputStream(pos);

renderBinary(pis, "toto.xls");

} catch (Exception ex) {
Logger.getLogger(Participants.class.getName()).log(Level.SEVERE, null, ex);
}
}

private static class ExcelExportJob extends Job {

private final BufferedOutputStream outputStream;

public ExcelExportJob(OutputStream outputStream) {
this.outputStream = new BufferedOutputStream(outputStream);
}

@Override
public void doJob() throws Exception {

FileExport.writeFile( this.outputStream, FileFormat.Excel );
this.outputStream.flush();
this.outputStream.close();
}
}

Thanks
Cyrille.

Guillaume Bort

unread,
Apr 24, 2011, 5:23:31 PM4/24/11
to play-fr...@googlegroups.com
It depends. If the available() method is not correctly implemented on
your stream, read() will be blocking and will probably block the whole
network thread. It is way better to make your job generate the file on
disk, and then send it.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

Reply all
Reply to author
Forward
0 new messages