jsr340-experts@servlet-spec.java.net

[jsr340-experts] Re: Issue with async writes and auto-close.

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Mon, 03 Feb 2014 15:43:43 -0800

+1 to return true for closed ServletOutputStreams and ServletInputStream.

There is one inconsistency in ServletInputStream and ServletOutputStream
docs:

ServletInputStream: "<code>true</code> if data can be *obtained without
blocking*, otherwise returns <code>false</code>."
vs.
ServletOutputStream: "<code>true</code> if a write to this
<code>ServletOutputStream</code> will *succeed*, otherwise returns
<code>false</code>."

IMO "without blocking" sounds better, because it doesn't imply any
success :)

One more point, assume HTTP POST usecase. On the server side we try to
read the POST body, at some point of time isReady() returns false and
connection is getting terminated unexpectedly by the client side. What
should happen? AFAIK in Glassfish we'll call
ReadListener.onDataAvailable() and consequent InputStream.read* call
will fail (return -1 or throw EOF/IOException).

Thank you.

WBR,
Alexey.

On 03.02.14 15:00, Greg Wilkins wrote:
>
> Consider the following simple async writer
>
>
> volatile boolean written=false;
> @Override
> public void onWritePossible() throws IOException
> {
> if (!written)
> {
> written=true;
> response.setContentLength(5);
> servletOutputStream.write("data\n".getBytes());
> }
>
> if (servletOutputStream.isReady())
> {
> asyncContext.complete();
> }
> }
>
> Because we set a content-length, then the write of 5 bytes is required
> to close the output stream when the write completes.
>
> If the write completes during it's call, what should isReady()
> return? True even though the output stream is closed and no more
> data can be written? False even though that will be interpreted by
> the caller as saying that onWritePossible() will be called later when
> a write is possible (but it never will be!)
>
> If the write down not complete during the call to write, then
> obviously isReady() must return false and call onWritePossible() once
> the write has completed. In that case, what should isReady() return?
> true even though no data can be written or false which means that yet
> another callback to owp will be scheduled!
>
> I think isReady should return true for a closed outputstream - a bit
> strange, but will result in the simplest code.
>
> thoughts?
>
>
>
>
>
>
> --
> Greg Wilkins <gregw_at_intalio.com <mailto:gregw_at_intalio.com>>
> http://www.webtide.com
> Developer advice and support from the Jetty & CometD experts.
> Intalio, the modern way to build business applications.