From f0f5d101ddac1bce16b49fce828e85096c24bbfd Mon Sep 17 00:00:00 2001 From: laszlocsomor Date: Mon, 9 Jul 2018 08:41:53 -0700 Subject: Bazel server, tools: ensure Readers are closed Follow-up to commit 59f17d6e0550bf63a0b6ef182e2d63474e058ede. Use try-with-resources to ensure Reader objects are closed eagerly. Eagerly closing Readers avoids hanging on to file handles until the garbage collector finalizes the object, meaning Bazel on Windows (and other processes) can delete or mutate these files. Hopefully this avoids intermittent file deletion errors that sometimes occur on Windows. See https://github.com/bazelbuild/bazel/issues/5512 RELNOTES: none PiperOrigin-RevId: 203771262 --- .../android/incrementaldeployment/StubApplication.java | 5 +---- .../devtools/build/android/ziputils/SplitZip.java | 17 ++++++----------- 2 files changed, 7 insertions(+), 15 deletions(-) (limited to 'src/tools') diff --git a/src/tools/android/java/com/google/devtools/build/android/incrementaldeployment/StubApplication.java b/src/tools/android/java/com/google/devtools/build/android/incrementaldeployment/StubApplication.java index 0b91d03207..5cf15fad73 100644 --- a/src/tools/android/java/com/google/devtools/build/android/incrementaldeployment/StubApplication.java +++ b/src/tools/android/java/com/google/devtools/build/android/incrementaldeployment/StubApplication.java @@ -434,8 +434,7 @@ public class StubApplication extends Application { private static Map parseManifest(File file) throws IOException { Map result = new HashMap<>(); - BufferedReader reader = new BufferedReader(new FileReader(file)); - try { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { while (true) { String line = reader.readLine(); if (line == null) { @@ -445,8 +444,6 @@ public class StubApplication extends Application { String[] items = line.split(" "); result.put(items[0], items[1]); } - } finally { - reader.close(); } return result; diff --git a/src/tools/android/java/com/google/devtools/build/android/ziputils/SplitZip.java b/src/tools/android/java/com/google/devtools/build/android/ziputils/SplitZip.java index 7a1f81a7e9..25a32a05cc 100644 --- a/src/tools/android/java/com/google/devtools/build/android/ziputils/SplitZip.java +++ b/src/tools/android/java/com/google/devtools/build/android/ziputils/SplitZip.java @@ -118,7 +118,7 @@ public class SplitZip implements EntryHandler { } // Package private for testing with mock file - SplitZip setMainClassListFile(InputStream clInputStream) { + SplitZip setMainClassListStreamForTesting(InputStream clInputStream) { filterInputStream = clInputStream; return this; } @@ -446,21 +446,16 @@ public class SplitZip implements EntryHandler { */ private Set readPaths(String fileName) throws IOException { Set paths = new HashSet<>(); - BufferedReader reader = null; - try { - if (filterInputStream == null) { - filterInputStream = new FileInputStream(fileName); - } - reader = new BufferedReader(new InputStreamReader(filterInputStream, UTF_8)); + if (filterInputStream == null) { + filterInputStream = new FileInputStream(fileName); + } + try (BufferedReader reader = + new BufferedReader(new InputStreamReader(filterInputStream, UTF_8))) { String line; while (null != (line = reader.readLine())) { paths.add(fixPath(line)); } return paths; - } finally { - if (reader != null) { - reader.close(); - } } } -- cgit v1.2.3