mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-17 07:47:57 -04:00
Remove IOUtils.closeQuietly
This commit is contained in:
parent
47031cb70e
commit
cadafe13e1
@ -19,7 +19,6 @@ package org.jackhuang.hmcl.game;
|
||||
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||
import org.jackhuang.hmcl.util.IOUtils;
|
||||
import org.jenkinsci.constant_pool_scanner.ConstantPool;
|
||||
import org.jenkinsci.constant_pool_scanner.ConstantPoolScanner;
|
||||
import org.jenkinsci.constant_pool_scanner.ConstantType;
|
||||
@ -72,9 +71,8 @@ public final class GameVersion {
|
||||
if (file == null || !file.exists() || !file.isFile() || !file.canRead())
|
||||
return Optional.empty();
|
||||
|
||||
ZipFile gameJar = null;
|
||||
try {
|
||||
gameJar = new ZipFile(file);
|
||||
try (ZipFile gameJar = new ZipFile(file)) {
|
||||
ZipArchiveEntry minecraft = gameJar.getEntry("net/minecraft/client/Minecraft.class");
|
||||
if (minecraft != null) {
|
||||
Optional<String> result = getVersionOfClassMinecraft(gameJar, minecraft);
|
||||
@ -85,10 +83,9 @@ public final class GameVersion {
|
||||
if (minecraftServer != null)
|
||||
return getVersionFromClassMinecraftServer(gameJar, minecraftServer);
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(gameJar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -115,9 +115,21 @@ public class FileDownloadTask extends Task {
|
||||
}
|
||||
|
||||
private void closeFiles() {
|
||||
IOUtils.closeQuietly(rFile);
|
||||
if (rFile != null)
|
||||
try {
|
||||
rFile.close();
|
||||
} catch (IOException e) {
|
||||
Logging.LOG.log(Level.WARNING, "Failed to close file: " + rFile, e);
|
||||
}
|
||||
|
||||
rFile = null;
|
||||
IOUtils.closeQuietly(stream);
|
||||
|
||||
if (stream != null)
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
Logging.LOG.log(Level.WARNING, "Failed to close stream", e);
|
||||
}
|
||||
stream = null;
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,8 @@ package org.jackhuang.hmcl.util;
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@ -70,9 +70,7 @@ public final class FileUtils {
|
||||
}
|
||||
|
||||
public static void writeBytes(File file, byte[] array) throws IOException {
|
||||
try (FileOutputStream stream = new FileOutputStream(file)) {
|
||||
stream.write(array);
|
||||
}
|
||||
Files.write(file.toPath(), array);
|
||||
}
|
||||
|
||||
public static void deleteDirectory(File directory)
|
||||
@ -94,10 +92,6 @@ public final class FileUtils {
|
||||
return Lang.test(() -> deleteDirectory(directory));
|
||||
}
|
||||
|
||||
public static boolean cleanDirectoryQuietly(File directory) {
|
||||
return Lang.test(() -> cleanDirectory(directory));
|
||||
}
|
||||
|
||||
public static void cleanDirectory(File directory)
|
||||
throws IOException {
|
||||
if (!directory.exists()) {
|
||||
@ -159,65 +153,8 @@ public final class FileUtils {
|
||||
return !fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());
|
||||
}
|
||||
|
||||
public static void copyDirectory(File srcDir, File destDir)
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, null);
|
||||
}
|
||||
public static void copyDirectory(Path src, Path dest) {
|
||||
|
||||
public static void copyDirectory(File srcDir, File destDir, FileFilter filter)
|
||||
throws IOException {
|
||||
Objects.requireNonNull(srcDir, "Source must not be null");
|
||||
Objects.requireNonNull(destDir, "Destination must not be null");
|
||||
if (!srcDir.exists())
|
||||
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
|
||||
if (!srcDir.isDirectory())
|
||||
throw new IOException("Source '" + srcDir + "' exists but is not a directory");
|
||||
if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath()))
|
||||
throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");
|
||||
|
||||
List<String> exclusionList = null;
|
||||
if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
|
||||
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
|
||||
if ((srcFiles != null) && (srcFiles.length > 0)) {
|
||||
exclusionList = new ArrayList<>(srcFiles.length);
|
||||
for (File srcFile : srcFiles) {
|
||||
File copiedFile = new File(destDir, srcFile.getName());
|
||||
exclusionList.add(copiedFile.getCanonicalPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
doCopyDirectory(srcDir, destDir, filter, exclusionList);
|
||||
}
|
||||
|
||||
private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, List<String> exclusionList)
|
||||
throws IOException {
|
||||
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
|
||||
if (srcFiles == null)
|
||||
throw new IOException("Failed to list contents of " + srcDir);
|
||||
if (destDir.exists()) {
|
||||
if (!destDir.isDirectory())
|
||||
throw new IOException("Destination '" + destDir + "' exists but is not a directory");
|
||||
} else if (!FileUtils.makeDirectory(destDir))
|
||||
throw new IOException("Destination '" + destDir + "' directory cannot be created");
|
||||
|
||||
if (!destDir.canWrite())
|
||||
throw new IOException("Destination '" + destDir + "' cannot be written to");
|
||||
for (File srcFile : srcFiles) {
|
||||
File dstFile = new File(destDir, srcFile.getName());
|
||||
if ((exclusionList == null) || (!exclusionList.contains(srcFile.getCanonicalPath())))
|
||||
if (srcFile.isDirectory())
|
||||
doCopyDirectory(srcFile, dstFile, filter, exclusionList);
|
||||
else
|
||||
doCopyFile(srcFile, dstFile);
|
||||
}
|
||||
destDir.setLastModified(srcDir.lastModified());
|
||||
}
|
||||
|
||||
public static void copyFileQuietly(File srcFile, File destFile) {
|
||||
try {
|
||||
copyFile(srcFile, destFile);
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyFile(File srcFile, File destFile)
|
||||
@ -235,7 +172,8 @@ public final class FileUtils {
|
||||
throw new IOException("Destination '" + parentFile + "' directory cannot be created");
|
||||
if (destFile.exists() && !destFile.canWrite())
|
||||
throw new IOException("Destination '" + destFile + "' exists but is read-only");
|
||||
doCopyFile(srcFile, destFile);
|
||||
|
||||
Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
public static void moveFile(File srcFile, File destFile) throws IOException {
|
||||
@ -243,11 +181,6 @@ public final class FileUtils {
|
||||
srcFile.delete();
|
||||
}
|
||||
|
||||
private static void doCopyFile(File srcFile, File destFile)
|
||||
throws IOException {
|
||||
Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
public static boolean makeDirectory(File directory) {
|
||||
return directory.isDirectory() || directory.mkdirs();
|
||||
}
|
||||
|
@ -31,22 +31,11 @@ public final class IOUtils {
|
||||
|
||||
public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
|
||||
|
||||
public static void closeQuietly(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null)
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static ByteArrayOutputStream readFully(InputStream stream) throws IOException {
|
||||
try {
|
||||
try (InputStream is = stream) {
|
||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
copyTo(stream, result);
|
||||
copyTo(is, result);
|
||||
return result;
|
||||
} finally {
|
||||
closeQuietly(stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,30 +84,22 @@ public final class NetworkUtils {
|
||||
con.setDoOutput(true);
|
||||
con.setRequestProperty("Content-Type", contentType + "; charset=utf-8");
|
||||
con.setRequestProperty("Content-Length", "" + bytes.length);
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = con.getOutputStream();
|
||||
if (os != null)
|
||||
try (OutputStream os = con.getOutputStream()) {
|
||||
os.write(bytes);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
return readData(con);
|
||||
}
|
||||
|
||||
public static String readData(HttpURLConnection con) throws IOException {
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = con.getInputStream();
|
||||
return IOUtils.readFullyAsString(is, UTF_8);
|
||||
try (InputStream stdout = con.getInputStream()) {
|
||||
return IOUtils.readFullyAsString(stdout, UTF_8);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
IOUtils.closeQuietly(is);
|
||||
is = con.getErrorStream();
|
||||
if (is != null)
|
||||
return IOUtils.readFullyAsString(is, UTF_8);
|
||||
throw e;
|
||||
} finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
try (InputStream stderr = con.getErrorStream()) {
|
||||
if (stderr == null) throw e;
|
||||
return IOUtils.readFullyAsString(stderr, UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user