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 // send "medium" files from an input stream
if (file.length()<MEDIUM) if (file.length()<MEDIUM)
{ {
// the file channel is closed by the async send
((HttpOutput)response.getOutputStream()).sendContent(FileChannel.open(file.toPath(),StandardOpenOption.READ),completionCB); ((HttpOutput)response.getOutputStream()).sendContent(FileChannel.open(file.toPath(),StandardOpenOption.READ),completionCB);
return; return;
} }

View File

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