diff options
author | Jonathan Nieder <jrn@google.com> | 2015-05-27 16:25:32 -0700 |
---|---|---|
committer | Jonathan Nieder <jrn@google.com> | 2015-05-27 16:49:12 -0700 |
commit | 1287cdaf338c4f1b782985597de9358de534bbef (patch) | |
tree | 6625a3955e92a8bd9e8ae54b8c2292d4690ed006 /org.eclipse.jgit | |
parent | ebfd62433a58d23af221adfdffed56d9274f4268 (diff) | |
download | jgit-1287cdaf338c4f1b782985597de9358de534bbef.tar.gz jgit-1287cdaf338c4f1b782985597de9358de534bbef.zip |
Close 'out' consistently in ArchiveCommand.call
Whether the output stream specified with setOutputStream() is closed by
ArchiveCommand.call() is murky and inconsistent:
- on success, it is closed
- if an exception is encountered when writing the archive, it is closed
- if an exception is encountered when calling createArchiveStream to
open the archive, we forget to close it
Close the output stream consistently to avoid leaks.
Now that the inner try-with-resources doesn't have its own finally
block, this allows us to merge the two try blocks.
It would be even better to never close the output stream. That will
involve more API changes to avoid silently breaking callers, so it is
deferred to a later change.
Change-Id: I0185bdaa60ecee4a541eab5d8ff6c9c4dbe40bf1
Signed-off-by: Jonathan Nieder <jrn@google.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java index 713866dc53..5f31be69f9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java @@ -364,10 +364,11 @@ public class ArchiveCommand extends GitCommand<OutputStream> { } private <T extends Closeable> OutputStream writeArchive(Format<T> fmt) { - final String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$ - try (final TreeWalk walk = new TreeWalk(repo)) { - final T outa = fmt.createArchiveOutputStream(out, formatOptions); - try (final RevWalk rw = new RevWalk(walk.getObjectReader())) { + try { + try (TreeWalk walk = new TreeWalk(repo); + RevWalk rw = new RevWalk(walk.getObjectReader())) { + final String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$ + final T outa = fmt.createArchiveOutputStream(out, formatOptions); final MutableObjectId idBuf = new MutableObjectId(); final ObjectReader reader = walk.getObjectReader(); @@ -376,7 +377,7 @@ public class ArchiveCommand extends GitCommand<OutputStream> { walk.setFilter(PathFilterGroup.createFromStrings(paths)); while (walk.next()) { - final String name = pfx + walk.getPathString(); + String name = pfx + walk.getPathString(); FileMode mode = walk.getFileMode(0); if (walk.isSubtree()) @@ -395,10 +396,10 @@ public class ArchiveCommand extends GitCommand<OutputStream> { fmt.putEntry(outa, name, mode, reader.open(idBuf)); } outa.close(); + return out; } finally { out.close(); } - return out; } catch (IOException e) { // TODO(jrn): Throw finer-grained errors. throw new JGitInternalException( |