From 63be96f48884dab8611b9ad8ccc0ad030a2056a4 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Tue, 27 Feb 2018 14:16:19 +0900 Subject: [PATCH] RefDirectory: Open BufferedReader in try-with-resource Change-Id: I9239df3e381b141b48ad7ea1b706e5b5927946cd Signed-off-by: David Pursehouse --- .../internal/storage/file/RefDirectory.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java index 4495e0951..198775cc1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java @@ -940,12 +940,27 @@ public class RefDirectory extends RefDatabase { int retries = 0; while (true) { final FileSnapshot snapshot = FileSnapshot.save(packedRefsFile); - final BufferedReader br; final MessageDigest digest = Constants.newMessageDigest(); - try { - br = new BufferedReader(new InputStreamReader( - new DigestInputStream(new FileInputStream(packedRefsFile), - digest), CHARSET)); + try (BufferedReader br = new BufferedReader(new InputStreamReader( + new DigestInputStream(new FileInputStream(packedRefsFile), + digest), + CHARSET))) { + try { + return new PackedRefList(parsePackedRefs(br), snapshot, + ObjectId.fromRaw(digest.digest())); + } catch (IOException e) { + if (FileUtils.isStaleFileHandleInCausalChain(e) + && retries < maxStaleRetries) { + if (LOG.isDebugEnabled()) { + LOG.debug(MessageFormat.format( + JGitText.get().packedRefsHandleIsStale, + Integer.valueOf(retries)), e); + } + retries++; + continue; + } + throw e; + } } catch (FileNotFoundException noPackedRefs) { if (packedRefsFile.exists()) { throw noPackedRefs; @@ -953,24 +968,6 @@ public class RefDirectory extends RefDatabase { // Ignore it and leave the new list empty. return NO_PACKED_REFS; } - try { - return new PackedRefList(parsePackedRefs(br), snapshot, - ObjectId.fromRaw(digest.digest())); - } catch (IOException e) { - if (FileUtils.isStaleFileHandleInCausalChain(e) - && retries < maxStaleRetries) { - if (LOG.isDebugEnabled()) { - LOG.debug(MessageFormat.format( - JGitText.get().packedRefsHandleIsStale, - Integer.valueOf(retries)), e); - } - retries++; - continue; - } - throw e; - } finally { - br.close(); - } } }