Skip to content

Commit

Permalink
2019.2 Performance Update and Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Jun 27, 2019
1 parent 7ec57fd commit 3c611f8
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 344 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
server
ssb.jar
.project
.classpath
.settings
bin
bin
SpigotServerBuilder-2.5.jar
188 changes: 188 additions & 0 deletions src/io/github/puyodead1/SSB/Download.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package io.github.puyodead1.SSB;

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.eclipse.swt.widgets.Display;

import io.github.puyodead1.SSB.utils.Utils;

public class Download implements Runnable {

private static final int MAX_BUFFER_SIZE = 1024;

public static final String STATUSES[] = { "Downloading", "Paused", "Complete", "Cancelled", "Error" };

public static final int DOWNLOADING = 0;
public static final int PAUSED = 1;
public static final int COMPLETE = 2;
public static final int CANCELLED = 3;
public static final int ERROR = 4;

private URL url;
private int size;
private int downloaded;
private int status;
private String output;

public Download(URL url, String output) {
this.url = url;
this.output = output;
size = -1;
downloaded = 0;
status = DOWNLOADING;

download();
}

public String getURL() {
return url.toString();
}

public int getSize() {
return size;
}

public float getProgress() {
return ((float) downloaded / size) * 100;
}

public int getStatus() {
return status;
}

private void error() {
status = ERROR;
stateChanged();
}

private void download() {
Thread thread = new Thread(this);
thread.start();
}

public void run() {
RandomAccessFile file = null;
InputStream stream = null;

try {
// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 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.getResponseCode() / 100 != 2) {
error();
System.out.println("Invalid response code");
}

// Check for valid content length.
int contentLength = connection.getContentLength();
if (contentLength < 1) {
error();
System.out.println("Invalid content length");
}

/*
* Set the size for this download if it hasn't been already set.
*/
if (size == -1) {
size = contentLength;
stateChanged();
}

// Open file and seek to the end of it.
file = new RandomAccessFile(output, "rw");
file.seek(downloaded);

stream = connection.getInputStream();
while (status == DOWNLOADING) {
/*
* Size buffer according to how much of the file is left to download.
*/
byte buffer[];
if (size - downloaded > MAX_BUFFER_SIZE) {
buffer = new byte[MAX_BUFFER_SIZE];
} else {
buffer = new byte[size - downloaded];
}

// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;

// Write buffer to file.
file.write(buffer, 0, read);
downloaded += read;
stateChanged();
}

/*
* Change status to complete if this point was reached because downloading has
* finished.
*/
if (status == DOWNLOADING) {
status = COMPLETE;
stateChanged();
}
} catch (Exception e) {
error();
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
SSB.Log(writer.toString());
} finally {
// Close file.
if (file != null) {
try {
file.close();
} catch (Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
SSB.Log(writer.toString());
}
}

// Close connection to server.
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
SSB.Log(writer.toString());
}
}
}
}

private void stateChanged() {
if (status == ERROR) {
System.out.println("Error downloading.");
} else if (status == DOWNLOADING) {
// update progess bar
int progress = Math.round(this.getProgress());

Display.getDefault().asyncExec(new Runnable() {
public void run() {
MainGUI.downloadProgress.setText(progress + "%");
}
});
} else if(status == COMPLETE) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
Utils.runBT();
}
});
}
}
}
36 changes: 0 additions & 36 deletions src/io/github/puyodead1/SSB/DownloadURL.java

This file was deleted.

Loading

0 comments on commit 3c611f8

Please sign in to comment.