Cleanup - File IO streams

This commit is contained in:
Jetz
2024-08-10 11:09:31 -04:00
parent 8e3d02f1aa
commit 09c76a4dc8
11 changed files with 23 additions and 31 deletions

View File

@@ -382,25 +382,14 @@ public class CardStorageReader {
* @return a new Card instance
*/
protected final CardRules loadCard(final CardRules.Reader reader, final File file) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
try (InputStream fileInputStream = java.nio.file.Files.newInputStream(file.toPath())) {
reader.reset();
final List<String> lines = readScript(fileInputStream);
return reader.readCard(lines, Files.getNameWithoutExtension(file.getName()));
} catch (final FileNotFoundException ex) {
throw new RuntimeException("CardReader : run error -- file not found: " + file.getPath(), ex);
} catch (final Exception ex) {
System.out.println("Error loading cardscript " + file.getName() + ". Please close Forge and resolve this.");
throw ex;
} finally {
try {
assert fileInputStream != null;
fileInputStream.close();
} catch (final IOException ignored) {
// 11:08
// PM
}
throw new RuntimeException("Error loading cardscript " + file.getName() + ". Please close Forge and resolve this.", ex);
}
}

View File

@@ -1,14 +1,14 @@
package forge.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Charsets;
public class CardTranslation {
private static Map <String, String> translatednames;
@@ -24,7 +24,7 @@ public class CardTranslation {
private static void readTranslationFile(String language, String languagesDirectory) {
String filename = "cardnames-" + language + ".txt";
try (LineReader translationFile = new LineReader(new FileInputStream(languagesDirectory + filename), Charsets.UTF_8)) {
try (LineReader translationFile = new LineReader(Files.newInputStream(Paths.get(languagesDirectory + filename)), StandardCharsets.UTF_8)) {
for (String line : translationFile.readLines()) {
String[] matches = line.split("\\|");
if (matches.length >= 2) {

View File

@@ -22,6 +22,7 @@ import org.apache.commons.lang3.tuple.Pair;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
@@ -252,7 +253,7 @@ public final class FileUtil {
final List<String> list = new ArrayList<>();
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(file), "UTF-8"));
new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
if (mayTrim) {

View File

@@ -1,8 +1,9 @@
package forge.assets;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import com.badlogic.gdx.Gdx;
@@ -370,7 +371,7 @@ public class FSkinFont {
String[] translationFilePaths = { ForgeConstants.LANG_DIR + "cardnames-" + langCode + ".txt",
ForgeConstants.LANG_DIR + langCode + ".properties" };
for (String translationFilePath : translationFilePaths) {
try (LineReader translationFile = new LineReader(new FileInputStream(translationFilePath),
try (LineReader translationFile = new LineReader(Files.newInputStream(Paths.get(translationFilePath)),
StandardCharsets.UTF_8)) {
for (String fileLine : translationFile.readLines()) {
final int stringLength = fileLine.length();

View File

@@ -98,7 +98,7 @@ public class GauntletIO {
public static GauntletData loadGauntlet(final File xmlSaveFile) {
boolean isCorrupt = false;
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
try (GZIPInputStream zin = new GZIPInputStream(Files.newInputStream(xmlSaveFile.toPath()));
InputStreamReader reader = new InputStreamReader(zin)) {
final GauntletData data = (GauntletData)GauntletIO.getSerializer(true).fromXML(reader);

View File

@@ -129,7 +129,7 @@ public class QuestDataIO {
QuestData data;
final StringBuilder xml = new StringBuilder();
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
try (GZIPInputStream zin = new GZIPInputStream(Files.newInputStream(xmlSaveFile.toPath()));
InputStreamReader reader = new InputStreamReader(zin)) {
final char[] buf = new char[1024];
while (reader.ready()) {

View File

@@ -79,7 +79,7 @@ public class TournamentIO {
public static TournamentData loadTournament(final File xmlSaveFile) {
boolean isCorrupt = false;
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
try (GZIPInputStream zin = new GZIPInputStream(Files.newInputStream(xmlSaveFile.toPath()));
InputStreamReader reader = new InputStreamReader(zin)) {
final TournamentData data = (TournamentData)TournamentIO.getSerializer(true).fromXML(reader);

View File

@@ -232,7 +232,7 @@ public class GuiDownloadZipService extends GuiDownloadService {
final byte[] buffer = new byte[1024];
int len;
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath))) {
try (BufferedOutputStream out = new BufferedOutputStream(java.nio.file.Files.newOutputStream(Paths.get(outPath)))) {
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}

View File

@@ -18,8 +18,8 @@
package forge.localinstance.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;
import java.util.Properties;
@@ -62,7 +62,7 @@ public class ForgeProfileProperties {
final File propFile = new File(ForgeConstants.PROFILE_FILE);
try {
if (propFile.canRead() && !isUsingAppDirectory) {
props.load(new FileInputStream(propFile));
props.load(Files.newInputStream(propFile.toPath()));
}
} catch (final IOException e) {
System.err.println("error while reading from profile properties file");

View File

@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
@@ -55,7 +56,7 @@ public class ZipUtil {
public static String unzip(File fileZip, File destDir) throws IOException {
StringBuilder val = new StringBuilder();
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
ZipInputStream zis = new ZipInputStream(Files.newInputStream(fileZip.toPath()));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = newFile(destDir, zipEntry);

View File

@@ -16,10 +16,10 @@
package forge.lda.lda.inference;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
public class InferenceProperties {
@@ -63,8 +63,8 @@ public class InferenceProperties {
}
class PropertiesLoader {
public InputStream getInputStream(String fileName) throws FileNotFoundException {
public InputStream getInputStream(String fileName) throws IOException {
if (fileName == null) throw new NullPointerException();
return new FileInputStream(fileName);
return Files.newInputStream(Paths.get(fileName));
}
}