Skip to content

Commit

Permalink
Close writers, usually with try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Nov 6, 2024
1 parent c4c22a2 commit c156c6f
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import com.google.gwt.dev.resource.ResourceOracle;
import com.google.gwt.dev.util.DefaultTextOutput;
import com.google.gwt.dev.util.OutputFileSet;
import com.google.gwt.util.tools.Utility;

import java.io.BufferedOutputStream;
import java.io.File;
Expand Down Expand Up @@ -521,10 +520,8 @@ public void produceOutput(TreeLogger logger, ArtifactSet artifacts,
partialPath = partialPath.substring(1);
}
}
OutputStream artifactStream = null;
try {
artifactStream = new BufferedOutputStream(out.openForWrite(partialPath,
artifact.getLastModified()));
try (OutputStream artifactStream = new BufferedOutputStream(
out.openForWrite(partialPath, artifact.getLastModified()))) {
artifact.writeTo(artifactLogger, artifactStream);
} catch (IOException e) {
artifactLogger.log(TreeLogger.ERROR,
Expand All @@ -533,8 +530,6 @@ public void produceOutput(TreeLogger logger, ArtifactSet artifacts,
if (visibility != Visibility.Private) {
throw new UnableToCompleteException();
}
} finally {
Utility.close(artifactStream);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
import com.google.gwt.thirdparty.json.JSONArray;
import com.google.gwt.thirdparty.json.JSONException;
import com.google.gwt.thirdparty.json.JSONObject;
import com.google.gwt.util.tools.Utility;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -170,14 +168,9 @@ private void recordCodeReferences(DependencyGraphRecorder codeGraph,
}

private String addArtifactFromJson(Object value, String named) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(baos);
writer.write(value.toString());
Utility.close(writer);

// TODO(ocallau) Must be updated with the correct/final linker
SyntheticArtifact artifact = new SyntheticArtifact(
SoycReportLinker.class, named, baos.toByteArray());
SoycReportLinker.class, named, value.toString().getBytes(StandardCharsets.UTF_8));
artifact.setVisibility(Visibility.LegacyDeploy);

toReturn.add(artifact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.gwt.dev.jjs.ast.JRunAsync;
import com.google.gwt.dev.jjs.impl.ControlFlowAnalyzer;
import com.google.gwt.dev.jjs.impl.codesplitter.MultipleDependencyGraphRecorder;
import com.google.gwt.util.tools.Utility;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -108,7 +107,10 @@ public void startDependencyGraph(String identifier, String extnds) {

/**
* Used to record dependencies of a program.
*
* @deprecated this method is unused internally, and may be removed in a future release.
*/
@Deprecated
protected void recordDependenciesImpl(TreeLogger logger, JProgram jprogram) {

logger = logger.branch(TreeLogger.DEBUG, "Creating dependencies file for the compile report");
Expand All @@ -129,7 +131,7 @@ protected void recordDependenciesImpl(TreeLogger logger, JProgram jprogram) {
printPost();

flushOutput();
Utility.close(writer);
writer.close();

} catch (Throwable e) {
logger.log(TreeLogger.WARN, "Could not write dependency file; proceeding anyway.", e);
Expand Down
101 changes: 50 additions & 51 deletions dev/core/src/com/google/gwt/core/ext/soyc/impl/SplitPointRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import com.google.gwt.dev.jjs.ast.JRunAsync;
import com.google.gwt.dev.jjs.impl.codesplitter.FragmentPartitioningResult;
import com.google.gwt.dev.util.HtmlTextOutput;
import com.google.gwt.util.tools.Utility;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Records split points to a file for Compile Reports.
*/
Expand All @@ -41,77 +42,75 @@ public static void recordSplitPoints(JProgram jprogram, OutputStream out, TreeLo
logger.branch(TreeLogger.TRACE, "Creating split point map file for the compile report");

try {
OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(out), "UTF-8");
PrintWriter pw = new PrintWriter(writer);
HtmlTextOutput htmlOut = new HtmlTextOutput(pw, false);
String curLine = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
htmlOut.printRaw(curLine);
htmlOut.newline();

curLine = "<soyc>";
htmlOut.printRaw(curLine);
htmlOut.newline();
htmlOut.indentIn();
htmlOut.indentIn();
try (OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(out), UTF_8);
PrintWriter pw = new PrintWriter(writer)) {
HtmlTextOutput htmlOut = new HtmlTextOutput(pw, false);
String curLine = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
htmlOut.printRaw(curLine);
htmlOut.newline();

List<JRunAsync> runAsyncs = jprogram.getRunAsyncs();
FragmentPartitioningResult partitionResult = jprogram.getFragmentPartitioningResult();
if (runAsyncs.size() > 0) {
curLine = "<splitpoints>";
curLine = "<soyc>";
htmlOut.printRaw(curLine);
htmlOut.newline();
htmlOut.indentIn();
htmlOut.indentIn();
for (JRunAsync runAsync : runAsyncs) {
int sp = runAsync.getRunAsyncId();
if (partitionResult != null) {
sp = partitionResult.getFragmentForRunAsync(sp);
}
String name = runAsync.getName();
curLine = "<splitpoint id=\"" + sp + "\" location=\"" + name + "\"/>";

List<JRunAsync> runAsyncs = jprogram.getRunAsyncs();
FragmentPartitioningResult partitionResult = jprogram.getFragmentPartitioningResult();
if (runAsyncs.size() > 0) {
curLine = "<splitpoints>";
htmlOut.printRaw(curLine);
htmlOut.newline();
if (logger.isLoggable(TreeLogger.TRACE)) {
logger.log(TreeLogger.TRACE, "Assigning split point #" + sp + " for '" + name + "'");
htmlOut.indentIn();
htmlOut.indentIn();
for (JRunAsync runAsync : runAsyncs) {
int sp = runAsync.getRunAsyncId();
if (partitionResult != null) {
sp = partitionResult.getFragmentForRunAsync(sp);
}
String name = runAsync.getName();
curLine = "<splitpoint id=\"" + sp + "\" location=\"" + name + "\"/>";
htmlOut.printRaw(curLine);
htmlOut.newline();
if (logger.isLoggable(TreeLogger.TRACE)) {
logger.log(TreeLogger.TRACE, "Assigning split point #" + sp + " for '" + name + "'");
}
}
htmlOut.indentOut();
htmlOut.indentOut();
curLine = "</splitpoints>";
htmlOut.printRaw(curLine);
htmlOut.newline();
}
htmlOut.indentOut();
htmlOut.indentOut();
curLine = "</splitpoints>";
htmlOut.printRaw(curLine);
htmlOut.newline();
}

if (!jprogram.getInitialFragmentIdSequence().isEmpty()) {
curLine = "<initialseq>";
htmlOut.printRaw(curLine);
htmlOut.newline();
htmlOut.indentIn();
if (!jprogram.getInitialFragmentIdSequence().isEmpty()) {
curLine = "<initialseq>";
htmlOut.printRaw(curLine);
htmlOut.newline();
htmlOut.indentIn();

for (int sp : jprogram.getInitialFragmentIdSequence()) {
if (partitionResult != null) {
sp = partitionResult.getFragmentForRunAsync(sp);
for (int sp : jprogram.getInitialFragmentIdSequence()) {
if (partitionResult != null) {
sp = partitionResult.getFragmentForRunAsync(sp);
}
curLine = "<splitpointref id=\"" + sp + "\"/>";
htmlOut.printRaw(curLine);
htmlOut.newline();
}
curLine = "<splitpointref id=\"" + sp + "\"/>";

htmlOut.indentOut();
curLine = "</initialseq>";
htmlOut.printRaw(curLine);
htmlOut.newline();
}

htmlOut.indentOut();
curLine = "</initialseq>";
htmlOut.indentOut();
curLine = "</soyc>";
htmlOut.printRaw(curLine);
htmlOut.newline();
}

htmlOut.indentOut();
htmlOut.indentOut();
curLine = "</soyc>";
htmlOut.printRaw(curLine);
htmlOut.newline();

Utility.close(writer);
pw.close();

logger.log(TreeLogger.DEBUG, "Done");

} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.google.gwt.dev.jjs.ast.JMethod;
import com.google.gwt.dev.util.Util;
import com.google.gwt.thirdparty.guava.common.collect.Lists;
import com.google.gwt.util.tools.Utility;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -145,7 +144,7 @@ protected void recordStoriesImpl(TreeLogger logger, OutputStream out,
storyCache = null;

Util.writeUtf8(builder, gzipStream);
Utility.close(gzipStream);
gzipStream.close();

logger.log(TreeLogger.INFO, "Done");
} catch (Throwable e) {
Expand Down
14 changes: 4 additions & 10 deletions dev/core/src/com/google/gwt/dev/MinimalRebuildCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.gwt.thirdparty.guava.common.io.Closeables;
import com.google.gwt.thirdparty.guava.common.util.concurrent.Futures;
import com.google.gwt.thirdparty.guava.common.util.concurrent.MoreExecutors;
import com.google.gwt.util.tools.Utility;
import com.google.gwt.util.tools.shared.Md5Utils;
import com.google.gwt.util.tools.shared.StringUtils;

Expand Down Expand Up @@ -222,12 +221,11 @@ public Void call() {
oldMinimalRebuildCacheFile.getParentFile().mkdirs();

// Write the new cache to disk.
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(newMinimalRebuildCacheFile)));
objectOutputStream.writeObject(minimalRebuildCache);
Utility.close(objectOutputStream);
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(newMinimalRebuildCacheFile)))) {
objectOutputStream.writeObject(minimalRebuildCache);
}

// Replace the old cache file with the new one.
oldMinimalRebuildCacheFile.delete();
Expand All @@ -236,10 +234,6 @@ public Void call() {
logger.log(TreeLogger.WARN,
"Unable to update the cache in " + oldMinimalRebuildCacheFile + ".");
newMinimalRebuildCacheFile.delete();
} finally {
if (objectOutputStream != null) {
Utility.close(objectOutputStream);
}
}
return null;
}
Expand Down
32 changes: 15 additions & 17 deletions dev/core/src/com/google/gwt/dev/ServletWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.gwt.dev;

import com.google.gwt.util.tools.Utility;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -69,22 +67,22 @@ public void realize(File webXml) throws IOException {
return;
}
webXml.getParentFile().mkdirs();
FileWriter xmlWriter = new FileWriter(webXml);
xmlWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
xmlWriter.write("<web-app>\n");
try (FileWriter xmlWriter = new FileWriter(webXml)) {
xmlWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
xmlWriter.write("<web-app>\n");

for (Entry<String, String> entry : mappings.entrySet()) {
String servletClass = entry.getKey();
String servletPath = entry.getValue();
String servletName = servletClass.replace('.', '_');
xmlWriter.write('\n');
xmlWriter.write(generateServletTag(servletName, servletClass));
xmlWriter.write('\n');
xmlWriter.write(generateServletMappingTag(servletName, servletPath));
xmlWriter.write('\n');
}
for (Entry<String, String> entry : mappings.entrySet()) {
String servletClass = entry.getKey();
String servletPath = entry.getValue();
String servletName = servletClass.replace('.', '_');
xmlWriter.write('\n');
xmlWriter.write(generateServletTag(servletName, servletClass));
xmlWriter.write('\n');
xmlWriter.write(generateServletMappingTag(servletName, servletPath));
xmlWriter.write('\n');
}

xmlWriter.write("\n</web-app>\n");
Utility.close(xmlWriter);
xmlWriter.write("\n</web-app>\n");
}
}
}
21 changes: 17 additions & 4 deletions dev/core/src/com/google/gwt/dev/javac/PersistentUnitCacheDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.gwt.thirdparty.guava.common.annotations.VisibleForTesting;
import com.google.gwt.thirdparty.guava.common.collect.Lists;
import com.google.gwt.thirdparty.guava.common.io.Closeables;
import com.google.gwt.util.tools.Utility;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand All @@ -38,6 +37,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -381,13 +381,17 @@ boolean writeUnit(TreeLogger logger, CompilationUnit unit)
}

/**
* Closes the current file and deletes it if it's empty. If no file is open, does nothing.
* Closes the current file and deletes it if it's empty.
*/
void close(TreeLogger logger) {
logger.log(Type.TRACE,
"Closing cache file: " + file + " (" + unitsWritten + " units written)");

Utility.close(stream);
try {
stream.close();
} catch (IOException e) {
logger.log(Type.WARN, "Error closing compilation unit cache file " + file, e);
}

if (unitsWritten == 0) {
// Remove useless empty file.
Expand All @@ -408,7 +412,16 @@ private static ObjectOutputStream openObjectStream(TreeLogger logger, File file)
return new ObjectOutputStream(new BufferedOutputStream(fstream));
} catch (IOException e) {
logger.log(Type.ERROR, "Can't open persistent unit cache file", e);
Utility.close(fstream);
try {
if (fstream != null) {
fstream.close();
if (file.exists()) {
Files.delete(file.toPath());
}
}
} catch (IOException ignored) {
// We can't handle this, and already logged an error
}
throw new UnableToCompleteException();
}
}
Expand Down
Loading

0 comments on commit c156c6f

Please sign in to comment.