JDK-6216226 : StreamResult(File) does not close File after transform()
  • Type: Bug
  • Component: xml
  • Sub-Component: javax.xml.transform
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-01-11
  • Updated: 2012-04-25
  • Resolved: 2007-06-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6 JDK 7
1.4.0 1.4Fixed 6u4Fixed 7Fixed
Description
FULL PRODUCT VERSION :
java version "1.5.0-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-rc-b63)
Java HotSpot(TM) Client VM (build 1.5.0-rc-b63, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
If I use a StreamResult(File) the file is not closed after calling the transform() method. The file could not be deleted (or renamed) until the JVM ends.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the example source and run the program.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Delete:true
ACTUAL -
Delete:false

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Test {

	public static void main(String[] args) throws TransformerException, IOException {
		File test = new File("test.txt");
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer xformer = tf.newTransformer();
		StringReader st = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?><doc></doc>");
		StreamSource s = new StreamSource(st);
		StreamResult r = new StreamResult(test);
		xformer.transform(s,r);
		System.out.println("Delete:"+test.delete());
	}
}

---------- END SOURCE ----------
###@###.### 2005-1-11 18:31:27 GMT

Comments
EVALUATION This is now fixed in jaxp 1.4 on java.net. The bug is caused by an un-closed output stream when the result is StreamResult(file). The reason the bug was not reproducing as commented before was that the test was executed on Unix where open files could still be deleted.
13-06-2007

EVALUATION By popular demand, I'm re-opening this bug. We should re-evaluate it and try to reproduce it with the latest JDK 5 update.
11-06-2007

EVALUATION Could not reproduce the bug. [sk112103@@suresh-12:25pm]~/eclipsews/testsource/test1> java -showversion TestBug java version "1.5.0_01" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08) Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing) Delete:true Source code used for testing ************************************************************ import java.io.File; import java.io.IOException; import java.io.StringReader; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class TestBug { public static void main(String[] args) throws TransformerException, IOException { File test = new File("test.txt"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer xformer = tf.newTransformer(); StringReader st = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>< doc></doc>"); StreamSource s = new StreamSource(st); StreamResult r = new StreamResult(test); xformer.transform(s,r); System.out.println("Delete:"+test.delete()); } } *************************************************************** ###@###.### 2005-05-18 06:57:33 GMT
18-05-2005