Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Avoid overwriting descriptors if the content is the same #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down