This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/xml2fastinfoset in repository https://gitbox.apache.org/repos/asf/cxf-build-utils.git
commit a549ad13dbeee371ce12e52aa849fcee0f97fe01 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Thu Oct 8 16:53:31 2026 +0100 Avoid partial or stale FastInfoset output on conversion failure --- .../XML2FastInfosetCompilerMojo.java | 41 ++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/xml2fastinfoset-plugin/src/main/java/org/apache/cxf/maven_plugin/xml2fastinfoset/XML2FastInfosetCompilerMojo.java b/xml2fastinfoset-plugin/src/main/java/org/apache/cxf/maven_plugin/xml2fastinfoset/XML2FastInfosetCompilerMojo.java index fbfcfa4..10928eb 100644 --- a/xml2fastinfoset-plugin/src/main/java/org/apache/cxf/maven_plugin/xml2fastinfoset/XML2FastInfosetCompilerMojo.java +++ b/xml2fastinfoset-plugin/src/main/java/org/apache/cxf/maven_plugin/xml2fastinfoset/XML2FastInfosetCompilerMojo.java @@ -20,11 +20,13 @@ package org.apache.cxf.maven_plugin.xml2fastinfoset; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; @@ -184,24 +186,27 @@ public class XML2FastInfosetCompilerMojo extends AbstractMojo { private void compileFile(File sourceFile, File destinationFile) throws ParserConfigurationException, SAXException, IOException { - FileInputStream fis = null; - FileOutputStream fos = null; + // Write to a temporary file and only move it into place once the conversion has succeeded, + // so that a failed conversion never leaves a partial or stale destination file behind. + Path destination = destinationFile.toPath(); + Path tmp = destination.resolveSibling(destination.getFileName() + ".tmp"); + boolean success = false; try { - fis = new FileInputStream(sourceFile); - fos = new FileOutputStream(destinationFile); - dehydrate(fis, fos); - fis.close(); - fos.close(); - } finally { + try (InputStream is = Files.newInputStream(sourceFile.toPath()); + OutputStream os = Files.newOutputStream(tmp)) { + dehydrate(is, os); + } try { - if (fis != null) { - fis.close(); - } - if (fos != null) { - fos.close(); - } - } catch (Exception e) { - // nothing. + Files.move(tmp, destination, StandardCopyOption.REPLACE_EXISTING, + StandardCopyOption.ATOMIC_MOVE); + } catch (AtomicMoveNotSupportedException e) { + Files.move(tmp, destination, StandardCopyOption.REPLACE_EXISTING); + } + success = true; + } finally { + Files.deleteIfExists(tmp); + if (!success) { + Files.deleteIfExists(destination); } } }
