jsr340-experts@servlet-spec.java.net

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

From: Rémy Maucherat <rmaucher_at_redhat.com>
Date: Tue, 04 Feb 2014 19:06:28 +0100

On 04/02/2014 00: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?
>
+1, but what about input for consistency ? ServletInputStream.isReady
could probably continue to return true after close (or eof, etc).

Rémy