From 5879ce9036aa5830b6032b33becf72250234b7f2 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 26 Aug 2022 12:17:09 +0530 Subject: [PATCH] Convert Chunk.java into Kotlin --- .../kiwixmobile/core/downloader/Chunk.java | 73 ------------------- .../kiwixmobile/core/downloader/Chunk.kt | 33 +++++++++ .../kiwixmobile/core/downloader/ChunkUtils.kt | 3 +- .../core/downloader/ChunkUtilsTest.kt | 2 +- 4 files changed, 36 insertions(+), 75 deletions(-) delete mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java deleted file mode 100644 index 84f6383fb..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -package org.kiwix.kiwixmobile.core.downloader; - -public class Chunk { - - public boolean isDownloaded = false; - private String rangeHeader; - private String fileName; - private String url; - private long contentLength; - private int notificationID; - private long startByte; - private long endByte; - - public Chunk(String rangeHeader, String fileName, String url, long contentLength, - int notificationID, long startByte, long endByte) { - this.rangeHeader = rangeHeader; - this.fileName = fileName; - this.url = url; - this.contentLength = contentLength; - this.startByte = startByte; - this.endByte = endByte; - this.notificationID = notificationID; - } - - public String getRangeHeader() { - return rangeHeader; - } - - public String getFileName() { - return fileName; - } - - public long getContentLength() { - return contentLength; - } - - public String getUrl() { - return url; - } - - public int getNotificationID() { - return notificationID; - } - - public long getStartByte() { - return startByte; - } - - public long getEndByte() { - return endByte; - } - - public long getSize() { - return 1 + endByte - startByte; - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.kt new file mode 100644 index 000000000..38c8876cf --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.kt @@ -0,0 +1,33 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.kiwix.kiwixmobile.core.downloader + +@Suppress("LongParameterList") +class Chunk( + val rangeHeader: String, + val fileName: String?, + val url: String?, + val contentLength: Long, + val notificationID: Int, + private val startByte: Long, + private val endByte: Long +) { + var isDownloaded = false + val size: Long + get() = 1 + endByte - startByte +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtils.kt index cddd2657f..2a43303e1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtils.kt @@ -46,7 +46,8 @@ object ChunkUtils { val ranges = fileNames.indices.map { it * (CHUNK_SIZE + 1) }.map { it..it + CHUNK_SIZE } return fileNames.zip(ranges).map { (filename, currentRange) -> Chunk( - "${currentRange.first}-${if (contentLength in currentRange) + "${currentRange.first}-${ + if (contentLength in currentRange) "" else "${currentRange.last}" diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtilsTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtilsTest.kt index 3015cc7d0..b24acfd8b 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtilsTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/downloader/ChunkUtilsTest.kt @@ -107,7 +107,7 @@ class ChunkUtilsTest { for (i in listReturned.indices) { val extension = listReturned[i] .fileName - .substringAfter('.') + ?.substringAfter('.') val expectedExtension = "zim" + alphabet[i / 26] + alphabet[i % 26] + ".part.part" assertThat(extension).isEqualTo(expectedExtension) }