Use Files.writeString() and Files.readString() where possible
See gh-31459
This commit is contained in:
committed by
Stephane Nicoll
parent
d9d1bdf0f6
commit
fb45fc4819
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.boot.build.bom.bomr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
@@ -42,7 +41,7 @@ class UpgradeApplicator {
|
||||
}
|
||||
|
||||
Path apply(Upgrade upgrade) throws IOException {
|
||||
String buildFileContents = new String(Files.readAllBytes(this.buildFile), StandardCharsets.UTF_8);
|
||||
String buildFileContents = Files.readString(this.buildFile);
|
||||
Matcher matcher = Pattern.compile("library\\(\"" + upgrade.getLibrary().getName() + "\", \"(.+)\"\\)")
|
||||
.matcher(buildFileContents);
|
||||
if (!matcher.find()) {
|
||||
@@ -68,7 +67,7 @@ class UpgradeApplicator {
|
||||
|
||||
private void updateGradleProperties(Upgrade upgrade, String version) throws IOException {
|
||||
String property = version.substring(2, version.length() - 1);
|
||||
String gradlePropertiesContents = new String(Files.readAllBytes(this.gradleProperties), StandardCharsets.UTF_8);
|
||||
String gradlePropertiesContents = Files.readString(this.gradleProperties);
|
||||
String modified = gradlePropertiesContents.replace(
|
||||
property + "=" + upgrade.getLibrary().getVersion().getVersion(), property + "=" + upgrade.getVersion());
|
||||
overwrite(this.gradleProperties, modified);
|
||||
@@ -82,8 +81,7 @@ class UpgradeApplicator {
|
||||
}
|
||||
|
||||
private void overwrite(Path target, String content) throws IOException {
|
||||
Files.write(target, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING);
|
||||
Files.writeString(target, content, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -309,7 +309,7 @@ public class MavenPluginPlugin implements Plugin<Project> {
|
||||
Path outputLocation = this.outputDir.toPath().resolve(relativePath);
|
||||
try {
|
||||
Files.createDirectories(outputLocation.getParent());
|
||||
Files.write(outputLocation, edit.getFormattedContent().getBytes(StandardCharsets.UTF_8));
|
||||
Files.writeString(outputLocation, edit.getFormattedContent());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new TaskExecutionException(this, ex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,6 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -49,13 +48,13 @@ class UpgradeApplicatorTests {
|
||||
void whenUpgradeIsAppliedToLibraryWithVersionThenBomIsUpdated() throws IOException {
|
||||
File bom = new File(this.temp, "bom.gradle");
|
||||
FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom);
|
||||
String originalContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
|
||||
String originalContents = Files.readString(bom.toPath());
|
||||
File gradleProperties = new File(this.temp, "gradle.properties");
|
||||
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
|
||||
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()).apply(new Upgrade(
|
||||
new Library("ActiveMQ", new LibraryVersion(DependencyVersion.parse("5.15.11"), null), null, null, null),
|
||||
DependencyVersion.parse("5.16")));
|
||||
String bomContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
|
||||
String bomContents = Files.readString(bom.toPath());
|
||||
assertThat(bomContents.length()).isEqualTo(originalContents.length() - 3);
|
||||
}
|
||||
|
||||
@@ -63,13 +62,13 @@ class UpgradeApplicatorTests {
|
||||
void whenUpgradeIsAppliedToLibraryWithAlignedVersionThenBomIsUpdated() throws IOException {
|
||||
File bom = new File(this.temp, "bom.gradle");
|
||||
FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom);
|
||||
String originalContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
|
||||
String originalContents = Files.readString(bom.toPath());
|
||||
File gradleProperties = new File(this.temp, "gradle.properties");
|
||||
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
|
||||
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()).apply(
|
||||
new Upgrade(new Library("OAuth2 OIDC SDK", new LibraryVersion(DependencyVersion.parse("8.36.1"), null),
|
||||
null, null, null), DependencyVersion.parse("8.36.2")));
|
||||
String bomContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
|
||||
String bomContents = Files.readString(bom.toPath());
|
||||
assertThat(bomContents.length()).isEqualTo(originalContents.length());
|
||||
assertThat(bomContents).contains("version(\"8.36.2\")");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user