From: Robert Varga Date: Sun, 21 Feb 2021 15:55:05 +0000 (+0100) Subject: Use java.nio.file for file operations X-Git-Tag: v1.13.1~5 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=135e2a94bda4db022f08b657a9ecff71083e9611;p=netconf.git Use java.nio.file for file operations java.nio.file is more expressive than java.io.File, use it to perform reads and atomic renames. Also clean up constant use and assertions a bit. JIRA: NETCONF-756 Change-Id: Ia42c61f8cca4253a7db297e188053fcf2b63ec35 Signed-off-by: Robert Varga --- diff --git a/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java b/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java index def348e33b..22aa014bb2 100644 --- a/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java +++ b/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java @@ -11,7 +11,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import com.google.common.io.CharStreams; -import com.google.common.io.Files; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.BufferedReader; import java.io.File; @@ -21,6 +20,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -34,6 +36,7 @@ import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.annotation.Arg; import net.sourceforge.argparse4j.inf.ArgumentParser; import net.sourceforge.argparse4j.inf.ArgumentParserException; +import org.opendaylight.yangtools.yang.common.YangConstants; @SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING"}) public class TesttoolParameters { @@ -313,7 +316,7 @@ public class TesttoolParameters { try { final String correctName = correctedName(file); if (correctName != null) { - Files.move(file, new File(correctName)); + Files.move(file.toPath(), Paths.get(correctName), StandardCopyOption.ATOMIC_MOVE); } } catch (final IOException e) { // print error to console (test tool is running from console) @@ -337,13 +340,13 @@ public class TesttoolParameters { } if (line != null) { final Matcher m = REVISION_DATE_PATTERN.matcher(line); - checkState(m.find()); + checkState(m.find(), "Revision pattern %s did not match line %s", REVISION_DATE_PATTERN, line); String moduleName = file.getAbsolutePath(); - if (file.getName().endsWith(".yang")) { + if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) { moduleName = moduleName.substring(0, moduleName.length() - 5); } - final String revision = m.group(1); - return moduleName + "@" + revision + ".yang"; + + return moduleName + "@" + m.group(1) + YangConstants.RFC6020_YANG_FILE_EXTENSION; } } return null; @@ -354,7 +357,7 @@ public class TesttoolParameters { final String editContentString; try { if (stream == null) { - editContentString = Files.asCharSource(editContent, StandardCharsets.UTF_8).read(); + editContentString = Files.readString(editContent.toPath()); } else { editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8)); }