Sergey,

You are correct that is does look a little weird and that the spec is not
exactly clear on the lifecycle of the buffer.

I think a lot of other containers just always do the copy, but we like to
avoid that if possible so we have taken the approach that "ownership" of
the buffer is effectively passed to jetty once the write call is made.  The
"ownership" is only passed back to the caller once they know the write is
complete.... and they only know the write is complete if they are told by
isReady() that another write can be performed.


Note that you can structure your code so that it does not looks so strange
as typically the writes should be done in a loop something like:

void onWritePossible()
{
  while (out.isReady())
  {
     if (prepareSomeContent(buffer))
       out.write(buffer);
     else
       returnBuffer(buffer);
   }
}

So a buffer is only reused just before the next write is attempted.

It is a compromise in complexity, but I think it is the best we can do with
the API and the benefits of avoiding copies are worth it.


cheers


On 30 November 2016 at 01:59, Sergey Mashkov <[email protected]>
wrote:

> Hi all
>
> In async mode servlet output stream write method receives a buffer and
> copies it to _aggregate buffer or schedules write job. However in the
> second case there is no way to detect if I can use the buffer again as
> there is no async write task callback (except flush that is not an option
> in fact). The other possible solution is to check isReady() after write
> operation to get onWritePossible() callback every time but it looks
> slightly weird and looks like this:
>
> bufferLocked = true
> if (stream.isReady) {
>     stream.write(buffer, 0, size)
>     if (stream.isReady) {
>         // mark buffer is free and ready for reuse
>         bufferLocked = false
>     }
> }
>
> From the specification it's not exactly clear what is a lifecycle of a
> buffer provided to write method. Am I missing something?
>
>
> _______________________________________________
> jetty-users mailing list
> [email protected]
> To change your delivery options, retrieve your password, or unsubscribe
> from this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users
>



-- 
Greg Wilkins <[email protected]> CTO http://webtide.com
_______________________________________________
jetty-users mailing list
[email protected]
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users

Reply via email to