Skip to content

Commit c455360

Browse files
authored
Merge pull request #3475 from hazendaz/nio-work
Modernize move to NIO
2 parents 16495da + 2558dfb commit c455360

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

src/main/java/org/apache/ibatis/io/DefaultVFS.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,9 @@
2626
import java.net.URLEncoder;
2727
import java.nio.charset.StandardCharsets;
2828
import java.nio.file.FileSystemException;
29+
import java.nio.file.Files;
2930
import java.nio.file.InvalidPathException;
31+
import java.nio.file.Path;
3032
import java.util.ArrayList;
3133
import java.util.Arrays;
3234
import java.util.List;
@@ -78,13 +80,13 @@ public List<String> list(URL url, String path) throws IOException {
7880
if (log.isDebugEnabled()) {
7981
log.debug("Listing " + url);
8082
}
81-
File destinationDir = new File(path);
83+
Path destinationDir = Path.of(path);
8284
for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null;) {
8385
if (log.isDebugEnabled()) {
8486
log.debug("Jar entry: " + entry.getName());
8587
}
86-
File entryFile = new File(destinationDir, entry.getName()).getCanonicalFile();
87-
if (!entryFile.getPath().startsWith(destinationDir.getCanonicalPath())) {
88+
File entryFile = destinationDir.resolve(entry.getName()).toFile().getCanonicalFile();
89+
if (!entryFile.getPath().startsWith(destinationDir.toFile().getCanonicalPath())) {
8890
throw new IOException("Bad zip entry: " + entry.getName());
8991
}
9092
children.add(entry.getName());
@@ -131,11 +133,11 @@ public List<String> list(URL url, String path) throws IOException {
131133
// No idea where the exception came from so rethrow it
132134
throw e;
133135
}
134-
File file = new File(url.getFile());
136+
File file = Path.of(url.getFile()).toFile();
135137
if (log.isDebugEnabled()) {
136138
log.debug("Listing directory " + file.getAbsolutePath());
137139
}
138-
if (file.isDirectory()) {
140+
if (Files.isDirectory(file.toPath())) {
139141
if (log.isDebugEnabled()) {
140142
log.debug("Listing " + url);
141143
}
@@ -273,11 +275,11 @@ protected URL findJarForResource(URL url) throws MalformedURLException {
273275
log.debug("Not a JAR: " + jarUrl);
274276
}
275277
jarUrl.replace(0, jarUrl.length(), testUrl.getFile());
276-
File file = new File(jarUrl.toString());
278+
File file = Path.of(jarUrl.toString()).toFile();
277279

278280
// File name might be URL-encoded
279281
if (!file.exists()) {
280-
file = new File(URLEncoder.encode(jarUrl.toString(), StandardCharsets.UTF_8));
282+
file = Path.of(URLEncoder.encode(jarUrl.toString(), StandardCharsets.UTF_8)).toFile();
281283
}
282284

283285
if (file.exists()) {

src/main/java/org/apache/ibatis/io/ExternalResources.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,12 @@
1616
package org.apache.ibatis.io;
1717

1818
import java.io.File;
19-
import java.io.FileInputStream;
2019
import java.io.FileNotFoundException;
21-
import java.io.FileOutputStream;
2220
import java.io.IOException;
2321
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
2425
import java.util.Properties;
2526

2627
import org.apache.ibatis.logging.Log;
@@ -43,9 +44,9 @@ public static void copyExternalResource(File sourceFile, File destFile) throws I
4344
destFile.createNewFile();
4445
}
4546

46-
try (FileInputStream source = new FileInputStream(sourceFile);
47-
FileOutputStream destination = new FileOutputStream(destFile)) {
48-
destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size());
47+
try (InputStream source = Files.newInputStream(sourceFile.toPath());
48+
OutputStream destination = Files.newOutputStream(destFile.toPath())) {
49+
source.transferTo(destination);
4950
}
5051

5152
}
@@ -55,7 +56,7 @@ public static String getConfiguredTemplate(String templatePath, String templateP
5556
String templateName = "";
5657
Properties migrationProperties = new Properties();
5758

58-
try (InputStream is = new FileInputStream(templatePath)) {
59+
try (InputStream is = Files.newInputStream(Path.of(templatePath))) {
5960
migrationProperties.load(is);
6061
templateName = migrationProperties.getProperty(templateProperty);
6162
} catch (FileNotFoundException e) {

src/main/java/org/apache/ibatis/io/Resources.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,11 @@
2020
import java.io.InputStream;
2121
import java.io.InputStreamReader;
2222
import java.io.Reader;
23+
import java.net.URISyntaxException;
2324
import java.net.URL;
2425
import java.net.URLConnection;
2526
import java.nio.charset.Charset;
27+
import java.nio.file.Path;
2628
import java.util.Properties;
2729

2830
/**
@@ -219,22 +221,24 @@ public static Reader getResourceAsReader(ClassLoader loader, String resource) th
219221
}
220222

221223
/**
222-
* Returns a resource on the classpath as a File object
224+
* Returns a resource on the classpath as a File object.
223225
*
224226
* @param resource
225227
* The resource to find
226228
*
227229
* @return The resource
228230
*
229-
* @throws java.io.IOException
230-
* If the resource cannot be found or read
231+
* @throws IOException
232+
* Signals that an I/O exception has occurred.
233+
* @throws URISyntaxException
234+
* the URI syntax exception
231235
*/
232-
public static File getResourceAsFile(String resource) throws IOException {
233-
return new File(getResourceURL(resource).getFile());
236+
public static File getResourceAsFile(String resource) throws IOException, URISyntaxException {
237+
return Path.of(getResourceURL(resource).toURI()).toFile();
234238
}
235239

236240
/**
237-
* Returns a resource on the classpath as a File object
241+
* Returns a resource on the classpath as a File object.
238242
*
239243
* @param loader
240244
* - the classloader used to fetch the resource
@@ -243,11 +247,13 @@ public static File getResourceAsFile(String resource) throws IOException {
243247
*
244248
* @return The resource
245249
*
246-
* @throws java.io.IOException
247-
* If the resource cannot be found or read
250+
* @throws IOException
251+
* Signals that an I/O exception has occurred.
252+
* @throws URISyntaxException
253+
* the URI syntax exception
248254
*/
249-
public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
250-
return new File(getResourceURL(loader, resource).getFile());
255+
public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException, URISyntaxException {
256+
return Path.of(getResourceURL(loader, resource).toURI()).toFile();
251257
}
252258

253259
/**

src/test/java/org/apache/ibatis/io/ExternalResourcesTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
import static org.junit.jupiter.api.Assertions.assertTrue;
2121
import static org.junit.jupiter.api.Assertions.fail;
2222

23+
import java.io.BufferedWriter;
2324
import java.io.File;
24-
import java.io.FileNotFoundException;
25-
import java.io.FileWriter;
26-
import java.io.IOException;
2725
import java.nio.charset.StandardCharsets;
2826
import java.nio.file.Files;
27+
import java.nio.file.InvalidPathException;
28+
import java.nio.file.NoSuchFileException;
29+
import java.nio.file.Path;
2930

3031
import org.junit.jupiter.api.AfterEach;
3132
import org.junit.jupiter.api.BeforeEach;
@@ -61,10 +62,10 @@ void testcopyExternalResource() {
6162
void testcopyExternalResource_fileNotFound() {
6263

6364
try {
64-
badFile = new File("/tmp/nofile.sql");
65+
badFile = Path.of("/tmp/nofile.sql").toFile();
6566
ExternalResources.copyExternalResource(badFile, destFile);
66-
} catch (IOException e) {
67-
assertTrue(e instanceof FileNotFoundException);
67+
} catch (Exception e) {
68+
assertTrue(e instanceof NoSuchFileException);
6869
}
6970

7071
}
@@ -73,10 +74,10 @@ void testcopyExternalResource_fileNotFound() {
7374
void testcopyExternalResource_emptyStringAsFile() {
7475

7576
try {
76-
badFile = new File(" ");
77+
badFile = Path.of(" ").toFile();
7778
ExternalResources.copyExternalResource(badFile, destFile);
7879
} catch (Exception e) {
79-
assertTrue(e instanceof FileNotFoundException);
80+
assertTrue(e instanceof InvalidPathException || e instanceof NoSuchFileException);
8081
}
8182

8283
}
@@ -85,7 +86,7 @@ void testcopyExternalResource_emptyStringAsFile() {
8586
void getConfiguredTemplate() {
8687
String templateName = "";
8788

88-
try (FileWriter fileWriter = new FileWriter(tempFile, StandardCharsets.UTF_8)) {
89+
try (BufferedWriter fileWriter = Files.newBufferedWriter(tempFile.toPath(), StandardCharsets.UTF_8)) {
8990
fileWriter.append("new_command.template=templates/col_new_template_migration.sql");
9091
fileWriter.flush();
9192
templateName = ExternalResources.getConfiguredTemplate(tempFile.getAbsolutePath(), "new_command.template");

0 commit comments

Comments
 (0)