diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/CachingWriter.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/CachingWriter.java new file mode 100644 index 000000000..42b55c5ea --- /dev/null +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/CachingWriter.java @@ -0,0 +1,56 @@ +package org.codehaus.plexus.metadata; + +/* + * Copyright (c) 2022, Codehaus.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Objects; + +public class CachingWriter extends StringWriter +{ + private final Path path; + private final Charset charset; + + public CachingWriter( Path path, Charset charset ) + { + this.path = Objects.requireNonNull( path ); + this.charset = Objects.requireNonNull( charset ); + } + + @Override + public void close() throws IOException + { + byte[] data = getBuffer().toString().getBytes( charset ); + if ( Files.exists( path ) && Files.size( path ) == data.length ) + { + byte[] ba = Files.readAllBytes( path ); + if ( Arrays.equals( data, ba ) ) + { + return; + } + } + Files.write( path, data ); + } + +} diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/DefaultMetadataGenerator.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/DefaultMetadataGenerator.java index d31999cb9..bd9a25831 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/DefaultMetadataGenerator.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/DefaultMetadataGenerator.java @@ -25,6 +25,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -185,7 +187,7 @@ private void writeDescriptor( ComponentSetDescriptor desc, File outputFile ) FileUtils.forceMkdir( outputFile.getParentFile() ); - BufferedWriter output = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( outputFile ), "UTF-8" ) ); + Writer output = new CachingWriter( outputFile.toPath(), StandardCharsets.UTF_8 ); try { diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/AbstractMerger.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/AbstractMerger.java index f20a06f1a..d5c5bc578 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/AbstractMerger.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/AbstractMerger.java @@ -26,8 +26,10 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.List; +import org.codehaus.plexus.metadata.CachingWriter; import org.codehaus.plexus.util.IOUtil; import org.jdom2.Document; import org.jdom2.JDOMException; @@ -61,7 +63,7 @@ public void writeMergedDocument( Document mergedDocument, File file ) Writer fw = null; try { - fw = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ); + fw = new CachingWriter( file.toPath(), StandardCharsets.UTF_8 ); out.output( mergedDocument, fw ); } finally