From a85db99d82c51c1f9a82db41ac74c0eaa88ea2f7 Mon Sep 17 00:00:00 2001 From: Szum123321 Date: Fri, 17 Jun 2022 21:33:13 +0200 Subject: [PATCH] pt II, slowly moving to the new file handling api (File -> Path) --- .../core/create/MakeBackupRunnable.java | 17 ++++++------ .../compressors/AbstractCompressor.java | 26 +++++++++---------- .../compressors/ParallelZipCompressor.java | 18 +++++++------ .../create/compressors/ZipCompressor.java | 21 ++++++++------- .../compressors/tar/AbstractTarArchiver.java | 11 ++++---- 5 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java b/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java index 436b713..70a17ac 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java @@ -30,9 +30,10 @@ import net.szum123321.textile_backup.core.create.compressors.tar.ParallelBZip2Co import net.szum123321.textile_backup.core.create.compressors.tar.ParallelGzipCompressor; import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream; -import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.time.LocalDateTime; public class MakeBackupRunnable implements Runnable { @@ -55,22 +56,22 @@ public class MakeBackupRunnable implements Runnable { log.sendInfoAL(context, "Starting backup"); - File world = Utilities.getWorldFolder(context.getServer()); + Path world = Utilities.getWorldFolder(context.getServer()).toPath(); log.trace("Minecraft world is: {}", world); - File outFile = Utilities + Path outFile = Utilities .getBackupRootPath(Utilities.getLevelName(context.getServer())) .toPath() - .resolve(getFileName()) - .toFile(); - + .resolve(getFileName()); log.trace("Outfile is: {}", outFile); - outFile.getParentFile().mkdirs(); + // outFile.getParentFile().mkdirs(); try { - outFile.createNewFile(); + //outFile.createNewFile(); + Files.createDirectories(outFile.getParent()); + Files.createFile(outFile); } catch (IOException e) { log.error("An exception occurred when trying to create new backup file!", e); diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java index 4f7aacf..125c32d 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java @@ -31,27 +31,27 @@ import java.nio.file.Path; import java.time.Duration; import java.time.Instant; import java.util.concurrent.ExecutionException; +import java.util.stream.Stream; public abstract class AbstractCompressor { private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); - public void createArchive(File inputFile, File outputFile, BackupContext ctx, int coreLimit) { + public void createArchive(Path inputFile, Path outputFile, BackupContext ctx, int coreLimit) { Instant start = Instant.now(); - try (FileOutputStream outStream = new FileOutputStream(outputFile); + try (OutputStream outStream = Files.newOutputStream(outputFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream); - OutputStream arc = createArchiveOutputStream(bufferedOutputStream, ctx, coreLimit)) { + OutputStream arc = createArchiveOutputStream(bufferedOutputStream, ctx, coreLimit); + Stream fileStream = Files.walk(inputFile)) { - Files.walk(inputFile.toPath()) - .filter(path -> !Utilities.isBlacklisted(inputFile.toPath().relativize(path))) - .map(Path::toFile) - .filter(File::isFile) - .forEach(file -> { + fileStream + .filter(path -> !Utilities.isBlacklisted(inputFile.relativize(path))) + .filter(Files::isRegularFile).forEach(file -> { try { //hopefully one broken file won't spoil the whole archive - addEntry(file, inputFile.toPath().relativize(file.toPath()).toString(), arc); + addEntry(file, inputFile.relativize(file).toString(), arc); } catch (IOException e) { - log.error("An exception occurred while trying to compress: {}", inputFile.toPath().relativize(file.toPath()).toString(), e); + log.error("An exception occurred while trying to compress: {}", inputFile.relativize(file).toString(), e); if (ctx.getInitiator() == ActionInitiator.Player) log.sendError(ctx, "Something went wrong while compressing files!"); @@ -86,13 +86,13 @@ public abstract class AbstractCompressor { } protected abstract OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException; - protected abstract void addEntry(File file, String entryName, OutputStream arc) throws IOException; + protected abstract void addEntry(Path file, String entryName, OutputStream arc) throws IOException; protected void finish(OutputStream arc) throws InterruptedException, ExecutionException, IOException { - ;//Basically this function is only needed for the ParallelZipCompressor to write out ParallelScatterZipCreator + //Basically this function is only needed for the ParallelZipCompressor to write out ParallelScatterZipCreator } protected void close() { - ;//Same as above, just for ParallelGzipCompressor to shutdown ExecutorService + //Same as above, just for ParallelGzipCompressor to shut down ExecutorService } } diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java index 63a81c7..7933067 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java @@ -26,6 +26,8 @@ import org.apache.commons.compress.archivers.zip.*; import org.apache.commons.compress.parallel.InputStreamSupplier; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Objects; import java.util.concurrent.*; import java.util.zip.ZipEntry; @@ -65,13 +67,13 @@ public class ParallelZipCompressor extends ZipCompressor { } @Override - protected void addEntry(File file, String entryName, OutputStream arc) throws IOException { + protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException { ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName); - if(ZipCompressor.isDotDat(file.getName())) { - entry.setMethod(ZipArchiveOutputStream.STORED); - entry.setSize(file.length()); - entry.setCompressedSize(file.length()); + if(ZipCompressor.isDotDat(file.getFileName().toString())) { + entry.setMethod(ZipEntry.STORED); + entry.setSize(Files.size(file)); + entry.setCompressedSize(Files.size(file)); entry.setCrc(getCRC(file)); } else entry.setMethod(ZipEntry.DEFLATED); @@ -126,12 +128,12 @@ public class ParallelZipCompressor extends ZipCompressor { } } - record FileInputStreamSupplier(File sourceFile) implements InputStreamSupplier { + record FileInputStreamSupplier(Path sourceFile) implements InputStreamSupplier { public InputStream get() { try { - return new FileInputStream(sourceFile); + return Files.newInputStream(sourceFile); } catch (IOException e) { - log.error("An exception occurred while trying to create an input stream from file: {}!", sourceFile.getName(), e); + log.error("An exception occurred while trying to create an input stream from file: {}!", sourceFile.toString(), e); } return null; diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java index ea98afd..0210708 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java @@ -27,9 +27,12 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.time.LocalDateTime; import java.util.zip.CRC32; import java.util.zip.Checksum; +import java.util.zip.ZipEntry; public class ZipCompressor extends AbstractCompressor { private final static ConfigHelper config = ConfigHelper.INSTANCE; @@ -51,14 +54,14 @@ public class ZipCompressor extends AbstractCompressor { } @Override - protected void addEntry(File file, String entryName, OutputStream arc) throws IOException { - try (FileInputStream fileInputStream = new FileInputStream(file)){ + protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException { + try (InputStream fileInputStream = Files.newInputStream(file)){ ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName); - if(isDotDat(file.getName())) { - entry.setMethod(ZipArchiveOutputStream.STORED); - entry.setSize(file.length()); - entry.setCompressedSize(file.length()); + if(isDotDat(file.getFileName().toString())) { + entry.setMethod(ZipEntry.STORED); + entry.setSize(Files.size(file)); + entry.setCompressedSize(Files.size(file)); entry.setCrc(getCRC(file)); } @@ -76,15 +79,15 @@ public class ZipCompressor extends AbstractCompressor { return arr[arr.length - 1].contains("dat"); //includes dat_old } - protected static long getCRC(File file) throws IOException { + protected static long getCRC(Path file) throws IOException { Checksum sum = new CRC32(); byte[] buffer = new byte[8192]; int len; - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file)) { while ((len = stream.read(buffer)) != -1) sum.update(buffer, 0, len); } catch (IOException e) { - throw new IOException("Error while calculating CRC of: " + file.getAbsolutePath(), e); + throw new IOException("Error while calculating CRC of: " + file.toAbsolutePath(), e); } return sum.getValue(); diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java index fe066bb..04489b4 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java @@ -24,10 +24,9 @@ import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; public class AbstractTarArchiver extends AbstractCompressor { protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException { @@ -44,8 +43,8 @@ public class AbstractTarArchiver extends AbstractCompressor { } @Override - protected void addEntry(File file, String entryName, OutputStream arc) throws IOException { - try (FileInputStream fileInputStream = new FileInputStream(file)){ + protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException { + try (InputStream fileInputStream = Files.newInputStream(file)){ TarArchiveEntry entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(file, entryName); ((TarArchiveOutputStream)arc).putArchiveEntry(entry);