From fb45fc48199323ea75592d90b70084ce6bdcaac5 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Mon, 20 Jun 2022 07:03:59 +0200 Subject: [PATCH] Use Files.writeString() and Files.readString() where possible See gh-31459 --- .../build/bom/bomr/UpgradeApplicator.java | 10 ++++------ .../build/mavenplugin/MavenPluginPlugin.java | 2 +- .../bom/bomr/UpgradeApplicatorTests.java | 11 +++++----- .../docker/ssl/CertificateParser.java | 8 +------- .../platform/docker/ssl/PrivateKeyParser.java | 8 +------- .../gradle/plugin/ResolveMainClassName.java | 7 +++---- .../buildinfo/BuildInfoIntegrationTests.java | 11 +++++----- .../bundling/AbstractBootArchiveTests.java | 18 +++++++++-------- .../boot/maven/MavenBuild.java | 20 ++++++++----------- 9 files changed, 38 insertions(+), 57 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java index 1588ed7b5b..47e24ceb79 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java @@ -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); } } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/MavenPluginPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/MavenPluginPlugin.java index c124a922cf..9bb0bb1977 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/MavenPluginPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/MavenPluginPlugin.java @@ -309,7 +309,7 @@ public class MavenPluginPlugin implements Plugin { 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); diff --git a/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java b/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java index c1c2c9c85d..b397f09c28 100644 --- a/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java +++ b/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java @@ -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\")"); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java index 307d722fa3..2b5246a523 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java @@ -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 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); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java index 63e870b28d..7502a235dc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java @@ -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); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java index d96337216b..6d5b9c5026 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java @@ -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 + "'"); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java index a792c552e2..7f8080e323 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java @@ -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); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java index 2489dc6de1..13cd42238b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java @@ -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 { 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 { 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 diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java index 93ab76e2ac..2f0da3d83e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java @@ -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 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);