From 78b51226aaf15c4995a1edb8399a998b3247982c Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 6 Dec 2018 16:06:27 +0100 Subject: [PATCH] Reextract content file until it's has the expected length. Maximum tries: 3 --- .../jnus/implementations/NUSDataProvider.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java index fce2f7d..f8fb946 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java @@ -109,26 +109,48 @@ public abstract class NUSDataProvider { * Content that should be saved * @param outputFolder * Target directory where the files will be stored in. + * @return * @throws IOException */ public void saveEncryptedContent(@NonNull Content content, @NonNull String outputFolder) throws IOException { - Utils.createDir(outputFolder); - InputStream inputStream = getInputStreamFromContent(content, 0); - if (inputStream == null) { - log.info("Couldn't save encrypted content. Input stream was null"); - return; - } + int maxTries = 3; + int i = 0; + while (i < maxTries) { + File output = new File(outputFolder + File.separator + content.getFilename()); + if (output.exists()) { + if (output.length() == content.getEncryptedFileSizeAligned()) { + log.info(content.getFilename() + "Encrypted content alreadys exists, skipped"); + return; + } else { + log.warning(content.getFilename() + " Encrypted content alreadys exists, but the length is not as expected. Saving it again. " + + output.length() + " " + content.getEncryptedFileSizeAligned() + " Difference: " + + (output.length() - content.getEncryptedFileSizeAligned())); + } + } - File output = new File(outputFolder + File.separator + content.getFilename()); - if (output.exists()) { - if (output.length() == content.getEncryptedFileSizeAligned()) { - log.info("Encrypted content alreadys exists, skipped"); + Utils.createDir(outputFolder); + InputStream inputStream = getInputStreamFromContent(content, 0); + if (inputStream == null) { + log.info(content.getFilename() + " Couldn't save encrypted content. Input stream was null"); return; - } else { - log.info("Encrypted content alreadys exists, but the length is not as expected. Saving it again"); + } + log.warning("loading " + content.getFilename()); + FileUtils.saveInputStreamToFile(output, inputStream, content.getEncryptedFileSizeAligned()); + + File outputNow = new File(outputFolder + File.separator + content.getFilename()); + if (outputNow.exists()) { + if (outputNow.length() != content.getEncryptedFileSizeAligned()) { + log.warning(content.getFilename() + " Encrypted content length is not as expected. Saving it again. Loaded: " + outputNow.length() + + " Expected: " + content.getEncryptedFileSizeAligned() + " Difference: " + + (outputNow.length() - content.getEncryptedFileSizeAligned())); + i++; + continue; + } else { + break; + } } } - FileUtils.saveInputStreamToFile(output, inputStream, content.getEncryptedFileSizeAligned()); + } /**