final String host = url.getHost();
if (byteRangeTestMap.containsKey(host)) {
return byteRangeTestMap.get(host);
} else {
SeekableStream str = null;
try {
boolean byteRangeTestSuccess = true;
if (host.contains("broadinstitute.org")) {
byteRangeTestSuccess = testBroadHost(host);
} else {
// Non-broad URL
int l = (int) Math.min(1000, HttpUtils.getInstance().getContentLength(url));
if (l > 100) {
byte[] firstBytes = new byte[l];
str = new IGVSeekableHTTPStream(url);
str.readFully(firstBytes);
int end = firstBytes.length;
int start = end - 100;
str.seek(start);
int len = end - start;
byte[] buffer = new byte[len];
int n = 0;
while (n < len) {
int count = str.read(buffer, n, len - n);
if (count < 0)
throw new EOFException();
n += count;
}
for (int i = 0; i < len; i++) {
if (buffer[i] != firstBytes[i + start]) {
byteRangeTestSuccess = false;
break;
}
}
} else {
// Too small a sample to test, or unknown content length. Return "true" but don't record
// this host as tested.
return true;
}
}
if (byteRangeTestSuccess) {
log.info("Range-byte request succeeded");
} else {
log.info("Range-byte test failed -- problem with client network environment.");
}
byteRangeTestMap.put(host, byteRangeTestSuccess);
return byteRangeTestSuccess;
} catch (IOException e) {
log.error("Error while testing byte range " + e.getMessage());
// We could not reach the test server, so we can't know if this client can do byte-range tests or
// not. Take the "optimistic" view.
return true;
} finally {
if (str != null) try {
str.close();
} catch (IOException e) {
log.error("Error closing stream (" + url.toExternalForm() + ")", e);
}
}
}