saker.util Documentation TaskDoc JavaDoc Packages
  1. saker.util
  2. Features
  3. Resource closing

Resource closing

There are many cases when a class holds multiple managed resources that need to be closed when the class itself is closed. The IOUtils class provides some utility functions that close the resources in a simpler way.

public class MyResourceHolder implements Closeable {
	private InputStream input;
	private ZipFile zip;
	private RandomAccessFile file;
	
	@Override
	public void close() throws IOException {
		IOUtils.close(input, zip, file);
	}
}

In the above, we close the resources in the class using a single function call. If we didn't use that, we would've needed to write multiple try-catch blocks:

try {
	input.close();
} finally {
	try {
		zip.close();
	} finally {
		file.close();
	}
}

And even in this case, if multiple close() calls fail, some of the exceptions may be not part of the actually thrown one.

There are workarounds for more cleaner code:

try (InputStream input = this.input;
		ZipFile zip = this.zip;
		RandomAccessFile file = this.file) {
}

However, we feel that using IOUtils.close is a more cleaner way of describing our intent.