From b4e6cee6368225fbff77ee1dc77955ebd0eeb40f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 20 Jun 2018 22:16:22 +0100 Subject: [PATCH] Use try-with-resources for closables --- .../net/kemitix/gitdb/impl/InitGitDBRepo.java | 18 +++++++++--------- .../net/kemitix/gitdb/impl/KeyRemover.java | 9 ++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index d99de8b..b2019e1 100644 --- a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -30,10 +30,7 @@ import org.eclipse.jgit.util.FS; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.DirectoryNotEmptyException; -import java.nio.file.Files; -import java.nio.file.NotDirectoryException; -import java.nio.file.Path; +import java.nio.file.*; /** * Initialise a new GitDB Repo. @@ -59,9 +56,10 @@ class InitGitDBRepo { final InitGitDBRepo initRepo = new InitGitDBRepo(); final File validDbDir = initRepo.validDbDir(dbDir.toFile()); validDbDir.mkdirs(); - final Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false); - repository.create(true); - initRepo.createInitialBranchOnMaster(repository); + try (final Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false)) { + repository.create(true); + initRepo.createInitialBranchOnMaster(repository); + } } private void createInitialBranchOnMaster(final Repository repository) throws IOException { @@ -102,8 +100,10 @@ class InitGitDBRepo { } private void verifyIsEmpty(final File dbDir) throws IOException { - if (Files.newDirectoryStream(dbDir.toPath()).iterator().hasNext()) { - throw new DirectoryNotEmptyException(dbDir.toString()); + try (final DirectoryStream directoryStream = Files.newDirectoryStream(dbDir.toPath())) { + if (directoryStream.iterator().hasNext()) { + throw new DirectoryNotEmptyException(dbDir.toString()); + } } } diff --git a/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java b/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java index 69f9d86..b27cdc3 100644 --- a/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java +++ b/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java @@ -22,10 +22,7 @@ package net.kemitix.gitdb.impl; import lombok.RequiredArgsConstructor; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.TreeFormatter; +import org.eclipse.jgit.lib.*; import java.io.IOException; import java.util.Optional; @@ -108,6 +105,8 @@ class KeyRemover { * @throws IOException the object could not be stored. */ private ObjectId insertTree(final TreeFormatter treeFormatter) throws IOException { - return repository.getObjectDatabase().newInserter().insert(treeFormatter); + try (final ObjectInserter inserter = repository.getObjectDatabase().newInserter()) { + return inserter.insert(treeFormatter); + } } }