]> source.dussan.org Git - jgit.git/commitdiff
TreeWalk: Do not close reader passed explicitly to constructor 17/43617/2
authorDave Borowitz <dborowitz@google.com>
Tue, 10 Mar 2015 22:21:06 +0000 (15:21 -0700)
committerDave Borowitz <dborowitz@google.com>
Tue, 10 Mar 2015 23:27:22 +0000 (16:27 -0700)
The TreeWalk(ObjectReader) constructor is explicitly to handle the case
where the caller is responsible for opening and closing the reader.
The reader should only be closed when it was created in the
TreeWalk(Repository) constructor.

Change-Id: I627681be80d69ea549f953255a64c7b3b68bcec9

org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java

index 3ecef453e6faad04e50a4832dbbd93ccd87a0cc1..a4d2d7e7968dce09dc0105f26d5d5bbb7eef35e9 100644 (file)
@@ -195,6 +195,8 @@ public class TreeWalk implements AutoCloseable {
 
        private final ObjectReader reader;
 
+       private final boolean closeReader;
+
        private final MutableObjectId idBuffer = new MutableObjectId();
 
        private TreeFilter filter;
@@ -217,22 +219,30 @@ public class TreeWalk implements AutoCloseable {
         * Create a new tree walker for a given repository.
         *
         * @param repo
-        *            the repository the walker will obtain data from.
+        *            the repository the walker will obtain data from. An
+        *            ObjectReader will be created by the walker, and will be closed
+        *            when the walker is closed.
         */
        public TreeWalk(final Repository repo) {
-               this(repo.newObjectReader());
+               this(repo.newObjectReader(), true);
        }
 
        /**
         * Create a new tree walker for a given repository.
         *
         * @param or
-        *            the reader the walker will obtain tree data from.
+        *            the reader the walker will obtain tree data from. The reader
+        *            is not closed when the walker is closed.
         */
        public TreeWalk(final ObjectReader or) {
+               this(or, false);
+       }
+
+       private TreeWalk(final ObjectReader or, final boolean closeReader) {
                reader = or;
                filter = TreeFilter.ALL;
                trees = NO_TREES;
+               this.closeReader = closeReader;
        }
 
        /** @return the reader this walker is using to load objects. */
@@ -261,7 +271,9 @@ public class TreeWalk implements AutoCloseable {
         */
        @Override
        public void close() {
-               reader.close();
+               if (closeReader) {
+                       reader.close();
+               }
        }
 
        /**