From: David Pursehouse Date: Tue, 13 Mar 2018 08:51:10 +0000 (+0900) Subject: DfsInserter: Refactor writePackIndex to use try-with-resource X-Git-Tag: v5.0.0.201805151920-m7~88 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fchanges%2F01%2F119301%2F3;p=jgit.git DfsInserter: Refactor writePackIndex to use try-with-resource Refactor to allow the temporary buffer to be opened in try-with-resource. Change-Id: Id913e6c3ed3913fd5d79d66238b779e0c225b47d Signed-off-by: David Pursehouse --- diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java index eb0a527c5f..2723fd1923 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java @@ -68,6 +68,7 @@ import java.util.zip.DeflaterOutputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; +import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.LargeObjectException; @@ -309,6 +310,15 @@ public class DfsInserter extends ObjectInserter { Collections.sort(objectList); } + @Nullable + private TemporaryBuffer.Heap maybeGetTemporaryBuffer( + List list) { + if (list.size() <= 58000) { + return new TemporaryBuffer.Heap(2 << 20); + } + return null; + } + PackIndex writePackIndex(DfsPackDescription pack, byte[] packHash, List list) throws IOException { pack.setIndexVersion(INDEX_VERSION); @@ -317,27 +327,20 @@ public class DfsInserter extends ObjectInserter { // If there are less than 58,000 objects, the entire index fits in under // 2 MiB. Callers will probably need the index immediately, so buffer // the index in process and load from the buffer. - TemporaryBuffer.Heap buf = null; PackIndex packIndex = null; - if (list.size() <= 58000) { - buf = new TemporaryBuffer.Heap(2 << 20); - index(buf, packHash, list); - packIndex = PackIndex.read(buf.openInputStream()); - } - - try (DfsOutputStream os = db.writeFile(pack, INDEX)) { - CountingOutputStream cnt = new CountingOutputStream(os); - if (buf != null) + try (TemporaryBuffer.Heap buf = maybeGetTemporaryBuffer(list); + DfsOutputStream os = db.writeFile(pack, INDEX); + CountingOutputStream cnt = new CountingOutputStream(os)) { + if (buf != null) { + index(buf, packHash, list); + packIndex = PackIndex.read(buf.openInputStream()); buf.writeTo(cnt, null); - else + } else { index(cnt, packHash, list); + } pack.addFileExt(INDEX); pack.setBlockSize(INDEX, os.blockSize()); pack.setFileSize(INDEX, cnt.getCount()); - } finally { - if (buf != null) { - buf.close(); - } } return packIndex; }