pay attention to closing streams

This commit is contained in:
Grahame Grieve 2023-06-30 14:38:04 +10:00
parent 2ecc4bb780
commit adfa344e5e
12 changed files with 108 additions and 30 deletions

View File

@ -258,8 +258,11 @@ public class FTPClient {
private void attemptUpload(String source, String resolvedPath) throws IOException {
final long startTime = System.nanoTime();
FileInputStream localStream = new FileInputStream(source);
clientImpl.storeFile(resolvedPath, localStream);
localStream.close();
try {
clientImpl.storeFile(resolvedPath, localStream);
} finally {
localStream.close();
}
this.storeFileTimeNanos += System.nanoTime() - startTime;
}

View File

@ -90,11 +90,16 @@ public class TextFile {
public static void stringToFile(String content, File file) throws IOException {
OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
FileOutputStream fs = new FileOutputStream(file);
try {
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
} finally {
fs.close();
}
}
public static void stringToStream(String content, OutputStream stream, boolean bom) throws IOException {
@ -124,7 +129,8 @@ public class TextFile {
}
public static void stringToFile(String content, File file, boolean bom) throws IOException {
OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
FileOutputStream fs = new FileOutputStream(file);
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
if (bom)
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);

View File

@ -91,11 +91,11 @@ public class XsltUtilities {
return res.getOutputStream().toString();
}
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws TransformerException, IOException {
saxonTransform(xsltDir, source, xslt, dest, alt, null);
}
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws FileNotFoundException, TransformerException {
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws TransformerException, IOException {
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
@ -108,12 +108,19 @@ public class XsltUtilities {
}
t.setURIResolver(new MyURIResolver(xsltDir, alt));
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new FileOutputStream(dest));
t.transform(src, res);
FileInputStream fso = new FileInputStream(source);
FileOutputStream fsr = new FileOutputStream(dest);
try {
StreamSource src = new StreamSource(fso);
StreamResult res = new StreamResult(fsr);
t.transform(src, res);
} finally {
fso.close();
fsr.close();
}
}
public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws TransformerException, IOException {
TransformerFactory f = TransformerFactory.newInstance();
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
@ -121,9 +128,16 @@ public class XsltUtilities {
Transformer t = f.newTransformer(xsrc);
t.setURIResolver(new MyURIResolver(xsltDir, alt));
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new FileOutputStream(dest));
t.transform(src, res);
FileInputStream fss = new FileInputStream(source);
FileOutputStream fsr = new FileOutputStream(dest);
try {
StreamSource src = new StreamSource(fss);
StreamResult res = new StreamResult(fsr);
t.transform(src, res);
} finally {
fss.close();
fsr.close();
}
}

View File

@ -49,7 +49,12 @@ public class JsonLangFileProducer extends LanguageFileProducer {
@Override
public void finish() throws IOException {
JsonParser.compose(json, new FileOutputStream(getFileName(id, baseLang)));
FileOutputStream fs = new FileOutputStream(getFileName(id, baseLang));
try {
JsonParser.compose(json, fs);
} finally {
fs.close();
}
}
}

View File

@ -187,7 +187,12 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
if (files != null) {
for (File f : files) {
if (f.getName().endsWith(".tgz")) {
temporaryPackages.add(NpmPackage.fromPackage(new FileInputStream(f)));
FileInputStream fs = new FileInputStream(f);
try {
temporaryPackages.add(NpmPackage.fromPackage(fs));
} finally {
fs.close();
}
}
}
}

View File

@ -37,7 +37,13 @@ public class PackageHacker {
if (!f.exists())
throw new Error("Unable to find "+f.getAbsolutePath());
NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
NpmPackage pck = null;
FileInputStream fs = new FileInputStream(f);
try {
pck = NpmPackage.fromPackage(fs);
} finally {
fs.close();
}
System.out.println("Altering Package "+f.getAbsolutePath());
System.out.println(nice(pck.getNpm()));
@ -51,7 +57,12 @@ public class PackageHacker {
int r = System.in.read();
if (r == 'y') {
f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
pck.save(new FileOutputStream(f));
FileOutputStream fso = new FileOutputStream(f);
try {
pck.save(fso);
} finally {
fso.close();
}
}
}

View File

@ -30,7 +30,12 @@ public class ResourceRenamer {
String rt = r.asString("resourceType");
String id = r.asString("id");
String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json");
JsonParser.compose(r, new FileOutputStream(nn), true);
FileOutputStream fs = new FileOutputStream(nn);
try {
JsonParser.compose(r, fs, true);
} finally {
fs.close();
}
}
}

View File

@ -1008,9 +1008,13 @@ public class HierarchicalTableGenerator extends TranslatingUtilities {
}
newFile.createNewFile();
FileOutputStream stream = new FileOutputStream(file);
genImage(indents, hasChildren, lineColor, stream);
if (outputTracker!=null)
outputTracker.add(file);
try {
genImage(indents, hasChildren, lineColor, stream);
if (outputTracker!=null)
outputTracker.add(file);
} finally {
stream.close();
}
}
return b.toString();
}

View File

@ -117,7 +117,12 @@ public class XLSXmlNormaliser {
if (!hasComment)
root.appendChild(xml.createComment("canonicalized"));
try {
saveXml(new FileOutputStream(dest));
FileOutputStream fs = new FileOutputStream(dest);
try {
saveXml(fs);
} finally {
fs.close();
}
String s = TextFile.fileToString(dest);
s = s.replaceAll("\r\n","\n");
s = replaceSignificantEoln(s);

View File

@ -55,6 +55,7 @@ import javax.xml.transform.stream.StreamResult;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@ -458,14 +459,24 @@ public class XMLUtil {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new FileInputStream(filename));
FileInputStream fs = new FileInputStream(filename);
try {
return builder.parse(fs);
} finally {
fs.close();
}
}
public static Document parseFileToDom(String filename, boolean ns) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(ns);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new FileInputStream(filename));
FileInputStream fs = new FileInputStream(filename);
try {
return builder.parse(fs);
} finally {
fs.close();
}
}
public static Element getLastChild(Element e) {

View File

@ -27,7 +27,12 @@ public class XmlEscaper {
}
public static void convert(String source, String target) throws IOException {
convertAndClose(new FileInputStream(source), new FileOutputStream(target));
FileOutputStream fs = new FileOutputStream(target);
try {
convertAndClose(new FileInputStream(source), fs);
} finally {
fs.close();
}
}
public static void convertAndClose(InputStream source, OutputStream target) throws IOException {

View File

@ -74,7 +74,11 @@ public class XmlGenerator {
public void generate(Element element, File file) throws IOException, FHIRException {
OutputStream stream = new FileOutputStream(file);
generate(element, stream);
try {
generate(element, stream);
} finally {
stream.close();
}
}
public void generate(Element element, OutputStream stream) throws IOException, FHIRException {