Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of deprecated freenet.support.io.Closer #830

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 49 additions & 51 deletions src/freenet/client/ArchiveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
import freenet.support.MutableBoolean;
import freenet.support.api.Bucket;
import freenet.support.api.BucketFactory;
import freenet.support.compress.CompressionOutputSizeException;
import freenet.support.compress.Compressor;
import freenet.support.compress.Compressor.COMPRESSOR_TYPE;
import freenet.support.io.BucketTools;
import freenet.support.io.Closer;
import freenet.support.io.SkipShieldingInputStream;
import net.contrapunctus.lzma.LzmaInputStream;

Expand Down Expand Up @@ -263,7 +261,7 @@ public void extractToCache(FreenetURI key, ARCHIVE_TYPE archiveType, COMPRESSOR_
ctx.removeAllCachedItems(this); // flush cache anyway
final long expectedSize = ctx.getLastSize();
final long archiveSize = data.size();
/** Set if we need to throw a RestartedException rather than returning success,
/* Set if we need to throw a RestartedException rather than returning success,
* after we have unpacked everything.
*/
boolean throwAtExit = false;
Expand Down Expand Up @@ -312,31 +310,21 @@ else if(logMINOR)
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pis.connect(pos);
final OutputStream os = new BufferedOutputStream(pos);

wrapper = new ExceptionWrapper();
context.mainExecutor.execute(new Runnable() {

@Override
public void run() {
InputStream is = null;
try {
Compressor.COMPRESSOR_TYPE.LZMA_NEW.decompress(is = data.getInputStream(), os, data.size(), expectedSize);
} catch (CompressionOutputSizeException e) {
Logger.error(this, "Failed to decompress archive: "+e, e);
wrapper.set(e);
try (
InputStream is = data.getInputStream();
OutputStream os = new BufferedOutputStream(pos)
){
Compressor.COMPRESSOR_TYPE.LZMA_NEW.decompress(is, os, data.size(), expectedSize);
} catch (IOException e) {
Logger.error(this, "Failed to decompress archive: "+e, e);
wrapper.set(e);
} finally {
try {
os.close();
} catch (IOException e) {
Logger.error(this, "Failed to close PipedOutputStream: "+e, e);
}
Closer.close(is);
}
}

});
is = pis;
} else if(ctype == COMPRESSOR_TYPE.LZMA) {
Expand All @@ -347,55 +335,63 @@ public void run() {
wrapper = null;
}

if(ARCHIVE_TYPE.ZIP == archiveType) {
handleZIPArchive(ctx, key, is, element, callback, gotElement, throwAtExit, context);
} else if(ARCHIVE_TYPE.TAR == archiveType) {
// COMPRESS-449 workaround, see https://freenet.mantishub.io/view.php?id=6921
handleTARArchive(ctx, key, new SkipShieldingInputStream(is), element, callback, gotElement, throwAtExit, context);
} else {
throw new ArchiveFailureException("Unknown or unsupported archive algorithm " + archiveType);
try (InputStream archiveInputStream = is) {
if (ARCHIVE_TYPE.ZIP == archiveType) {
handleZIPArchive(ctx, key, archiveInputStream, element, callback, gotElement, throwAtExit, context);
} else if (ARCHIVE_TYPE.TAR == archiveType) {
// COMPRESS-449 workaround, see https://freenet.mantishub.io/view.php?id=6921
handleTARArchive(ctx, key, new SkipShieldingInputStream(archiveInputStream), element, callback, gotElement, throwAtExit, context);
} else {
throw new ArchiveFailureException("Unknown or unsupported archive algorithm " + archiveType);
}
}
if(wrapper != null) {
if (wrapper != null) {
Exception e = wrapper.get();
if(e != null) throw new ArchiveFailureException("An exception occured decompressing: "+e.getMessage(), e);
if (e != null) {
throw new ArchiveFailureException("An exception occurred decompressing: " + e.getMessage(), e);
}
}
} catch (IOException ioe) {
throw new ArchiveFailureException("An IOE occured: "+ioe.getMessage(), ioe);
} finally {
Closer.close(is);
}
throw new ArchiveFailureException("An IOE occurred: "+ioe.getMessage(), ioe);
}
}

private void handleTARArchive(ArchiveStoreContext ctx, FreenetURI key, InputStream data, String element, ArchiveExtractCallback callback, MutableBoolean gotElement, boolean throwAtExit, ClientContext context) throws ArchiveFailureException, ArchiveRestartException {
if(logMINOR) Logger.minor(this, "Handling a TAR Archive");
TarArchiveInputStream tarIS = null;
try {
tarIS = new TarArchiveInputStream(data);

if(logMINOR) {
Logger.minor(this, "Handling a TAR Archive");
}
try(
TarArchiveInputStream tarIS = new TarArchiveInputStream(data);
){
// MINOR: Assumes the first entry in the tarball is a directory.
ArchiveEntry entry;

byte[] buf = new byte[32768];
HashSet<String> names = new HashSet<String>();
Set<String> names = new HashSet<>();
boolean gotMetadata = false;

outerTAR: while(true) {
try {
entry = tarIS.getNextEntry();
entry = tarIS.getNextEntry();
} catch (IllegalArgumentException e) {
// Annoyingly, it can throw this on some corruptions...
throw new ArchiveFailureException("Error reading archive: "+e.getMessage(), e);
}
if(entry == null) break;
if(entry.isDirectory()) continue;
if(entry == null) {
break;
}
if(entry.isDirectory()) {
continue;
}
String name = stripLeadingSlashes(entry.getName());
if(names.contains(name)) {
Logger.error(this, "Duplicate key "+name+" in archive "+key);
continue;
}
long size = entry.getSize();
if(name.equals(".metadata"))
if(name.equals(".metadata")) {
gotMetadata = true;
}
if(size > maxArchivedFileSize && !name.equals(element)) {
addErrorElement(ctx, key, name, "File too big: "+size+" greater than current archived file size limit "+maxArchivedFileSize, true);
} else {
Expand All @@ -417,9 +413,11 @@ private void handleTARArchive(ArchiveStoreContext ctx, FreenetURI key, InputStre
continue outerTAR;
}
}

} finally {
if(out != null) out.close();
if(out != null) {
out.close();
}
}
if(size <= maxArchivedFileSize) {
addStoreElement(ctx, key, name, output, gotElement, element, callback, context);
Expand All @@ -439,15 +437,15 @@ private void handleTARArchive(ArchiveStoreContext ctx, FreenetURI key, InputStre
generateMetadata(ctx, key, names, gotElement, element, callback, context);
trimStoredData();
}
if(throwAtExit) throw new ArchiveRestartException("Archive changed on re-fetch");
if(throwAtExit) {
throw new ArchiveRestartException("Archive changed on re-fetch");
}

if((!gotElement.value) && element != null)
if((!gotElement.value) && element != null) {
callback.notInArchive(context);

}
} catch (IOException e) {
throw new ArchiveFailureException("Error reading archive: "+e.getMessage(), e);
} finally {
Closer.close(tarIS);
}
}

Expand Down Expand Up @@ -484,7 +482,7 @@ private void handleZIPArchive(ArchiveStoreContext ctx, FreenetURI key, InputStre
Bucket output = tempBucketFactory.makeBucket(size);
OutputStream out = output.getOutputStream();
try {

int readBytes;
while((readBytes = zis.read(buf)) > 0) {
out.write(buf, 0, readBytes);
Expand All @@ -497,7 +495,7 @@ private void handleZIPArchive(ArchiveStoreContext ctx, FreenetURI key, InputStre
continue outerZIP;
}
}

} finally {
if(out != null) out.close();
}
Expand Down
29 changes: 11 additions & 18 deletions src/freenet/client/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import freenet.support.api.BucketFactory;
import freenet.support.api.RandomAccessBucket;
import freenet.support.compress.Compressor.COMPRESSOR_TYPE;
import freenet.support.io.Closer;
import freenet.support.io.CountedOutputStream;
import freenet.support.io.NullOutputStream;

Expand Down Expand Up @@ -1228,16 +1227,13 @@ public byte[] writeToByteArray() throws MetadataUnresolvedException {
}

public long writtenLength() throws MetadataUnresolvedException {
CountedOutputStream cos = new CountedOutputStream(new NullOutputStream());
DataOutputStream dos = null;
try {
dos = new DataOutputStream(cos);
CountedOutputStream cos = new CountedOutputStream(new NullOutputStream());
try (
DataOutputStream dos = new DataOutputStream(cos);
){
writeTo(dos);
} catch (IOException e) {
throw new Error("Could not write to CountedOutputStream: "+e, e);
} finally {
Closer.close(dos);
Closer.close(cos);
throw new RuntimeException("Could not write to CountedOutputStream: "+e, e);
}
return cos.written();
}
Expand Down Expand Up @@ -1687,20 +1683,17 @@ public void resolve(String name) {

public RandomAccessBucket toBucket(BucketFactory bf) throws MetadataUnresolvedException, IOException {
RandomAccessBucket b = bf.makeBucket(-1);
DataOutputStream dos = null;
boolean success = false;
try {
dos = new DataOutputStream(b.getOutputStream());
try (DataOutputStream dos = new DataOutputStream(b.getOutputStream())) {
writeTo(dos);
dos.close();
dos = null;
b.setReadOnly(); // Must be after dos.close()
success = true;
return b;
} finally {
Closer.close(dos);
if(!success) b.free();
if(!success) {
b.free();
}
}
b.setReadOnly();
return b;
}

public boolean isResolved() {
Expand Down
56 changes: 32 additions & 24 deletions src/freenet/client/async/ClientGetWorkerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

package freenet.client.async;

import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

Expand All @@ -26,7 +22,6 @@
import freenet.keys.FreenetURI;
import freenet.support.Logger;
import freenet.support.compress.CompressionOutputSizeException;
import freenet.support.io.Closer;
import freenet.support.io.FileUtil;

/**A thread which does postprocessing of decompressed data, in particular,
Expand All @@ -35,7 +30,7 @@
* the relevant constructor arguments.*/
public class ClientGetWorkerThread extends Thread {

private InputStream input;
private final InputStream input;
final private String schemeHostAndPort;
final private URI uri;
final private HashResult[] hashes;
Expand All @@ -48,7 +43,7 @@ public class ClientGetWorkerThread extends Thread {
private final LinkFilterExceptionProvider linkFilterExceptionProvider;

final private String mimeType;
private OutputStream output;
private final OutputStream output;
private boolean finished = false;
private Throwable error = null;
private ClientMetadata clientMetadata = null;
Expand Down Expand Up @@ -96,14 +91,21 @@ public ClientGetWorkerThread(InputStream input, OutputStream output, FreenetURI
String mimeType, String schemeHostAndPort, HashResult[] hashes, boolean filterData, String charset,
FoundURICallback prefetchHook, TagReplacerCallback tagReplacer, LinkFilterExceptionProvider linkFilterExceptionProvider) throws URISyntaxException {
super("ClientGetWorkerThread-"+counter());
if (input == null) {
throw new IllegalArgumentException("Input stream is missing");
}
if (output == null) {
throw new IllegalArgumentException("Output stream is missing");
}
this.input = input;
this.output = output;
if(uri != null) this.uri = uri.toURI("/");
else this.uri = null;
if(mimeType != null && mimeType.compareTo("application/xhtml+xml") == 0) mimeType = "text/html";
this.mimeType = mimeType;
this.schemeHostAndPort = schemeHostAndPort;
this.hashes = hashes;
this.output = output;

this.filterData = filterData;
this.charset = charset;
this.prefetchHook = prefetchHook;
Expand All @@ -114,21 +116,30 @@ public ClientGetWorkerThread(InputStream input, OutputStream output, FreenetURI

@Override
public void run() {
if(logMINOR) Logger.minor(this, "Starting worker thread for "+uri+" mime type "+mimeType+" filter data = "+filterData+" charset "+charset);
try {
if (logMINOR) {
Logger.minor(this, "Starting worker thread for "+uri+" mime type "+mimeType+" filter data = "+filterData+" charset "+charset);
}
try (
OutputStream outputStream = this.output;
InputStream is = this.input
){
//Validate the hash of the now decompressed data
input = new BufferedInputStream(input);
InputStream inputStream = new BufferedInputStream(is);
MultiHashInputStream hashStream = null;
if(hashes != null) {
hashStream = new MultiHashInputStream(input, HashResult.makeBitmask(hashes));
input = hashStream;
hashStream = new MultiHashInputStream(inputStream, HashResult.makeBitmask(hashes));
inputStream = 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");
if(logMINOR) {
Logger.minor(this, "Running content filter... Prefetch hook: "+prefetchHook+" tagReplacer: "+tagReplacer);
}
if(mimeType == null || uri == 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,
FilterStatus filterStatus = ContentFilter.filter(inputStream, outputStream, mimeType, uri,
schemeHostAndPort, prefetchHook, tagReplacer, charset, linkFilterExceptionProvider);

String detectedMIMEType = filterStatus.mimeType.concat(filterStatus.charset == null ? "" : "; charset="+filterStatus.charset);
Expand All @@ -138,7 +149,7 @@ public void run() {
}
else {
if(logMINOR) Logger.minor(this, "Ignoring content filter. The final result has not been written. Writing now.");
FileUtil.copy(input, output, -1);
FileUtil.copy(inputStream, outputStream, -1);
}
// Dump the rest.
try {
Expand All @@ -147,14 +158,14 @@ public void run() {
// 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);
int r = inputStream.read(buf);
if(r < 0) break;
}
} catch (EOFException e) {
// Okay.
}
input.close();
output.close();
inputStream.close();
outputStream.close();
if(hashes != null) {
HashResult[] results = hashStream.getResults();
if(!HashResult.strictEquals(results, hashes)) {
Expand All @@ -170,9 +181,6 @@ public void run() {
else if(logMINOR)
Logger.minor(this, "Exception caught while processing fetch: "+t,t);
setError(t);
} finally {
Closer.close(input);
Closer.close(output);
}
}

Expand Down
Loading