372190: HttpContent.getIndirectBuffer() close InputStream

This commit is contained in:
Thomas Becker 2012-02-22 13:54:01 +01:00 committed by Jesse McConnell
parent 7fe5454888
commit 94c979f2b2

View File

@ -18,6 +18,8 @@ import java.io.InputStream;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
/* ------------------------------------------------------------ */
@ -41,6 +43,8 @@ public interface HttpContent
/* ------------------------------------------------------------ */
public class ResourceAsHttpContent implements HttpContent
{
private static final Logger LOG = Log.getLogger(ResourceAsHttpContent.class);
final Resource _resource;
final Buffer _mimeType;
final int _maxBuffer;
@ -80,18 +84,31 @@ public interface HttpContent
/* ------------------------------------------------------------ */
public Buffer getIndirectBuffer()
{
InputStream inputStream = null;
try
{
if (_resource.length() <= 0 || _maxBuffer < _resource.length())
return null;
ByteArrayBuffer buffer = new ByteArrayBuffer((int)_resource.length());
buffer.readFrom(_resource.getInputStream(),(int)_resource.length());
inputStream = _resource.getInputStream();
buffer.readFrom(inputStream,(int)_resource.length());
return buffer;
}
catch (IOException e)
{
throw new RuntimeException(e);
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
LOG.warn("Couldn't close inputStream. Possible file handle leak",e);
}
}
}
/* ------------------------------------------------------------ */