aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt
diff options
context:
space:
mode:
authorBryson Steck <brysonsteck@protonmail.com>2023-05-14 13:37:55 -0600
committerBryson Steck <brysonsteck@protonmail.com>2023-05-14 13:37:55 -0600
commit8fbd006b494ce4c6ab284d9eeab8692195984151 (patch)
tree19ea906921c945dbb05656d3604d23d4c3d289da /app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt
parentefd5388d335f73351ab1bc40fcfdde30cb04c8e0 (diff)
downloadServerCraft-8fbd006b494ce4c6ab284d9eeab8692195984151.tar
ServerCraft-8fbd006b494ce4c6ab284d9eeab8692195984151.tar.gz
ServerCraft-8fbd006b494ce4c6ab284d9eeab8692195984151.tar.bz2
turns out it just doesnt work on linux, reverting and organizing
Diffstat (limited to 'app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt')
-rw-r--r--app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt117
1 files changed, 0 insertions, 117 deletions
diff --git a/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt b/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt
deleted file mode 100644
index b98b511..0000000
--- a/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/Download.kt
+++ /dev/null
@@ -1,117 +0,0 @@
-import java.io.*;
-import java.net.*;
-import java.util.*;
-
-class Download: Runnable {
- public enum class Status {
- DOWNLOADING, PAUSED, COMPLETE, CANCELLED, ERROR
- }
- public var size: Int
- public var downloaded: Int
- public var contentLength: Int
- public var status: Status
-
- private final val MAX_BUFFER_SIZE: Number = 1024
-
- private var url: URL
- private var dir: String
-
- constructor (url: URL, dir: String) {
- this.url = url
- this.dir = dir
- size = -1
- downloaded = 0
- status = Status.DOWNLOADING
- contentLength = 1
- }
-
- public fun start() {
- val thread = Thread(this)
- thread.start()
- }
-
- private fun getFilename(url: URL): String {
- val filename = url.getFile()
- return filename.substring(filename.lastIndexOf('/') + 1)
- }
-
- override fun run() {
- var stream: InputStream? = null
- var file: RandomAccessFile? = null
-
- try {
- // Open connection to URL.
- var connection = url.openConnection() as HttpURLConnection;
-
- // Specify what portion of file to download.
- connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
-
- // Connect to server.
- connection.connect();
-
- // Make sure response code is in the 200 range.
- if (connection.responseCode / 100 != 2) {
- status = Status.ERROR
- }
-
- // Check for valid content length.
- contentLength = connection.getContentLength();
- if (contentLength < 1) {
- status = Status.ERROR
- }
-
- /* Set the size for this download if it
- hasn't been already set. */
- if (size == -1) {
- size = contentLength;
- }
-
- // Open file and seek to the end of it.
- file = RandomAccessFile(dir + getFilename(url), "rw");
- file.seek(downloaded.toLong());
-
- stream = connection.getInputStream();
- while (status == Status.DOWNLOADING) {
- /* Size buffer according to how much of the
- file is left to download. */
- val buffer: ByteArray;
- if (size - downloaded > MAX_BUFFER_SIZE as Int) {
- buffer = ByteArray(MAX_BUFFER_SIZE)
- } else {
- buffer = ByteArray(size - downloaded);
- }
-
- // Read from server into buffer.
- val read = stream.read(buffer);
- if (read == -1)
- break;
-
- // Write buffer to file.
- file.write(buffer, 0, read);
- downloaded += read;
- }
-
- /* Change status to complete if this point was
- reached because downloading has finished. */
- if (status == Status.DOWNLOADING) {
- status = Status.COMPLETE;
- }
- } catch (e: Exception) {
- status = Status.ERROR
- } finally {
- // Close file.
- if (file != null) {
- try {
- file.close();
- } catch (e: Exception) {}
- }
-
- // Close connection to server.
- if (stream != null) {
- try {
- stream.close();
- } catch (e: Exception) {}
- }
- }
- }
-}