diff --git a/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java b/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java index db567aa781d3383608690fb02950b740b1786ff9..0f40a6c3974a1c1c7411b2c74d7f65c0f2e9a47d 100644 --- a/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java +++ b/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java @@ -68,6 +68,11 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic private static final String TABLEXIA_TRUST_KEYSTORE_PASSWORD = "tablexia"; public static final int STATUS_CODE_SUCCESS = 200; + public static final int STATUS_CODE_SUCCESS_APPEND = 206; + + public static final String REQUEST_HEADER_KEY = "Range"; + public static final String REQUEST_HEADER_FORMAT = "bytes=%d-"; + private static final String SNAPSHOT_PACKAGE_NAME = "SNAPSHOT"; public static final int DOWNLOAD_TRY_COUNT = 3; @@ -271,14 +276,14 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic // check current package file and download it if it is necessary prepareKeyStore(); - if (!checkAndDownload(assetsPackageName)) { + if (!checkAndDownload(assetsPackageName, true)) { Log.err(getClass(), String.format("Cannot download assets package: %s", zipAssetsPackageName)); Log.info(ZipAssetLoader.class, "Devel mode --> try to use SNAPSHOT assets package"); String snapshotAssetsPackageName = language + "_" + SNAPSHOT_PACKAGE_NAME; String snapshotZipAssetsPackageName = snapshotAssetsPackageName + ZIP_FILE_EXTENSION; - if (checkAndDownload(snapshotAssetsPackageName)) { + if (checkAndDownload(snapshotAssetsPackageName, false)) { assetsPackageFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(ZIP_FILES_STORAGE_TYPE, snapshotZipAssetsPackageName); } else { error(getClass(), String.format("Cannot download assets package: %s", snapshotZipAssetsPackageName)); @@ -346,7 +351,7 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic System.setProperty("javax.net.ssl.trustStorePassword", TABLEXIA_TRUST_KEYSTORE_PASSWORD); } - private boolean checkAndDownload(final String fileName) { + private boolean checkAndDownload(final String fileName, boolean appendDownload) { int tryCounter = 0; String zipFileName = fileName + ZIP_FILE_EXTENSION; String checksumFileName = fileName + CHECKSUM_FILE_EXTENSION; @@ -355,15 +360,17 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic FileHandle zipFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL, zipFileName); FileHandle checksumFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL, checksumFileName); + long fileSize = 0; + while (true) { // check if checksum and package file exists - if (checksumFileHandle.exists() && zipFileHandle.exists()) { + if (checksumFileHandle.exists() & zipFileHandle.exists()) { if (checkAssetsPackage(checksumFileHandle, zipFileHandle)) { Log.info(ZipAssetLoader.class, String.format("Valid assets package file: %s", zipFileName)); return true; } else { Log.info(ZipAssetLoader.class, String.format("Malformed assets package file: %s", zipFileName)); - removeUnnecessaryAssetFiles(); + if(appendDownload) fileSize = zipFileHandle.file().length(); } } else { Log.info(ZipAssetLoader.class, String.format("Checksum or assets package file not found!", zipFileName)); @@ -396,13 +403,13 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic Log.info(ZipAssetLoader.class, String.format("Downloading checksum file: %s try %d/%d", checksumFileName, tryCounter + 1, DOWNLOAD_TRY_COUNT)); startTimer(); - checksumDownloadSuccess = downloadToOutputStream(fileName + CHECKSUM_FILE_EXTENSION, checksumFileHandle.write(false)); + checksumDownloadSuccess = downloadToOutputStream(fileName + CHECKSUM_FILE_EXTENSION, checksumFileHandle.write(false), 0); resultTimer("Download delay of " + checksumFileName); if(checksumDownloadSuccess) { Log.info(ZipAssetLoader.class, String.format("Downloading assets package file: %s try %d/%d", zipFileName, tryCounter + 1, DOWNLOAD_TRY_COUNT)); startTimer(); - assetsDownloadSuccess = downloadToOutputStream(fileName + ZIP_FILE_EXTENSION, zipFileHandle.write(false)); + assetsDownloadSuccess = downloadToOutputStream(fileName + ZIP_FILE_EXTENSION, zipFileHandle.write(true), fileSize); resultTimer("Download delay of " + zipFileName); } @@ -415,31 +422,32 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic } } - private boolean downloadToOutputStream(final String fileName, final OutputStream os) { + private boolean downloadToOutputStream(final String fileName, final OutputStream os, final long bytesDownloaded) { downloadHasResult = false; Net.HttpRequest request = new Net.HttpRequest(Net.HttpMethods.GET); request.setTimeOut(TABLEXIA_ASSETS_DOWNLOAD_TIMEOUT); request.setUrl(TABLEXIA_ASSETS_DOWNLOAD_BASE_PATH + fileName); + request.setHeader(REQUEST_HEADER_KEY, String.format(REQUEST_HEADER_FORMAT, bytesDownloaded)); Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { @Override public void handleHttpResponse(Net.HttpResponse httpResponse) { - if(httpResponse.getStatus().getStatusCode() != STATUS_CODE_SUCCESS) { + if(httpResponse.getStatus().getStatusCode() != STATUS_CODE_SUCCESS_APPEND) { Log.info(ZipAssetLoader.class, String.format("Download failed. Returned status code: %d", httpResponse.getStatus().getStatusCode())); downloadResult = false; notifyDownload(); return; } - long length = Long.parseLong(httpResponse.getHeader("Content-Length")); + long length = Long.parseLong(httpResponse.getHeader("Content-Length")) + bytesDownloaded; InputStream is = httpResponse.getResultAsStream(); byte[] bytes = new byte[1024]; int count = -1; - long read = 0; + long read = bytesDownloaded; ApplicationBus.getInstance().post(new FileDownloadEvent(FileDownloadEvent.FileDownloadEventType.Started, fileName, 0)).asynchronously();