Skip to content

Commit 82f793f

Browse files
authored
Migrate more java.io.File to java.nio.Path (#3978)
1 parent 7b978e9 commit 82f793f

File tree

5 files changed

+49
-44
lines changed

5 files changed

+49
-44
lines changed

build-logic/src/main/java/org/spongepowered/gradle/impl/OutputDependenciesToJson.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@
4545
import org.gradle.api.tasks.TaskAction;
4646

4747
import java.io.BufferedWriter;
48-
import java.io.FileInputStream;
49-
import java.io.FileOutputStream;
5048
import java.io.IOException;
5149
import java.io.InputStream;
52-
import java.io.OutputStreamWriter;
53-
import java.nio.charset.StandardCharsets;
50+
import java.nio.file.Files;
5451
import java.security.MessageDigest;
5552
import java.security.NoSuchAlgorithmException;
5653
import java.util.Comparator;
@@ -211,10 +208,7 @@ public void generateDependenciesJson() {
211208
final DependencyManifest manifest = new DependencyManifest(dependenciesMap);
212209

213210
this.getLogger().info("Writing to {}", this.getOutputFile().get().getAsFile());
214-
try (
215-
final BufferedWriter writer = new BufferedWriter(
216-
new OutputStreamWriter(new FileOutputStream(this.getOutputFile().get().getAsFile()), StandardCharsets.UTF_8))
217-
) {
211+
try (final BufferedWriter writer = Files.newBufferedWriter(this.getOutputFile().get().getAsFile().toPath())) {
218212
OutputDependenciesToJson.GSON.toJson(manifest, writer);
219213
} catch (final IOException ex) {
220214
throw new GradleException("Failed to write dependencies manifest", ex);
@@ -233,7 +227,7 @@ private List<DependencyDescriptor> configToDescriptor(final Set<ResolvedArtifact
233227

234228
// Get file input stream for reading the file content
235229
final String md5hash;
236-
try (final InputStream in = new FileInputStream(dependency.getFile())) {
230+
try (final InputStream in = Files.newInputStream(dependency.getFile().toPath())) {
237231
final MessageDigest hasher = MessageDigest.getInstance("MD5");
238232
final byte[] buf = new byte[4096];
239233
int read;

src/main/java/org/spongepowered/common/event/filter/FilterGenerator.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@
8686
import org.spongepowered.common.event.manager.ListenerClassVisitor;
8787
import org.spongepowered.common.util.generator.GeneratorUtils;
8888

89-
import java.io.File;
90-
import java.io.FileOutputStream;
9189
import java.io.IOException;
9290
import java.lang.annotation.Annotation;
9391
import java.lang.invoke.MethodHandles;
9492
import java.lang.invoke.MethodType;
93+
import java.nio.file.Files;
94+
import java.nio.file.Path;
9595
import java.util.ArrayList;
9696
import java.util.Collections;
9797
import java.util.HashMap;
@@ -282,13 +282,15 @@ public byte[] generateClass(final Class<?> handle, final String localName, final
282282
final byte[] data = cw.toByteArray();
283283

284284
if (FilterGenerator.FILTER_DEBUG) {
285-
final File outDir = new File(".sponge.debug.out");
286-
final File outFile = new File(outDir, name + ".class");
287-
if (!outFile.getParentFile().exists()) {
288-
outFile.getParentFile().mkdirs();
285+
final Path outDir = Path.of(".sponge.debug.out");
286+
final Path outFile = outDir.resolve(name + ".class");
287+
try {
288+
Files.createDirectories(outFile.getParent());
289+
} catch (final IOException e) {
290+
FilterGenerator.LOGGER.error("Failed to create parent directory", e);
289291
}
290-
try (final FileOutputStream out = new FileOutputStream(outFile)) {
291-
out.write(data);
292+
try {
293+
Files.write(outFile, data);
292294
} catch (final IOException e) {
293295
FilterGenerator.LOGGER.error("Failed to write class to debug directory", e);
294296
}

src/test/java/org/spongepowered/common/test/TestPluginPlatform.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.spongepowered.common.applaunch.plugin.PluginPlatform;
3030
import org.spongepowered.common.applaunch.plugin.PluginPlatformConstants;
3131

32-
import java.io.File;
3332
import java.nio.file.Path;
3433
import java.util.Collections;
3534
import java.util.List;
@@ -50,9 +49,9 @@ public class TestPluginPlatform implements PluginPlatform {
5049
public TestPluginPlatform() {
5150
final ClassLoader classLoader = this.getClass().getClassLoader();
5251
final String directory = classLoader.getResource(".").getFile();
53-
final File file = new File(directory);
54-
this.outputDirectory = file.toPath();
55-
this.pluginDirectory = new File(file, "plugins").toPath();
52+
final Path p = Path.of(directory);
53+
this.outputDirectory = p;
54+
this.pluginDirectory = p.resolve("plugins");
5655
}
5756

5857
@Override

testplugins/src/main/java/org/spongepowered/test/map/MapTest.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@
7878
import java.awt.Color;
7979
import java.awt.Image;
8080
import java.awt.image.BufferedImage;
81-
import java.io.File;
8281
import java.io.IOException;
82+
import java.io.InputStream;
83+
import java.io.OutputStream;
84+
import java.nio.file.Files;
85+
import java.nio.file.Path;
8386
import java.util.ArrayList;
8487
import java.util.Collection;
8588
import java.util.Collections;
@@ -392,19 +395,17 @@ private CommandResult loadMapFromFile(final CommandContext ctx) throws CommandEx
392395
if (map.type() != ItemTypes.FILLED_MAP.get()) {
393396
throw new CommandException(Component.text("You must hold a map in your hand"));
394397
}
395-
final File file = new File("map.png");
396-
if (!file.isFile()) {
398+
final Path p = Path.of("map.png");
399+
if (!Files.isRegularFile(p)) {
397400
try {
398-
if (!file.createNewFile()) {
399-
throw new IOException("failed to create new file :(");
400-
}
401+
Files.createFile(p);
401402
} catch (final IOException e) {
402403
e.printStackTrace();
403404
}
404405
}
405406
final BufferedImage image;
406-
try {
407-
image = ImageIO.read(file);
407+
try (final InputStream is = Files.newInputStream(p)){
408+
image = ImageIO.read(is);
408409
} catch (final IOException e) {
409410
throw new CommandException(Component.text(e.getMessage()), e);
410411
}
@@ -420,10 +421,10 @@ private CommandResult loadMapFromFile(final CommandContext ctx) throws CommandEx
420421
}
421422

422423
private CommandResult savePallete(final CommandContext ctx) {
423-
final File file = new File("pallete.png");
424+
final Path p = Path.of("pallete.png");
424425
try {
425-
if (!file.isFile()) {
426-
file.createNewFile();
426+
if (!Files.isRegularFile(p)) {
427+
Files.createFile(p);
427428
}
428429
final MapCanvas.Builder builder = MapCanvas.builder();
429430

@@ -451,7 +452,9 @@ private CommandResult savePallete(final CommandContext ctx) {
451452
}
452453
final MapCanvas canvas = builder.build();
453454
final Color color = new Color(0,0,0,0);
454-
ImageIO.write((BufferedImage) canvas.toImage(color),"png", file);
455+
try (final OutputStream os = Files.newOutputStream(p)) {
456+
ImageIO.write((BufferedImage) canvas.toImage(color), "png", os);
457+
}
455458
} catch (final IOException e) {
456459
Sponge.server().sendMessage(Component.text("IOException"));
457460
}
@@ -465,18 +468,18 @@ private CommandResult saveToFile(final CommandContext ctx) throws CommandExcepti
465468
throw new CommandException(Component.text("You must hold a map in your hand"));
466469
}
467470
final Image image = map.require(Keys.MAP_INFO).require(Keys.MAP_CANVAS).toImage();
468-
final File file = new File("map.png");
469-
if (!file.isFile()) {
471+
final Path p = Path.of("map.png");
472+
if (!Files.isRegularFile(p)) {
470473
try {
471-
if (!file.createNewFile()) {
472-
throw new IOException("failed to create new file :(");
473-
}
474+
Files.createFile(p);
474475
} catch (final IOException e) {
475476
e.printStackTrace();
476477
}
477478
}
478479
try {
479-
ImageIO.write((BufferedImage)image, "png", file);
480+
try (final OutputStream os = Files.newOutputStream(p)) {
481+
ImageIO.write((BufferedImage) image, "png", os);
482+
}
480483
} catch (final IOException e) {
481484
e.printStackTrace();
482485
}

vanilla/src/applaunch/java/org/spongepowered/vanilla/applaunch/handler/AbstractVanillaLaunchHandler.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
import java.net.URISyntaxException;
4747
import java.net.URL;
4848
import java.net.URLConnection;
49+
import java.nio.file.Files;
4950
import java.nio.file.Path;
5051
import java.nio.file.Paths;
52+
import java.nio.file.attribute.BasicFileAttributes;
5153
import java.util.Collections;
5254
import java.util.Enumeration;
5355
import java.util.Iterator;
@@ -154,21 +156,26 @@ public void configureTransformationClassLoader(final ITransformingClassLoaderBui
154156
}
155157

156158
protected boolean isTransformable(final URI uri) throws URISyntaxException, IOException {
157-
final File file = new File(uri);
159+
final Path p = Path.of(uri);
158160

159161
// in Java 8 ONLY, the system classpath contains JVM internals
160162
// let's make sure those don't get transformed
161-
if (file.getAbsolutePath().startsWith(AbstractVanillaLaunchHandler.JAVA_HOME_PATH)) {
163+
if (p.toAbsolutePath().startsWith(AbstractVanillaLaunchHandler.JAVA_HOME_PATH)) {
162164
return false;
163165
}
164166

165-
if (file.isDirectory()) {
167+
if (!Files.exists(p)) {
168+
return false;
169+
}
170+
171+
final BasicFileAttributes basicFileAttributes = Files.readAttributes(p, BasicFileAttributes.class);
172+
if (basicFileAttributes.isDirectory()) {
166173
for (final String test : AbstractVanillaLaunchHandler.NON_TRANSFORMABLE_PATHS) {
167-
if (new File(file, test).exists()) {
174+
if (Files.exists(p.resolve(test))) {
168175
return false;
169176
}
170177
}
171-
} else if (file.isFile()) {
178+
} else if (basicFileAttributes.isRegularFile()) {
172179
try (final JarFile jf = new JarFile(new File(uri))) {
173180
for (final String test : AbstractVanillaLaunchHandler.NON_TRANSFORMABLE_PATHS) {
174181
if (jf.getEntry(test) != null) {

0 commit comments

Comments
 (0)