byte[] buffer = new byte[bufferSize];
int length = -1;
String ranges = request.getHeader("Ranges");
ByteRange byteRange;
if (ranges != null && byteRanges) {
try {
ranges = ranges.substring(ranges.indexOf('=') + 1);
byteRange = new ByteRange(ranges);
} catch (NumberFormatException e) {
byteRange = null;
// TC: Hm.. why don't we have setStatus in the Response interface ?
if (response instanceof HttpResponse) {
// Respond with status 416 (Request range not satisfiable)
((HttpResponse)response).setStatus(416);
if (getLogger().isDebugEnabled()) {
getLogger().debug("malformed byte range header [" + String.valueOf(ranges) + "]");
}
}
}
}
else {
byteRange = null;
}
long contentLength = inputSource.getContentLength();
if (byteRange != null) {
String entityLength;
String entityRange;
if (contentLength != -1) {
entityLength = "" + contentLength;
entityRange = byteRange.intersection(new ByteRange(0, contentLength)).toString();
} else {
entityLength = "*";
entityRange = byteRange.toString();
}
response.setHeader("Content-Range", entityRange + "/" + entityLength);
if (response instanceof HttpResponse) {
// Response with status 206 (Partial content)
((HttpResponse)response).setStatus(206);
}
response.setHeader("Accept-Ranges", "bytes");
int pos = 0;
int posEnd;
while ((length = inputStream.read(buffer)) > -1) {
posEnd = pos + length - 1;
ByteRange intersection = byteRange.intersection(new ByteRange(pos, posEnd));
if (intersection != null) {
out.write(buffer, (int) intersection.getStart() - pos, (int) intersection.length());
}
pos += length;
}
}
else {