From bec19ec41d5feaf7ea0844c277932e9398f0515d Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 7 Mar 2023 19:11:30 -0500 Subject: [PATCH] replace deprecated methods that don't properly handle encoding (#1033) --- .../DefaultRepositoryMetadataManager.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java b/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java index a1c482708a..8741eca2c4 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java @@ -21,8 +21,9 @@ package org.apache.maven.artifact.repository.metadata; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.Reader; -import java.io.Writer; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -42,8 +43,6 @@ import org.apache.maven.wagon.TransferFailedException; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.AbstractLogEnabled; -import org.codehaus.plexus.util.ReaderFactory; -import org.codehaus.plexus.util.WriterFactory; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** @@ -267,19 +266,17 @@ public class DefaultRepositoryMetadataManager extends AbstractLogEnabled impleme * TODO share with DefaultPluginMappingManager. */ protected Metadata readMetadata(File mappingFile) throws RepositoryMetadataReadException { - Metadata result; - try (Reader reader = ReaderFactory.newXmlReader(mappingFile)) { - MetadataXpp3Reader mappingReader = new MetadataXpp3Reader(); - - result = mappingReader.read(reader, false); + MetadataXpp3Reader mappingReader = new MetadataXpp3Reader(); + try (InputStream in = Files.newInputStream(mappingFile.toPath())) { + Metadata result = mappingReader.read(in, false); + return result; } catch (FileNotFoundException e) { throw new RepositoryMetadataReadException("Cannot read metadata from '" + mappingFile + "'", e); } catch (IOException | XmlPullParserException e) { throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "': " + e.getMessage(), e); } - return result; } /** @@ -310,8 +307,8 @@ public class DefaultRepositoryMetadataManager extends AbstractLogEnabled impleme if (changed) { getLogger().debug("Repairing metadata in " + metadataFile); - try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) { - new MetadataXpp3Writer().write(writer, metadata); + try (OutputStream out = Files.newOutputStream(metadataFile.toPath())) { + new MetadataXpp3Writer().write(out, metadata); } catch (IOException e) { String msg = "Could not write fixed metadata to " + metadataFile + ": " + e.getMessage(); if (getLogger().isDebugEnabled()) {