Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/main/java/org/apache/ibatis/io/DefaultVFS.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -78,13 +80,13 @@ public List<String> 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());
Expand Down Expand Up @@ -131,11 +133,11 @@ public List<String> 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);
}
Expand Down Expand Up @@ -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()) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/apache/ibatis/io/ExternalResources.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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);
}

}
Expand All @@ -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) {
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/org/apache/ibatis/io/Resources.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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
Expand All @@ -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();
}

/**
Expand Down
19 changes: 10 additions & 9 deletions src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Expand All @@ -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 || e instanceof NoSuchFileException);
}

}
Expand All @@ -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");
Expand Down