Use try-with-resources for closables

This commit is contained in:
Paul Campbell 2018-06-20 22:16:22 +01:00
parent 32354fe8ea
commit b4e6cee636
2 changed files with 13 additions and 14 deletions

View file

@ -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<Path> directoryStream = Files.newDirectoryStream(dbDir.toPath())) {
if (directoryStream.iterator().hasNext()) {
throw new DirectoryNotEmptyException(dbDir.toString());
}
}
}

View file

@ -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);
}
}
}