Allow Archives to be closed so they can release resources

Closes gh-17126
This commit is contained in:
Andy Wilkinson 2019-06-12 16:33:39 +01:00
parent 4748626f45
commit c56fbf8c3d
2 changed files with 16 additions and 1 deletions

View File

@ -30,7 +30,7 @@ import org.springframework.boot.loader.Launcher;
* @author Phillip Webb
* @see JarFileArchive
*/
public interface Archive extends Iterable<Archive.Entry> {
public interface Archive extends Iterable<Archive.Entry>, AutoCloseable {
/**
* Returns a URL that can be used to load the archive.
@ -54,6 +54,16 @@ public interface Archive extends Iterable<Archive.Entry> {
*/
List<Archive> getNestedArchives(EntryFilter filter) throws IOException;
/**
* Closes the {@code Archive}, releasing any open resources.
* @throws Exception if an error occurs during close processing
* @since 2.2.0
*/
@Override
default void close() throws Exception {
}
/**
* Represents a single entry in the archive.
*/

View File

@ -94,6 +94,11 @@ public class JarFileArchive implements Archive {
return new EntryIterator(this.jarFile.entries());
}
@Override
public void close() throws IOException {
this.jarFile.close();
}
protected Archive getNestedArchive(Entry entry) throws IOException {
JarEntry jarEntry = ((JarFileEntry) entry).getJarEntry();
if (jarEntry.getComment().startsWith(UNPACK_MARKER)) {