Use Files.writeString() and Files.readString() where possible

See gh-31459
This commit is contained in:
dreis2211
2022-06-20 07:03:59 +02:00
committed by Stephane Nicoll
parent d9d1bdf0f6
commit fb45fc4819
9 changed files with 38 additions and 57 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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 + "'");

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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);