Reextract content file until it's has the expected length. Maximum tries: 3

This commit is contained in:
Maschell 2018-12-06 16:06:27 +01:00
parent 7425e6e904
commit 78b51226aa

View File

@ -109,26 +109,48 @@ public abstract class NUSDataProvider {
* Content that should be saved * Content that should be saved
* @param outputFolder * @param outputFolder
* Target directory where the files will be stored in. * Target directory where the files will be stored in.
* @return
* @throws IOException * @throws IOException
*/ */
public void saveEncryptedContent(@NonNull Content content, @NonNull String outputFolder) throws IOException { public void saveEncryptedContent(@NonNull Content content, @NonNull String outputFolder) throws IOException {
Utils.createDir(outputFolder); int maxTries = 3;
InputStream inputStream = getInputStreamFromContent(content, 0); int i = 0;
if (inputStream == null) { while (i < maxTries) {
log.info("Couldn't save encrypted content. Input stream was null"); File output = new File(outputFolder + File.separator + content.getFilename());
return; 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()); Utils.createDir(outputFolder);
if (output.exists()) { InputStream inputStream = getInputStreamFromContent(content, 0);
if (output.length() == content.getEncryptedFileSizeAligned()) { if (inputStream == null) {
log.info("Encrypted content alreadys exists, skipped"); log.info(content.getFilename() + " Couldn't save encrypted content. Input stream was null");
return; 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());
} }
/** /**