Skip to content

Commit

Permalink
Migrate more java.io.File to java.nio.Path (#3978)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMorpheus committed Mar 30, 2024
1 parent 7b978e9 commit 82f793f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@
import org.gradle.api.tasks.TaskAction;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
Expand Down Expand Up @@ -211,10 +208,7 @@ public void generateDependenciesJson() {
final DependencyManifest manifest = new DependencyManifest(dependenciesMap);

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

// Get file input stream for reading the file content
final String md5hash;
try (final InputStream in = new FileInputStream(dependency.getFile())) {
try (final InputStream in = Files.newInputStream(dependency.getFile().toPath())) {
final MessageDigest hasher = MessageDigest.getInstance("MD5");
final byte[] buf = new byte[4096];
int read;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@
import org.spongepowered.common.event.manager.ListenerClassVisitor;
import org.spongepowered.common.util.generator.GeneratorUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -282,13 +282,15 @@ public byte[] generateClass(final Class<?> handle, final String localName, final
final byte[] data = cw.toByteArray();

if (FilterGenerator.FILTER_DEBUG) {
final File outDir = new File(".sponge.debug.out");
final File outFile = new File(outDir, name + ".class");
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
final Path outDir = Path.of(".sponge.debug.out");
final Path outFile = outDir.resolve(name + ".class");
try {
Files.createDirectories(outFile.getParent());
} catch (final IOException e) {
FilterGenerator.LOGGER.error("Failed to create parent directory", e);
}
try (final FileOutputStream out = new FileOutputStream(outFile)) {
out.write(data);
try {
Files.write(outFile, data);
} catch (final IOException e) {
FilterGenerator.LOGGER.error("Failed to write class to debug directory", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.spongepowered.common.applaunch.plugin.PluginPlatform;
import org.spongepowered.common.applaunch.plugin.PluginPlatformConstants;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
Expand All @@ -50,9 +49,9 @@ public class TestPluginPlatform implements PluginPlatform {
public TestPluginPlatform() {
final ClassLoader classLoader = this.getClass().getClassLoader();
final String directory = classLoader.getResource(".").getFile();
final File file = new File(directory);
this.outputDirectory = file.toPath();
this.pluginDirectory = new File(file, "plugins").toPath();
final Path p = Path.of(directory);
this.outputDirectory = p;
this.pluginDirectory = p.resolve("plugins");
}

@Override
Expand Down
39 changes: 21 additions & 18 deletions testplugins/src/main/java/org/spongepowered/test/map/MapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
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.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -392,19 +395,17 @@ private CommandResult loadMapFromFile(final CommandContext ctx) throws CommandEx
if (map.type() != ItemTypes.FILLED_MAP.get()) {
throw new CommandException(Component.text("You must hold a map in your hand"));
}
final File file = new File("map.png");
if (!file.isFile()) {
final Path p = Path.of("map.png");
if (!Files.isRegularFile(p)) {
try {
if (!file.createNewFile()) {
throw new IOException("failed to create new file :(");
}
Files.createFile(p);
} catch (final IOException e) {
e.printStackTrace();
}
}
final BufferedImage image;
try {
image = ImageIO.read(file);
try (final InputStream is = Files.newInputStream(p)){
image = ImageIO.read(is);
} catch (final IOException e) {
throw new CommandException(Component.text(e.getMessage()), e);
}
Expand All @@ -420,10 +421,10 @@ private CommandResult loadMapFromFile(final CommandContext ctx) throws CommandEx
}

private CommandResult savePallete(final CommandContext ctx) {
final File file = new File("pallete.png");
final Path p = Path.of("pallete.png");
try {
if (!file.isFile()) {
file.createNewFile();
if (!Files.isRegularFile(p)) {
Files.createFile(p);
}
final MapCanvas.Builder builder = MapCanvas.builder();

Expand Down Expand Up @@ -451,7 +452,9 @@ private CommandResult savePallete(final CommandContext ctx) {
}
final MapCanvas canvas = builder.build();
final Color color = new Color(0,0,0,0);
ImageIO.write((BufferedImage) canvas.toImage(color),"png", file);
try (final OutputStream os = Files.newOutputStream(p)) {
ImageIO.write((BufferedImage) canvas.toImage(color), "png", os);
}
} catch (final IOException e) {
Sponge.server().sendMessage(Component.text("IOException"));
}
Expand All @@ -465,18 +468,18 @@ private CommandResult saveToFile(final CommandContext ctx) throws CommandExcepti
throw new CommandException(Component.text("You must hold a map in your hand"));
}
final Image image = map.require(Keys.MAP_INFO).require(Keys.MAP_CANVAS).toImage();
final File file = new File("map.png");
if (!file.isFile()) {
final Path p = Path.of("map.png");
if (!Files.isRegularFile(p)) {
try {
if (!file.createNewFile()) {
throw new IOException("failed to create new file :(");
}
Files.createFile(p);
} catch (final IOException e) {
e.printStackTrace();
}
}
try {
ImageIO.write((BufferedImage)image, "png", file);
try (final OutputStream os = Files.newOutputStream(p)) {
ImageIO.write((BufferedImage) image, "png", os);
}
} catch (final IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
Expand Down Expand Up @@ -154,21 +156,26 @@ public void configureTransformationClassLoader(final ITransformingClassLoaderBui
}

protected boolean isTransformable(final URI uri) throws URISyntaxException, IOException {
final File file = new File(uri);
final Path p = Path.of(uri);

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

if (file.isDirectory()) {
if (!Files.exists(p)) {
return false;
}

final BasicFileAttributes basicFileAttributes = Files.readAttributes(p, BasicFileAttributes.class);
if (basicFileAttributes.isDirectory()) {
for (final String test : AbstractVanillaLaunchHandler.NON_TRANSFORMABLE_PATHS) {
if (new File(file, test).exists()) {
if (Files.exists(p.resolve(test))) {
return false;
}
}
} else if (file.isFile()) {
} else if (basicFileAttributes.isRegularFile()) {
try (final JarFile jf = new JarFile(new File(uri))) {
for (final String test : AbstractVanillaLaunchHandler.NON_TRANSFORMABLE_PATHS) {
if (jf.getEntry(test) != null) {
Expand Down

0 comments on commit 82f793f

Please sign in to comment.