geg
geg

Reputation: 55

Is StreamingOutput in Jersey buffered?

I have two questions regarding StreamingOutput in Jersey:

1) Is it already buffered by the jax-rs runtime? I have seen some examples that create BufferedWriter from the OutputStream object when overriding the write() method. But I wonder if that is really necessary.

2) Does Jersey or the jax-rs runtime for that matter close the OutputStream object after the stream is finished?

Thanks,

Geg

Upvotes: 1

Views: 1656

Answers (1)

kartik
kartik

Reputation: 2135

For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example: http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html

import javax.ws.rs.core.StreamingOutput;
StreamingOutput stream = new StreamingOutput() {
                @Override
                public void write(OutputStream os) throws IOException,
                        WebApplicationException {
                        Writer writer = new BufferedWriter(new OutputStreamWriter(os));
                        writer.write(msg);
                        writer.flush();
                }
            };

Upvotes: 1

Related Questions