]> source.dussan.org Git - jgit.git/commitdiff
ReflogWriter: Refactor to open FileOutputStream in try-with-resource 62/119462/2
authorDavid Pursehouse <david.pursehouse@gmail.com>
Thu, 15 Mar 2018 04:32:47 +0000 (13:32 +0900)
committerDavid Pursehouse <david.pursehouse@gmail.com>
Fri, 16 Mar 2018 03:06:52 +0000 (12:06 +0900)
Change-Id: I028ced10eecc99214a4c4a8055c379af72193f13
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ReflogWriter.java

index d99c266144f0307c9dae9d0292c2d468f57b2dd7..b366eafe2f32c42732a0f62beca2f06ffe3a0121 100644 (file)
@@ -219,6 +219,22 @@ public class ReflogWriter {
                return Constants.encode(r.toString());
        }
 
+       private FileOutputStream getFileOutputStream(File log) throws IOException {
+               try {
+                       return new FileOutputStream(log, true);
+               } catch (FileNotFoundException err) {
+                       File dir = log.getParentFile();
+                       if (dir.exists()) {
+                               throw err;
+                       }
+                       if (!dir.mkdirs() && !dir.isDirectory()) {
+                               throw new IOException(MessageFormat
+                                               .format(JGitText.get().cannotCreateDirectory, dir));
+                       }
+                       return new FileOutputStream(log, true);
+               }
+       }
+
        private ReflogWriter log(String refName, byte[] rec) throws IOException {
                File log = refdb.logFor(refName);
                boolean write = forceWrite
@@ -228,29 +244,17 @@ public class ReflogWriter {
                        return this;
 
                WriteConfig wc = refdb.getRepository().getConfig().get(WriteConfig.KEY);
-               FileOutputStream out;
-               try {
-                       out = new FileOutputStream(log, true);
-               } catch (FileNotFoundException err) {
-                       File dir = log.getParentFile();
-                       if (dir.exists())
-                               throw err;
-                       if (!dir.mkdirs() && !dir.isDirectory())
-                               throw new IOException(MessageFormat.format(
-                                               JGitText.get().cannotCreateDirectory, dir));
-                       out = new FileOutputStream(log, true);
-               }
-               try {
+               try (FileOutputStream out = getFileOutputStream(log)) {
                        if (wc.getFSyncRefFiles()) {
                                FileChannel fc = out.getChannel();
                                ByteBuffer buf = ByteBuffer.wrap(rec);
-                               while (0 < buf.remaining())
+                               while (0 < buf.remaining()) {
                                        fc.write(buf);
+                               }
                                fc.force(true);
-                       } else
+                       } else {
                                out.write(rec);
-               } finally {
-                       out.close();
+                       }
                }
                return this;
        }