public void run() {
if(logMINOR) Logger.minor(this, "Starting worker thread for "+uri+" mime type "+mimeType+" filter data = "+filterData+" charset "+charset);
try {
//Validate the hash of the now decompressed data
input = new BufferedInputStream(input);
MultiHashInputStream hashStream = null;
if(hashes != null) {
hashStream = new MultiHashInputStream(input, HashResult.makeBitmask(hashes));
input = hashStream;
}
//Filter the data, if we are supposed to
if(filterData){
if(logMINOR) Logger.minor(this, "Running content filter... Prefetch hook: "+prefetchHook+" tagReplacer: "+tagReplacer);
if(mimeType == null || uri == null || input == null || output == null) throw new IOException("Insufficient arguements to worker thread");
// Send XHTML as HTML because we can't use web-pushing on XHTML.
FilterStatus filterStatus = ContentFilter.filter(input, output, mimeType, uri, prefetchHook, tagReplacer, charset, linkFilterExceptionProvider);
String detectedMIMEType = filterStatus.mimeType.concat(filterStatus.charset == null ? "" : "; charset="+filterStatus.charset);
synchronized(this) {
clientMetadata = new ClientMetadata(detectedMIMEType);
}
}
else {
if(logMINOR) Logger.minor(this, "Ignoring content filter. The final result has not been written. Writing now.");
FileUtil.copy(input, output, -1);
}
// Dump the rest.
try {
while(true) {
// FileInputStream.skip() doesn't do what we want. Use read().
// Note this is only necessary because we might have an AEADInputStream?
// FIXME get rid - they should check the end anyway?
byte[] buf = new byte[4096];
int r = input.read(buf);
if(r < 0) break;
}
} catch (EOFException e) {
// Okay.
}
input.close();
output.close();
if(hashes != null) {
HashResult[] results = hashStream.getResults();
if(!HashResult.strictEquals(results, hashes)) {
Logger.error(this, "Hashes failed verification (length read is "+hashStream.getReadBytes()+") "+" for "+uri);
throw new FetchException(FetchExceptionMode.CONTENT_HASH_FAILED);
}
}
onFinish();