416102 - Clean up of async sendContent process

This commit is contained in:
Greg Wilkins 2013-08-30 14:37:43 +10:00
parent 9bf83d427b
commit 47c679dabb
3 changed files with 16 additions and 28 deletions

View File

@ -155,6 +155,7 @@ public class FastFileServer
// send "medium" files from an input stream
if (file.length()<MEDIUM)
{
// the file channel is closed by the async send
((HttpOutput)response.getOutputStream()).sendContent(FileChannel.open(file.toPath(),StandardOpenOption.READ),completionCB);
return;
}

View File

@ -420,6 +420,7 @@ public class HttpOutput extends ServletOutputStream
ReadableByteChannel rbc=httpContent.getReadableByteChannel();
if (rbc!=null)
{
// Close of the rbc is done by the async sendContent
sendContent(rbc,callback);
return;
}
@ -475,6 +476,8 @@ public class HttpOutput extends ServletOutputStream
@Override
protected boolean process() throws Exception
{
// Only return if EOF has previously been read and thus
// a write done with EOF=true
if (_eof)
{
// Handle EOF
@ -484,21 +487,15 @@ public class HttpOutput extends ServletOutputStream
return true;
}
int len=_in.read(_buffer.array(),0,_buffer.capacity());
if (len<0)
// Read until buffer full or EOF
int len=0;
while (len<_buffer.capacity() && !_eof)
{
_eof=true;
len=0;
}
else if (len<_buffer.capacity())
{
// read ahead for EOF to try for single commit
int len2=_in.read(_buffer.array(),len,_buffer.capacity()-len);
if (len2<0)
int r=_in.read(_buffer.array(),0,_buffer.capacity()-len);
if (r<0)
_eof=true;
else
len+=len2;
len+=r;
}
// write what we have
@ -550,6 +547,8 @@ public class HttpOutput extends ServletOutputStream
@Override
protected boolean process() throws Exception
{
// Only return if EOF has previously been read and thus
// a write done with EOF=true
if (_eof)
{
_in.close();
@ -558,23 +557,10 @@ public class HttpOutput extends ServletOutputStream
return true;
}
// Read from stream until buffer full or EOF
_buffer.clear();
int len=_in.read(_buffer);
if (len<0)
{
_eof=true;
len=0;
}
else if (len<_buffer.capacity())
{
// read ahead for EOF to try for single commit
int len2=_in.read(_buffer);
if (len2<0)
_eof=true;
else
len+=len2;
}
while (_buffer.hasRemaining() && !_eof)
_eof = (_in.read(_buffer)) < 0;
// write what we have
_buffer.flip();

View File

@ -538,6 +538,7 @@ public class ResourceHandler extends HandlerWrapper
}
else // Do a blocking write of a channel (if available) or input stream
{
// Close of the channel/inputstream is done by the async sendContent
ReadableByteChannel channel= resource.getReadableByteChannel();
if (channel!=null)
((HttpOutput)out).sendContent(channel,callback);