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\")");
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.buildpack.platform.docker.ssl;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.cert.CertificateException;
|
||||
@@ -76,7 +75,7 @@ final class CertificateParser {
|
||||
|
||||
private static void readCertificates(Path path, CertificateFactory factory, Consumer<X509Certificate> consumer) {
|
||||
try {
|
||||
String text = readText(path);
|
||||
String text = Files.readString(path);
|
||||
Matcher matcher = PATTERN.matcher(text);
|
||||
while (matcher.find()) {
|
||||
String encodedText = matcher.group(1);
|
||||
@@ -92,11 +91,6 @@ final class CertificateParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static String readText(Path path) throws IOException {
|
||||
byte[] bytes = Files.readAllBytes(path);
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte[] decodeBase64(String content) {
|
||||
byte[] bytes = content.replaceAll("\r", "").replaceAll("\n", "").getBytes();
|
||||
return Base64Utils.decode(bytes);
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.buildpack.platform.docker.ssl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.GeneralSecurityException;
|
||||
@@ -65,7 +64,7 @@ final class PrivateKeyParser {
|
||||
*/
|
||||
static PrivateKey parse(Path path) {
|
||||
try {
|
||||
String text = readText(path);
|
||||
String text = Files.readString(path);
|
||||
Matcher matcher = PKCS1_PATTERN.matcher(text);
|
||||
if (matcher.find()) {
|
||||
return parsePkcs1(decodeBase64(matcher.group(1)));
|
||||
@@ -132,11 +131,6 @@ final class PrivateKeyParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static String readText(Path path) throws IOException {
|
||||
byte[] bytes = Files.readAllBytes(path);
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte[] decodeBase64(String content) {
|
||||
byte[] contentBytes = content.replaceAll("\r", "").replaceAll("\n", "").getBytes();
|
||||
return Base64Utils.decode(contentBytes);
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.gradle.plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
@@ -122,8 +121,8 @@ public class ResolveMainClassName extends DefaultTask {
|
||||
File outputFile = this.outputFile.getAsFile().get();
|
||||
outputFile.getParentFile().mkdirs();
|
||||
String mainClassName = resolveMainClassName();
|
||||
Files.write(outputFile.toPath(), mainClassName.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
Files.writeString(outputFile.toPath(), mainClassName, StandardOpenOption.WRITE, StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING);
|
||||
}
|
||||
|
||||
private String resolveMainClassName() {
|
||||
@@ -158,7 +157,7 @@ public class ResolveMainClassName extends DefaultTask {
|
||||
}
|
||||
Path output = file.getAsFile().toPath();
|
||||
try {
|
||||
return new String(Files.readAllBytes(output), StandardCharsets.UTF_8);
|
||||
return Files.readString(output);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException("Failed to read main class name from '" + output + "'");
|
||||
|
||||
@@ -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.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.boot.gradle.tasks.buildinfo;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
@@ -101,11 +100,11 @@ class BuildInfoIntegrationTests {
|
||||
@TestTemplate
|
||||
void notUpToDateWhenExecutedTwiceWithFixedTimeAndChangedGradlePropertiesProjectVersion() throws IOException {
|
||||
Path gradleProperties = new File(this.gradleBuild.getProjectDir(), "gradle.properties").toPath();
|
||||
Files.write(gradleProperties, "version=0.1.0".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
|
||||
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
Files.writeString(gradleProperties, "version=0.1.0", StandardOpenOption.CREATE, StandardOpenOption.WRITE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING);
|
||||
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
Files.write(gradleProperties, "version=0.2.0".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
|
||||
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
Files.writeString(gradleProperties, "version=0.2.0", StandardOpenOption.CREATE, StandardOpenOption.WRITE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING);
|
||||
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,12 @@ import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -294,11 +295,11 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
|
||||
void customLaunchScriptCanBePrepended() throws IOException {
|
||||
this.task.getMainClass().set("com.example.Main");
|
||||
File customScript = new File(this.temp, "custom.script");
|
||||
Files.write(customScript.toPath(), Arrays.asList("custom script"), StandardOpenOption.CREATE);
|
||||
Files.writeString(customScript.toPath(), "custom script", StandardOpenOption.CREATE);
|
||||
this.task.launchScript((configuration) -> configuration.setScript(customScript));
|
||||
executeTask();
|
||||
assertThat(Files.readAllBytes(this.task.getArchiveFile().get().getAsFile().toPath()))
|
||||
.startsWith("custom script".getBytes());
|
||||
Path path = this.task.getArchiveFile().get().getAsFile().toPath();
|
||||
assertThat(Files.readString(path, StandardCharsets.ISO_8859_1)).startsWith("custom script");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -310,10 +311,11 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
|
||||
configuration.getProperties().put("initInfoDescription", "description");
|
||||
});
|
||||
executeTask();
|
||||
byte[] bytes = Files.readAllBytes(this.task.getArchiveFile().get().getAsFile().toPath());
|
||||
assertThat(bytes).containsSequence("Provides: provides".getBytes());
|
||||
assertThat(bytes).containsSequence("Short-Description: short description".getBytes());
|
||||
assertThat(bytes).containsSequence("Description: description".getBytes());
|
||||
Path path = this.task.getArchiveFile().get().getAsFile().toPath();
|
||||
String content = Files.readString(path, StandardCharsets.ISO_8859_1);
|
||||
assertThat(content).containsSequence("Provides: provides");
|
||||
assertThat(content).containsSequence("Short-Description: short description");
|
||||
assertThat(content).containsSequence("Description: description");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -140,12 +139,12 @@ class MavenBuild {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if (file.toFile().getName().equals("pom.xml")) {
|
||||
String pomXml = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
|
||||
String pomXml = Files.readString(file);
|
||||
for (Entry<String, String> replacement : MavenBuild.this.pomReplacements.entrySet()) {
|
||||
pomXml = pomXml.replace("@" + replacement.getKey() + "@", replacement.getValue());
|
||||
}
|
||||
Files.write(destination.resolve(source.relativize(file)),
|
||||
pomXml.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW);
|
||||
Files.writeString(destination.resolve(source.relativize(file)), pomXml,
|
||||
StandardOpenOption.CREATE_NEW);
|
||||
}
|
||||
else {
|
||||
Files.copy(file, destination.resolve(source.relativize(file)),
|
||||
@@ -155,14 +154,11 @@ class MavenBuild {
|
||||
}
|
||||
|
||||
});
|
||||
String settingsXml = new String(Files.readAllBytes(Paths.get("src", "intTest", "projects", "settings.xml")),
|
||||
StandardCharsets.UTF_8)
|
||||
.replace("@localCentralUrl@",
|
||||
new File("build/int-test-maven-repository").toURI().toURL().toString())
|
||||
.replace("@localRepositoryPath@",
|
||||
new File("build/local-maven-repository").getAbsolutePath());
|
||||
Files.write(destination.resolve("settings.xml"), settingsXml.getBytes(StandardCharsets.UTF_8),
|
||||
StandardOpenOption.CREATE_NEW);
|
||||
String settingsXml = Files.readString(Paths.get("src", "intTest", "projects", "settings.xml"))
|
||||
.replace("@localCentralUrl@",
|
||||
new File("build/int-test-maven-repository").toURI().toURL().toString())
|
||||
.replace("@localRepositoryPath@", new File("build/local-maven-repository").getAbsolutePath());
|
||||
Files.writeString(destination.resolve("settings.xml"), settingsXml, StandardOpenOption.CREATE_NEW);
|
||||
request.setBaseDirectory(this.temp);
|
||||
request.setJavaHome(new File(System.getProperty("java.home")));
|
||||
request.setProperties(this.properties);
|
||||
|
||||
Reference in New Issue
Block a user