fix(java_indexer): do not close System.out after first analysis
@@ -90,6 +90,11 @@ public class JavaIndexer {
statistics = new MemoryStatisticsCollector();
}
+ try (OutputStream stream =
+ Strings.isNullOrEmpty(config.getOutputPath())
+ ? System.out
+ : new BufferedOutputStream(new FileOutputStream(config.getOutputPath()))) {
+
if (!Strings.isNullOrEmpty(config.getIndexPackRoot())) {
// java_indexer --index_pack=archive-root unit-key+
logger.atWarning().log(
@@ -98,7 +103,7 @@ public class JavaIndexer {
Archive archive = new Archive(config.getIndexPackRoot());
for (String unitDigest : config.getCompilation()) {
CompilationDescription desc = archive.readDescription(unitDigest);
- analyzeCompilation(config, plugins, statistics, desc);
+ analyzeCompilation(config, plugins, statistics, desc, stream);
}
} else {
// java_indexer compilation-file+
@@ -113,7 +118,7 @@ public class JavaIndexer {
CompilationDescription desc =
IndexInfoUtils.indexedCompilationToCompilationDescription(
indexedCompilation, reader);
- analyzeCompilation(config, plugins, statistics, desc);
+ analyzeCompilation(config, plugins, statistics, desc, stream);
}
} catch (KZipException e) {
throw new IllegalArgumentException("Unable to read kzip", e);
@@ -129,7 +134,7 @@ public class JavaIndexer {
+ "use kzip files instead: https://kythe.io/docs/kythe-kzip.html");
try {
CompilationDescription desc = IndexInfoUtils.readKindexInfoFromFile(compilationPath);
- analyzeCompilation(config, plugins, statistics, desc);
+ analyzeCompilation(config, plugins, statistics, desc, stream);
} catch (EOFException e) {
if (config.getIgnoreEmptyKIndex()) {
return;
@@ -141,6 +146,7 @@ public class JavaIndexer {
}
}
}
+ }
if (statistics != null) {
statistics.printStatistics(System.err);
@@ -151,7 +157,8 @@ public class JavaIndexer {
StandaloneConfig config,
List<Supplier<Plugin>> plugins,
StatisticsCollector statistics,
- CompilationDescription desc)
+ CompilationDescription desc,
+ OutputStream stream)
throws AnalysisException, IOException {
if (!desc.getFileContents().iterator().hasNext()) {
// Skip empty compilations.
@@ -163,10 +170,6 @@ public class JavaIndexer {
new ProtobufMetadataLoader(desc.getCompilationUnit(), config.getDefaultMetadataCorpus()));
metadataLoaders.addLoader(new KytheMetadataLoader());
- try (OutputStream stream =
- Strings.isNullOrEmpty(config.getOutputPath())
- ? System.out
- : new BufferedOutputStream(new FileOutputStream(config.getOutputPath(), true))) {
KytheJavacAnalyzer analyzer =
new KytheJavacAnalyzer(
config,
@@ -178,7 +181,6 @@ public class JavaIndexer {
new JavacAnalysisDriver()
.analyze(analyzer, desc.getCompilationUnit(), new FileDataCache(desc.getFileContents()));
}
- }
private static URL fileURL(String path) throws MalformedURLException {
return new File(path).toURI().toURL();
