Permalink
Browse files

Accept pull request #161 to close many potential resource leaks

  • Loading branch information...
ndw committed Aug 26, 2014
2 parents 3f9b832 + d469997 commit 30598d7d3f861b281f405f069d99eee32a6ad69f
View
@@ -119,17 +119,44 @@
<target name="default" depends="jar"/>
- <target name="init" depends="saxon-version">
+ <target name="init" depends="saxon-version,ant-version">
<mkdir dir="${build.dir}"/>
<mkdir dir="${test.dir}"/>
<property name="dist-version" value="${version}-${saxon-version-short}"/>
<property name="install.dir" value="${dist.dir}/calabash-${dist-version}"/>
<property name="docs.install.dir" value="${dist.dir}/calabash-${version}"/>
</target>
+ <target name="ant-version">
+ <fail message="Please upgrade to Ant 1.7.0 or higher.">
+ <condition>
+ <not>
+ <antversion atleast="1.7.0"/>
+ </not>
+ </condition>
+ </fail>
+ </target>
+
<target name="saxon95" if="saxon95">
<property name="saxon-version-short" value="95"/>
- <property name="saxon-version" value="9.5.1.1"/>
+ <!-- <property name="saxon-version" value="9.5.1.5"/> -->
+ <tempfile property="saxon-version-tmp-file"
+ destdir="${java.io.tmpdir}"
+ prefix="saxon.version"
+ suffix=".tmp"
+ deleteonexit="true"/>
+ <java classname="net.sf.saxon.Version"
+ classpathref="build.classpath"
+ output="${saxon-version-tmp-file}"/>
+ <loadfile property="saxon-version"
+ srcFile="${saxon-version-tmp-file}">
+ <filterchain>
+ <tokenfilter>
+ <stringtokenizer suppressdelims="true"/>
+ <containsregex pattern="\d+\.\d+\.\d+(\.\d+)?"/>
+ </tokenfilter>
+ </filterchain>
+ </loadfile>
</target>
<target name="saxon94" if="saxon94" unless="saxon95">
@@ -143,7 +170,9 @@
</target>
<target name="saxon-version" depends="saxon93,saxon94,saxon95">
+
<echo>Currently using saxon ${saxon-version}</echo>
+ <echo>Currently using java ${java.version}</echo>
</target>
<target name="update-output">
@@ -1025,7 +1025,8 @@ public void finish(XStep step) {
serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
OutputStream outstr = null;
- switch (this.profile.getKind()) {
+ try {
+ switch (this.profile.getKind()) {
case URI:
URI furi = URI.create(this.profile.getUri());
outstr = new FileOutputStream(new File(furi));
@@ -1037,12 +1038,14 @@ public void finish(XStep step) {
default:
throw new UnsupportedOperationException(format("Unsupported profile kind '%s'", this.profile.getKind()));
- }
-
- serializer.setOutputStream(outstr);
- S9apiUtils.serialize(this, result.getXdmNode(), serializer);
- if (!System.out.equals(outstr) && !System.err.equals(outstr)) {
- outstr.close();
+ }
+
+ serializer.setOutputStream(outstr);
+ S9apiUtils.serialize(this, result.getXdmNode(), serializer);
+ } finally {
+ if (!System.out.equals(outstr) && !System.err.equals(outstr)) {
+ outstr.close();
+ }
}
profileWriter = new TreeWriter(this);
@@ -43,6 +43,7 @@
import com.xmlcalabash.model.RuntimeValue;
import com.xmlcalabash.model.Serialization;
import com.xmlcalabash.runtime.XPipeline;
+import com.xmlcalabash.util.Closer;
import com.xmlcalabash.util.Input;
import com.xmlcalabash.util.Output;
import com.xmlcalabash.util.Output.Kind;
@@ -98,8 +99,6 @@ public void run(String[] args) throws IOException {
System.out.println();
}
- // Here all memory should be freed by the next gc, right?
- runtime.close();
} catch (UnsupportedOperationException uoe) {
usage();
} catch (XProcException err) {
@@ -132,6 +131,9 @@ public void run(String[] args) throws IOException {
if (debug) {
err.printStackTrace();
}
+ } finally {
+ // Here all memory should be freed by the next gc, right?
+ runtime.close();
}
}
@@ -229,8 +231,11 @@ boolean run(UserArgs userArgs, XProcConfiguration config) throws SaxonApiExcepti
case INPUT_STREAM:
InputStream inputStream = input.getInputStream();
- doc = runtime.parse(new InputSource(inputStream));
- inputStream.close();
+ try {
+ doc = runtime.parse(new InputSource(inputStream));
+ } finally {
+ Closer.close(inputStream);
+ }
break;
default:
@@ -248,9 +253,12 @@ boolean run(UserArgs userArgs, XProcConfiguration config) throws SaxonApiExcepti
case INPUT_STREAM:
InputStream inputStream = input.getInputStream();
- rd = new ReadableData(runtime, c_data, inputStream, input.getContentType());
- doc = rd.read();
- inputStream.close();
+ try {
+ rd = new ReadableData(runtime, c_data, inputStream, input.getContentType());
+ doc = rd.read();
+ } finally {
+ Closer.close(inputStream);
+ }
break;
default:
@@ -404,13 +412,15 @@ boolean run(UserArgs userArgs, XProcConfiguration config) throws SaxonApiExcepti
}
}
- ReadablePipe rpipe = pipeline.readFrom(port);
- while (rpipe.moreDocuments()) {
- wd.write(rpipe.read());
- }
-
- if (output != null) {
- wd.close();
+ try {
+ ReadablePipe rpipe = pipeline.readFrom(port);
+ while (rpipe.moreDocuments()) {
+ wd.write(rpipe.read());
+ }
+ } finally {
+ if (output != null) {
+ wd.close();
+ }
}
}
@@ -439,12 +449,17 @@ private void usage() throws IOException {
}
BufferedReader br = new BufferedReader(new InputStreamReader(instream));
- String line = null;
- while ((line = br.readLine()) != null) {
- System.err.println(line);
+ try {
+ String line = null;
+ while ((line = br.readLine()) != null) {
+ System.err.println(line);
+ }
+ } finally {
+ // BufferedReader.close also closes the underlying stream, so only
+ // one close() call is necessary.
+ // instream.close();
+ br.close();
}
- instream.close();
- br.close();
System.exit(1);
}
@@ -111,113 +111,119 @@ public void load(URI id, String media, InputStream content,
void unzip(DatatypeFactory dfactory, String systemId, InputStream stream) throws IOException {
ZipInputStream zipFile = new ZipInputStream(stream);
- TreeWriter tree = new TreeWriter(runtime);
+ try {
+ TreeWriter tree = new TreeWriter(runtime);
- if (name == null) {
- tree.startDocument(step.getNode().getBaseURI());
- tree.addStartElement(c_zipfile);
- tree.addAttribute(_href, systemId);
- tree.startContent();
+ if (name == null) {
+ tree.startDocument(step.getNode().getBaseURI());
+ tree.addStartElement(c_zipfile);
+ tree.addAttribute(_href, systemId);
+ tree.startContent();
- GregorianCalendar cal = new GregorianCalendar();
+ GregorianCalendar cal = new GregorianCalendar();
- ZipEntry entry = zipFile.getNextEntry();
- while (entry != null) {
- cal.setTimeInMillis(entry.getTime());
- XMLGregorianCalendar xmlCal = dfactory.newXMLGregorianCalendar(cal);
+ ZipEntry entry = zipFile.getNextEntry();
+ while (entry != null) {
+ cal.setTimeInMillis(entry.getTime());
+ XMLGregorianCalendar xmlCal = dfactory.newXMLGregorianCalendar(cal);
- if (entry.isDirectory()) {
- tree.addStartElement(c_directory);
- } else {
- tree.addStartElement(c_file);
+ if (entry.isDirectory()) {
+ tree.addStartElement(c_directory);
+ } else {
+ tree.addStartElement(c_file);
- tree.addAttribute(_compressed_size, ""+entry.getCompressedSize());
- tree.addAttribute(_size, ""+entry.getSize());
- }
+ tree.addAttribute(_compressed_size, ""+entry.getCompressedSize());
+ tree.addAttribute(_size, ""+entry.getSize());
+ }
+
+ if (entry.getComment() != null) {
+ tree.addAttribute(_comment, entry.getComment());
+ }
- if (entry.getComment() != null) {
- tree.addAttribute(_comment, entry.getComment());
+ tree.addAttribute(_name, ""+entry.getName());
+ tree.addAttribute(_date, xmlCal.toXMLFormat());
+ tree.startContent();
+ tree.addEndElement();
+ entry = zipFile.getNextEntry();
}
- tree.addAttribute(_name, ""+entry.getName());
- tree.addAttribute(_date, xmlCal.toXMLFormat());
- tree.startContent();
tree.addEndElement();
- entry = zipFile.getNextEntry();
- }
-
- zipFile.close();
-
- tree.addEndElement();
- tree.endDocument();
- result.write(tree.getResult());
- } else {
- ZipEntry entry = zipFile.getNextEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
+ tree.endDocument();
+ result.write(tree.getResult());
+ } else {
+ ZipEntry entry = zipFile.getNextEntry();
+ while (entry != null) {
+ if (name.equals(entry.getName())) {
+ break;
+ }
+ entry = zipFile.getNextEntry();
}
- entry = zipFile.getNextEntry();
- }
-
- if (entry == null) {
- throw new XProcException(step.getNode(), "ZIP file does not contain '" + name + "'");
- }
- if ("application/xml".equals(contentType) || "text/xml".equals(contentType)
- || contentType.endsWith("+xml")) {
- InputSource isource = new InputSource(zipFile);
- XdmNode doc = runtime.parse(isource);
- result.write(doc);
- } else {
- boolean storeText = (contentType != null && contentType.startsWith("text/") && charset != null);
-
- // There's no point giving the file the URI of the pipeline document.
- // This formulation is parallel to the jar scheme.
- URI zipURI = URI.create("zip:" + zipFn + "!" + entry.getName());
-
- tree.startDocument(zipURI);
- tree.addStartElement(XProcConstants.c_data);
- tree.addAttribute(_name,name);
- tree.addAttribute(_content_type, contentType);
- if (!storeText) {
- tree.addAttribute(_encoding, "base64");
+ if (entry == null) {
+ throw new XProcException(step.getNode(), "ZIP file does not contain '" + name + "'");
}
- tree.startContent();
- if (storeText) {
- InputStreamReader reader = new InputStreamReader(zipFile, charset);
- int maxlen = 4096;
- char[] chars = new char[maxlen];
- int read = reader.read(chars, 0, maxlen);
- while (read >= 0) {
- if (read > 0) {
- String s = new String(chars);
- tree.addText(s);
- }
- read = reader.read(chars, 0, maxlen);
- }
- reader.close();
+ if ("application/xml".equals(contentType) || "text/xml".equals(contentType)
+ || contentType.endsWith("+xml")) {
+ InputSource isource = new InputSource(zipFile);
+ XdmNode doc = runtime.parse(isource);
+ result.write(doc);
} else {
- BufferedInputStream bufstream = new BufferedInputStream(zipFile);
- int maxlen = 4096 * 3;
- byte[] bytes = new byte[maxlen];
- int read = bufstream.read(bytes, 0, maxlen);
- while (read >= 0) {
- if (read > 0) {
- String base64 = Base64.encodeBytes(bytes, 0, read);
- tree.addText(base64 + "\n");
+ boolean storeText = (contentType != null && contentType.startsWith("text/") && charset != null);
+
+ // There's no point giving the file the URI of the pipeline document.
+ // This formulation is parallel to the jar scheme.
+ URI zipURI = URI.create("zip:" + zipFn + "!" + entry.getName());
+
+ tree.startDocument(zipURI);
+ tree.addStartElement(XProcConstants.c_data);
+ tree.addAttribute(_name,name);
+ tree.addAttribute(_content_type, contentType);
+ if (!storeText) {
+ tree.addAttribute(_encoding, "base64");
+ }
+ tree.startContent();
+
+ if (storeText) {
+ InputStreamReader reader = new InputStreamReader(zipFile, charset);
+ try {
+ int maxlen = 4096;
+ char[] chars = new char[maxlen];
+ int read = reader.read(chars, 0, maxlen);
+ while (read >= 0) {
+ if (read > 0) {
+ String s = new String(chars);
+ tree.addText(s);
+ }
+ read = reader.read(chars, 0, maxlen);
+ }
+ } finally {
+ reader.close();
+ }
+ } else {
+ BufferedInputStream bufstream = new BufferedInputStream(zipFile);
+ try {
+ int maxlen = 4096 * 3;
+ byte[] bytes = new byte[maxlen];
+ int read = bufstream.read(bytes, 0, maxlen);
+ while (read >= 0) {
+ if (read > 0) {
+ String base64 = Base64.encodeBytes(bytes, 0, read);
+ tree.addText(base64 + "\n");
+ }
+ read = bufstream.read(bytes, 0, maxlen);
+ }
+ } finally {
+ bufstream.close();
}
- read = bufstream.read(bytes, 0, maxlen);
}
- bufstream.close();
- }
- tree.addEndElement();
- tree.endDocument();
- result.write(tree.getResult());
+ tree.addEndElement();
+ tree.endDocument();
+ result.write(tree.getResult());
+ }
}
-
+ } finally {
zipFile.close();
}
}
Oops, something went wrong.

0 comments on commit 30598d7

Please sign in to comment.