From 303279582ec4a2ed9ee4be7c90002ed038902eba Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Mon, 26 May 2025 21:50:02 -0400 Subject: [PATCH 1/2] Modernize move to NIO --- .../java/org/apache/ibatis/io/DefaultVFS.java | 18 ++++++------ .../apache/ibatis/io/ExternalResources.java | 15 +++++----- .../java/org/apache/ibatis/io/Resources.java | 28 +++++++++++-------- .../ibatis/io/ExternalResourcesTest.java | 19 +++++++------ 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/apache/ibatis/io/DefaultVFS.java b/src/main/java/org/apache/ibatis/io/DefaultVFS.java index a53b278f558..eb213707edb 100644 --- a/src/main/java/org/apache/ibatis/io/DefaultVFS.java +++ b/src/main/java/org/apache/ibatis/io/DefaultVFS.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2024 the original author or authors. + * Copyright 2009-2025 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. @@ -26,7 +26,9 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystemException; +import java.nio.file.Files; import java.nio.file.InvalidPathException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -78,13 +80,13 @@ public List list(URL url, String path) throws IOException { if (log.isDebugEnabled()) { log.debug("Listing " + url); } - File destinationDir = new File(path); + Path destinationDir = Path.of(path); for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null;) { if (log.isDebugEnabled()) { log.debug("Jar entry: " + entry.getName()); } - File entryFile = new File(destinationDir, entry.getName()).getCanonicalFile(); - if (!entryFile.getPath().startsWith(destinationDir.getCanonicalPath())) { + File entryFile = destinationDir.resolve(entry.getName()).toFile().getCanonicalFile(); + if (!entryFile.getPath().startsWith(destinationDir.toFile().getCanonicalPath())) { throw new IOException("Bad zip entry: " + entry.getName()); } children.add(entry.getName()); @@ -131,11 +133,11 @@ public List list(URL url, String path) throws IOException { // No idea where the exception came from so rethrow it throw e; } - File file = new File(url.getFile()); + File file = Path.of(url.getFile()).toFile(); if (log.isDebugEnabled()) { log.debug("Listing directory " + file.getAbsolutePath()); } - if (file.isDirectory()) { + if (Files.isDirectory(file.toPath())) { if (log.isDebugEnabled()) { log.debug("Listing " + url); } @@ -273,11 +275,11 @@ protected URL findJarForResource(URL url) throws MalformedURLException { log.debug("Not a JAR: " + jarUrl); } jarUrl.replace(0, jarUrl.length(), testUrl.getFile()); - File file = new File(jarUrl.toString()); + File file = Path.of(jarUrl.toString()).toFile(); // File name might be URL-encoded if (!file.exists()) { - file = new File(URLEncoder.encode(jarUrl.toString(), StandardCharsets.UTF_8)); + file = Path.of(URLEncoder.encode(jarUrl.toString(), StandardCharsets.UTF_8)).toFile(); } if (file.exists()) { diff --git a/src/main/java/org/apache/ibatis/io/ExternalResources.java b/src/main/java/org/apache/ibatis/io/ExternalResources.java index b713133d4c8..8ddd9556741 100644 --- a/src/main/java/org/apache/ibatis/io/ExternalResources.java +++ b/src/main/java/org/apache/ibatis/io/ExternalResources.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2025 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. @@ -16,11 +16,12 @@ package org.apache.ibatis.io; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Properties; import org.apache.ibatis.logging.Log; @@ -43,9 +44,9 @@ public static void copyExternalResource(File sourceFile, File destFile) throws I destFile.createNewFile(); } - try (FileInputStream source = new FileInputStream(sourceFile); - FileOutputStream destination = new FileOutputStream(destFile)) { - destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size()); + try (InputStream source = Files.newInputStream(sourceFile.toPath()); + OutputStream destination = Files.newOutputStream(destFile.toPath())) { + source.transferTo(destination); } } @@ -55,7 +56,7 @@ public static String getConfiguredTemplate(String templatePath, String templateP String templateName = ""; Properties migrationProperties = new Properties(); - try (InputStream is = new FileInputStream(templatePath)) { + try (InputStream is = Files.newInputStream(Path.of(templatePath))) { migrationProperties.load(is); templateName = migrationProperties.getProperty(templateProperty); } catch (FileNotFoundException e) { diff --git a/src/main/java/org/apache/ibatis/io/Resources.java b/src/main/java/org/apache/ibatis/io/Resources.java index c3c3ee6ad3f..772745b7895 100644 --- a/src/main/java/org/apache/ibatis/io/Resources.java +++ b/src/main/java/org/apache/ibatis/io/Resources.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2025 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,9 +20,11 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.Properties; /** @@ -219,22 +221,24 @@ public static Reader getResourceAsReader(ClassLoader loader, String resource) th } /** - * Returns a resource on the classpath as a File object + * Returns a resource on the classpath as a File object. * * @param resource * The resource to find * * @return The resource * - * @throws java.io.IOException - * If the resource cannot be found or read + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws URISyntaxException + * the URI syntax exception */ - public static File getResourceAsFile(String resource) throws IOException { - return new File(getResourceURL(resource).getFile()); + public static File getResourceAsFile(String resource) throws IOException, URISyntaxException { + return Path.of(getResourceURL(resource).toURI()).toFile(); } /** - * Returns a resource on the classpath as a File object + * Returns a resource on the classpath as a File object. * * @param loader * - the classloader used to fetch the resource @@ -243,11 +247,13 @@ public static File getResourceAsFile(String resource) throws IOException { * * @return The resource * - * @throws java.io.IOException - * If the resource cannot be found or read + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws URISyntaxException + * the URI syntax exception */ - public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException { - return new File(getResourceURL(loader, resource).getFile()); + public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException, URISyntaxException { + return Path.of(getResourceURL(loader, resource).toURI()).toFile(); } /** diff --git a/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java b/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java index 895c2ddca68..de45446313b 100644 --- a/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java +++ b/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java @@ -20,12 +20,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -61,10 +62,10 @@ void testcopyExternalResource() { void testcopyExternalResource_fileNotFound() { try { - badFile = new File("/tmp/nofile.sql"); + badFile = Path.of("/tmp/nofile.sql").toFile(); ExternalResources.copyExternalResource(badFile, destFile); - } catch (IOException e) { - assertTrue(e instanceof FileNotFoundException); + } catch (Exception e) { + assertTrue(e instanceof NoSuchFileException); } } @@ -73,10 +74,10 @@ void testcopyExternalResource_fileNotFound() { void testcopyExternalResource_emptyStringAsFile() { try { - badFile = new File(" "); + badFile = Path.of(" ").toFile(); ExternalResources.copyExternalResource(badFile, destFile); } catch (Exception e) { - assertTrue(e instanceof FileNotFoundException); + assertTrue(e instanceof InvalidPathException); } } @@ -85,7 +86,7 @@ void testcopyExternalResource_emptyStringAsFile() { void getConfiguredTemplate() { String templateName = ""; - try (FileWriter fileWriter = new FileWriter(tempFile, StandardCharsets.UTF_8)) { + try (BufferedWriter fileWriter = Files.newBufferedWriter(tempFile.toPath(), StandardCharsets.UTF_8)) { fileWriter.append("new_command.template=templates/col_new_template_migration.sql"); fileWriter.flush(); templateName = ExternalResources.getConfiguredTemplate(tempFile.getAbsolutePath(), "new_command.template"); From 2558dfb998ba5d47f0b73a51ae42bbdff2ecd848 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Mon, 26 May 2025 22:00:38 -0400 Subject: [PATCH 2/2] [test] Try no such file exception --- src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java b/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java index de45446313b..abb1b165ecb 100644 --- a/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java +++ b/src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java @@ -77,7 +77,7 @@ void testcopyExternalResource_emptyStringAsFile() { badFile = Path.of(" ").toFile(); ExternalResources.copyExternalResource(badFile, destFile); } catch (Exception e) { - assertTrue(e instanceof InvalidPathException); + assertTrue(e instanceof InvalidPathException || e instanceof NoSuchFileException); } }