Returns Optionals instead of null or throw proper Exceptions on WUDService class.

This commit is contained in:
Maschell 2019-04-10 18:06:02 +02:00
parent 9ecd9825b5
commit 88d9f90735

View File

@ -32,6 +32,7 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import java.util.zip.Checksum; import java.util.zip.Checksum;
@ -52,23 +53,23 @@ public final class WUDService {
// Just an utility class // Just an utility class
} }
public static File compressWUDToWUX(WUDImage image, String outputFolder) throws IOException { public static Optional<File> compressWUDToWUX(WUDImage image, String outputFolder) throws IOException {
return compressWUDToWUX(image, outputFolder, "game.wux", false); return compressWUDToWUX(image, outputFolder, "game.wux", false);
} }
public static File compressWUDToWUX(WUDImage image, String outputFolder, boolean overwrite) throws IOException { public static Optional<File> compressWUDToWUX(WUDImage image, String outputFolder, boolean overwrite) throws IOException {
return compressWUDToWUX(image, outputFolder, "game.wux", overwrite); return compressWUDToWUX(image, outputFolder, "game.wux", overwrite);
} }
public static File compressWUDToWUX(WUDImage image, String outputFolder, String filename, boolean overwrite) throws IOException { public static Optional<File> compressWUDToWUX(WUDImage image, String outputFolder, String filename, boolean overwrite) throws IOException {
if (image.isCompressed()) { if (image.isCompressed()) {
log.info("Given image is already compressed"); log.info("Given image is already compressed");
return null; return Optional.empty();
} }
if (image.getWUDFileSize() != WUDImage.WUD_FILESIZE) { if (image.getWUDFileSize() != WUDImage.WUD_FILESIZE) {
log.info("Given WUD has not the expected filesize"); log.info("Given WUD has not the expected filesize");
return null; return Optional.empty();
} }
String usedOutputFolder = outputFolder; String usedOutputFolder = outputFolder;
@ -86,7 +87,7 @@ public final class WUDService {
if (outputFile.exists() && !overwrite) { if (outputFile.exists() && !overwrite) {
log.info("Couldn't compress wud, target file already exists (" + outputFile.getAbsolutePath() + ")"); log.info("Couldn't compress wud, target file already exists (" + outputFile.getAbsolutePath() + ")");
return null; return Optional.empty();
} }
log.info("Writing compressed file to: " + outputFile.getAbsolutePath()); log.info("Writing compressed file to: " + outputFile.getAbsolutePath());
@ -159,7 +160,7 @@ public final class WUDService {
fileOutput.write(buffer.array()); fileOutput.write(buffer.array());
fileOutput.close(); fileOutput.close();
return outputFile; return Optional.of(outputFile);
} }
public static boolean compareWUDImage(WUDImage firstImage, WUDImage secondImage) throws IOException { public static boolean compareWUDImage(WUDImage firstImage, WUDImage secondImage) throws IOException {
@ -210,23 +211,23 @@ public final class WUDService {
return result; return result;
} }
public static File decompressWUX(WUDImage image, String outputFolder) throws IOException { public static Optional<File> decompressWUX(WUDImage image, String outputFolder) throws IOException {
return decompressWUX(image, outputFolder, "game.wud", false); return decompressWUX(image, outputFolder, "game.wud", false);
} }
public static File decompressWUX(WUDImage image, String outputFolder, boolean overwrite) throws IOException { public static Optional<File> decompressWUX(WUDImage image, String outputFolder, boolean overwrite) throws IOException {
return decompressWUX(image, outputFolder, "game.wud", overwrite); return decompressWUX(image, outputFolder, "game.wud", overwrite);
} }
public static File decompressWUX(WUDImage image, String outputFolder, String filename, boolean overwrite) throws IOException { public static Optional<File> decompressWUX(WUDImage image, String outputFolder, String filename, boolean overwrite) throws IOException {
if (!image.isCompressed()) { if (!image.isCompressed()) {
log.info("Given image is already decompressed (a wud file)"); log.info("Given image is already decompressed (a wud file)");
return null; return Optional.empty();
} }
if (image.getWUDFileSize() != WUDImage.WUD_FILESIZE) { if (image.getWUDFileSize() != WUDImage.WUD_FILESIZE) {
log.info("Given WUX has not the expected filesize"); log.info("Given WUX has not the expected filesize");
return null; return Optional.empty();
} }
String usedOutputFolder = outputFolder; String usedOutputFolder = outputFolder;
@ -244,7 +245,7 @@ public final class WUDService {
if (outputFile.exists() && !overwrite) { if (outputFile.exists() && !overwrite) {
log.info("Couldn't decompress wux, target file already exists (" + outputFile.getAbsolutePath() + ")"); log.info("Couldn't decompress wux, target file already exists (" + outputFile.getAbsolutePath() + ")");
return null; return Optional.empty();
} }
log.info("Writing decompressed file to: " + outputFile.getAbsolutePath()); log.info("Writing decompressed file to: " + outputFile.getAbsolutePath());
@ -279,13 +280,13 @@ public final class WUDService {
in.close(); in.close();
out.close(); out.close();
return outputFile; return Optional.of(outputFile);
} }
public static HashResult hashWUDImage(WUDImage image) throws IOException { public static HashResult hashWUDImage(WUDImage image) throws IOException {
if (image == null) { if (image == null) {
log.info("Failed to calculate the hash of the given image: input was null."); log.info("Failed to calculate the hash of the given image: input was null.");
return null; throw new IOException("Failed to calculate the hash of the given image: input was null.");
} }
if (image.isCompressed()) { if (image.isCompressed()) {