Skip to content

Commit 25e68ef

Browse files
committed
Use NIO to make modernizer happy
1 parent 91bd882 commit 25e68ef

File tree

17 files changed

+92
-83
lines changed

17 files changed

+92
-83
lines changed

src/main/java/org/apache/ibatis/migration/Environment.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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,10 +16,11 @@
1616
package org.apache.ibatis.migration;
1717

1818
import java.io.File;
19-
import java.io.FileInputStream;
2019
import java.io.FileNotFoundException;
2120
import java.io.IOException;
21+
import java.io.InputStream;
2222
import java.nio.charset.Charset;
23+
import java.nio.file.Files;
2324
import java.util.ArrayList;
2425
import java.util.Arrays;
2526
import java.util.Collections;
@@ -208,7 +209,7 @@ private boolean isMigrationsKey(String key) {
208209

209210
private Properties loadPropertiesFromFile(File file) {
210211
Properties properties = new Properties();
211-
try (FileInputStream inputStream = new FileInputStream(file)) {
212+
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
212213
properties.load(inputStream);
213214
return properties;
214215
} catch (FileNotFoundException e) {

src/main/java/org/apache/ibatis/migration/MigrationReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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,13 +16,13 @@
1616
package org.apache.ibatis.migration;
1717

1818
import java.io.File;
19-
import java.io.FileInputStream;
2019
import java.io.FilterReader;
2120
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.io.InputStreamReader;
2423
import java.io.Reader;
2524
import java.nio.charset.Charset;
25+
import java.nio.file.Files;
2626
import java.util.Properties;
2727

2828
public class MigrationReader extends FilterReader {
@@ -85,7 +85,7 @@ private enum VariableStatus {
8585
}
8686

8787
public MigrationReader(File file, String charset, boolean undo, Properties variables) throws IOException {
88-
this(new FileInputStream(file), charset, undo, variables);
88+
this(Files.newInputStream(file.toPath()), charset, undo, variables);
8989
}
9090

9191
public MigrationReader(InputStream inputStream, String charset, boolean undo, Properties variables) {

src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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.
@@ -18,8 +18,6 @@
1818
import static org.apache.ibatis.migration.utils.Util.file;
1919

2020
import java.io.File;
21-
import java.io.FileReader;
22-
import java.io.FileWriter;
2321
import java.io.IOException;
2422
import java.io.LineNumberReader;
2523
import java.io.OutputStream;
@@ -28,6 +26,8 @@
2826
import java.io.Reader;
2927
import java.net.URL;
3028
import java.net.URLClassLoader;
29+
import java.nio.file.Files;
30+
import java.nio.file.Paths;
3131
import java.text.DecimalFormat;
3232
import java.text.ParseException;
3333
import java.text.SimpleDateFormat;
@@ -161,7 +161,7 @@ protected void copyResourceTo(String resource, File toFile, Properties variables
161161
protected void copyExternalResourceTo(String resource, File toFile, Properties variables) {
162162
printStream.println("Creating: " + toFile.getName());
163163
try {
164-
File sourceFile = new File(resource);
164+
File sourceFile = Paths.get(resource).toFile();
165165
copyTemplate(sourceFile, toFile, variables);
166166
} catch (Exception e) {
167167
throw new MigrationException("Error copying " + resource + " to " + toFile.getAbsolutePath() + ". Cause: " + e,
@@ -170,15 +170,15 @@ protected void copyExternalResourceTo(String resource, File toFile, Properties v
170170
}
171171

172172
protected static void copyTemplate(File templateFile, File toFile, Properties variables) throws IOException {
173-
try (FileReader reader = new FileReader(templateFile)) {
173+
try (Reader reader = Files.newBufferedReader(templateFile.toPath())) {
174174
copyTemplate(reader, toFile, variables);
175175
}
176176
}
177177

178178
protected static void copyTemplate(Reader templateReader, File toFile, Properties variables) throws IOException {
179179
VariableReplacer replacer = new VariableReplacer(variables);
180180
try (LineNumberReader reader = new LineNumberReader(templateReader);
181-
PrintWriter writer = new PrintWriter(new FileWriter(toFile))) {
181+
PrintWriter writer = new PrintWriter(Files.newBufferedWriter(toFile.toPath()))) {
182182
String line;
183183
while ((line = reader.readLine()) != null) {
184184
line = replacer.replace(line);
@@ -259,7 +259,7 @@ private ClassLoader getDriverClassLoader() {
259259
private File getCustomDriverPath() {
260260
String customDriverPath = environment().getDriverPath();
261261
if (customDriverPath != null && customDriverPath.length() > 0) {
262-
return new File(customDriverPath);
262+
return Paths.get(customDriverPath).toFile();
263263
}
264264
return options.getPaths().getDriverPath();
265265
}

src/main/java/org/apache/ibatis/migration/hook/FileHookScriptFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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.
@@ -58,7 +58,7 @@ public HookScript create(String hookSetting) {
5858
// First segment is language
5959
String scriptLang = segments[0];
6060
// Second segment is file
61-
File scriptFile = new File(hooksDir, segments[1]);
61+
File scriptFile = hooksDir.toPath().resolve(segments[1]).toFile();
6262
// The rest are script dependent options
6363
String[] hookOptions = Arrays.copyOfRange(segments, 2, segments.length);
6464
if (!scriptFile.exists()) {

src/main/java/org/apache/ibatis/migration/hook/Jsr223HookScript.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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,11 @@
1616
package org.apache.ibatis.migration.hook;
1717

1818
import java.io.File;
19-
import java.io.FileInputStream;
2019
import java.io.IOException;
2120
import java.io.InputStreamReader;
2221
import java.io.PrintStream;
2322
import java.nio.charset.Charset;
23+
import java.nio.file.Files;
2424
import java.util.ArrayList;
2525
import java.util.HashMap;
2626
import java.util.List;
@@ -102,8 +102,8 @@ public void execute(Map<String, Object> bindingMap) {
102102
bindings.putAll(bindingMap);
103103
try {
104104
printStream.println(Util.horizontalLine("Applying JSR-223 hook : " + scriptFile.getName(), 80));
105-
try (
106-
InputStreamReader stream = new InputStreamReader(new FileInputStream(scriptFile), Charset.forName(charset))) {
105+
try (InputStreamReader stream = new InputStreamReader(Files.newInputStream(scriptFile.toPath()),
106+
Charset.forName(charset))) {
107107
engine.eval(stream);
108108
}
109109
if (functionName != null || objectName != null && methodName != null) {

src/main/java/org/apache/ibatis/migration/hook/SqlHookScript.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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.
@@ -17,10 +17,11 @@
1717

1818
import java.io.ByteArrayOutputStream;
1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.IOException;
21+
import java.io.InputStream;
2222
import java.io.PrintStream;
2323
import java.io.StringReader;
24+
import java.nio.file.Files;
2425
import java.util.Map;
2526
import java.util.Properties;
2627

@@ -57,7 +58,7 @@ public void execute(Map<String, Object> bindingMap) {
5758
HookContext context = (HookContext) bindingMap.get(MigrationHook.HOOK_CONTEXT);
5859
printStream.println(Util.horizontalLine("Applying SQL hook: " + scriptFile.getName(), 80));
5960

60-
try (FileInputStream inputStream = new FileInputStream(scriptFile);
61+
try (InputStream inputStream = Files.newInputStream(scriptFile.toPath());
6162
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
6263
byte[] buffer = new byte[1024];
6364
int length;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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,6 +26,7 @@
2626
import java.net.URL;
2727
import java.net.URLEncoder;
2828
import java.nio.file.InvalidPathException;
29+
import java.nio.file.Paths;
2930
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.List;
@@ -124,7 +125,7 @@ public List<String> list(URL url, String path) throws IOException {
124125
// No idea where the exception came from so rethrow it
125126
throw e;
126127
}
127-
File file = new File(url.getFile());
128+
File file = Paths.get(url.getFile()).toFile();
128129
if (log.isLoggable(Level.FINER)) {
129130
log.log(Level.FINER, "Listing directory " + file.getAbsolutePath());
130131
}
@@ -266,12 +267,12 @@ protected URL findJarForResource(URL url) throws MalformedURLException {
266267
log.log(Level.FINER, "Not a JAR: " + jarUrl);
267268
}
268269
jarUrl.replace(0, jarUrl.length(), testUrl.getFile());
269-
File file = new File(jarUrl.toString());
270+
File file = Paths.get(jarUrl.toString()).toFile();
270271

271272
// File name might be URL-encoded
272273
if (!file.exists()) {
273274
try {
274-
file = new File(URLEncoder.encode(jarUrl.toString(), "UTF-8"));
275+
file = Paths.get(URLEncoder.encode(jarUrl.toString(), "UTF-8")).toFile();
275276
} catch (UnsupportedEncodingException e) {
276277
throw new RuntimeException("Unsupported encoding? UTF-8? That's impossible.");
277278
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-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.
@@ -23,6 +23,7 @@
2323
import java.net.URL;
2424
import java.net.URLConnection;
2525
import java.nio.charset.Charset;
26+
import java.nio.file.Paths;
2627
import java.util.Properties;
2728

2829
/**
@@ -227,7 +228,7 @@ public static Reader getResourceAsReader(ClassLoader loader, String resource) th
227228
* If the resource cannot be found or read
228229
*/
229230
public static File getResourceAsFile(String resource) throws IOException {
230-
return new File(getResourceURL(resource).getFile());
231+
return Paths.get(getResourceURL(resource).getFile()).toFile();
231232
}
232233

233234
/**
@@ -244,7 +245,7 @@ public static File getResourceAsFile(String resource) throws IOException {
244245
* If the resource cannot be found or read
245246
*/
246247
public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
247-
return new File(getResourceURL(loader, resource).getFile());
248+
return Paths.get(getResourceURL(loader, resource).getFile()).toFile();
248249
}
249250

250251
/**

src/main/java/org/apache/ibatis/migration/options/OptionsParser.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 the original author or authors.
2+
* Copyright 2010-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.
@@ -17,7 +17,7 @@
1717

1818
import static org.apache.ibatis.migration.utils.Util.isOption;
1919

20-
import java.io.File;
20+
import java.nio.file.Paths;
2121

2222
public enum OptionsParser {
2323
;
@@ -55,19 +55,19 @@ private static boolean parseOptions(String arg, SelectedOptions options) {
5555

5656
switch (option) {
5757
case PATH:
58-
options.getPaths().setBasePath(new File(argParts[1]));
58+
options.getPaths().setBasePath(Paths.get(argParts[1]).toFile());
5959
break;
6060
case ENVPATH:
61-
options.getPaths().setEnvPath(new File(argParts[1]));
61+
options.getPaths().setEnvPath(Paths.get(argParts[1]).toFile());
6262
break;
6363
case SCRIPTPATH:
64-
options.getPaths().setScriptPath(new File(argParts[1]));
64+
options.getPaths().setScriptPath(Paths.get(argParts[1]).toFile());
6565
break;
6666
case DRIVERPATH:
67-
options.getPaths().setDriverPath(new File(argParts[1]));
67+
options.getPaths().setDriverPath(Paths.get(argParts[1]).toFile());
6868
break;
6969
case HOOKPATH:
70-
options.getPaths().setHookPath(new File(argParts[1]));
70+
options.getPaths().setHookPath(Paths.get(argParts[1]).toFile());
7171
break;
7272
case ENV:
7373
options.setEnvironment(argParts[1]);

src/main/java/org/apache/ibatis/migration/options/SelectedPaths.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 the original author or authors.
2+
* Copyright 2010-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.
@@ -18,9 +18,10 @@
1818
import static org.apache.ibatis.migration.utils.Util.file;
1919

2020
import java.io.File;
21+
import java.nio.file.Paths;
2122

2223
public class SelectedPaths {
23-
private File basePath = new File("./");
24+
private File basePath = Paths.get("./").toFile();
2425
private File envPath;
2526
private File scriptPath;
2627
private File driverPath;

0 commit comments

Comments
 (0)