Browse Source

Merge changes from topic 'try-with-resource'

* changes:
  DiffCommand: Open DiffFormatter in try-with-resource
  DiffAlgorithms: Open Repository in try-with-resource
  DescribeCommandTest: Open FileWriter in try-with-resource
  CommitCommand: Open InputStream in try-with-resource
  DefaultNoteMerger: Open UnionInputStream in try-with-resource
stable-4.11
David Pursehouse 7 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
e6c456eeae
  1. 5
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java
  2. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
  3. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
  4. 19
      org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java
  5. 7
      org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java

5
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java

@ -160,11 +160,8 @@ class DiffAlgorithms extends TextBuiltin {
else else
rb.findGitDir(dir); rb.findGitDir(dir);
Repository repo = rb.build(); try (Repository repo = rb.build()) {
try {
run(repo); run(repo);
} finally {
repo.close();
} }
} }
} }

6
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java

@ -297,9 +297,9 @@ public class DescribeCommandTest extends RepositoryTestCase {
} }
private static void touch(File f, String contents) throws Exception { private static void touch(File f, String contents) throws Exception {
FileWriter w = new FileWriter(f); try (FileWriter w = new FileWriter(f)) {
w.write(contents); w.write(contents);
w.close(); }
} }
private String describe(ObjectId c1, boolean longDesc) private String describe(ObjectId c1, boolean longDesc)

7
org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

@ -409,14 +409,11 @@ public class CommitCommand extends GitCommand<RevCommit> {
inserter = repo.newObjectInserter(); inserter = repo.newObjectInserter();
long contentLength = fTree long contentLength = fTree
.getEntryContentLength(); .getEntryContentLength();
InputStream inputStream = fTree try (InputStream inputStream = fTree
.openEntryStream(); .openEntryStream()) {
try {
dcEntry.setObjectId(inserter.insert( dcEntry.setObjectId(inserter.insert(
Constants.OBJ_BLOB, contentLength, Constants.OBJ_BLOB, contentLength,
inputStream)); inputStream));
} finally {
inputStream.close();
} }
} }
} }

19
org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java

@ -104,6 +104,12 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> {
super(repo); super(repo);
} }
private DiffFormatter getDiffFormatter() {
return out != null && !showNameAndStatusOnly
? new DiffFormatter(new BufferedOutputStream(out))
: new DiffFormatter(NullOutputStream.INSTANCE);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p> * <p>
@ -114,14 +120,9 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> {
*/ */
@Override @Override
public List<DiffEntry> call() throws GitAPIException { public List<DiffEntry> call() throws GitAPIException {
final DiffFormatter diffFmt; try (DiffFormatter diffFmt = getDiffFormatter()) {
if (out != null && !showNameAndStatusOnly) diffFmt.setRepository(repo);
diffFmt = new DiffFormatter(new BufferedOutputStream(out)); diffFmt.setProgressMonitor(monitor);
else
diffFmt = new DiffFormatter(NullOutputStream.INSTANCE);
diffFmt.setRepository(repo);
diffFmt.setProgressMonitor(monitor);
try {
if (cached) { if (cached) {
if (oldTree == null) { if (oldTree == null) {
ObjectId head = repo.resolve(HEAD + "^{tree}"); //$NON-NLS-1$ ObjectId head = repo.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
@ -159,8 +160,6 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> {
} }
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e); throw new JGitInternalException(e.getMessage(), e);
} finally {
diffFmt.close();
} }
} }

7
org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java

@ -82,14 +82,11 @@ public class DefaultNoteMerger implements NoteMerger {
ObjectLoader lo = reader.open(ours.getData()); ObjectLoader lo = reader.open(ours.getData());
ObjectLoader lt = reader.open(theirs.getData()); ObjectLoader lt = reader.open(theirs.getData());
UnionInputStream union = new UnionInputStream(lo.openStream(), try (UnionInputStream union = new UnionInputStream(lo.openStream(),
lt.openStream()); lt.openStream())) {
try {
ObjectId noteData = inserter.insert(Constants.OBJ_BLOB, ObjectId noteData = inserter.insert(Constants.OBJ_BLOB,
lo.getSize() + lt.getSize(), union); lo.getSize() + lt.getSize(), union);
return new Note(ours, noteData); return new Note(ours, noteData);
} finally {
union.close();
} }
} }
} }

Loading…
Cancel
Save