Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Nov 13, 2019
1 parent 6252cae commit 50336f1
Show file tree
Hide file tree
Showing 5 changed files with 488 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.classpath
.project
bin/
.settings/
81 changes: 81 additions & 0 deletions src/me/puyodead1/filedownloader/DownloadDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package me.puyodead1.filedownloader;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ProgressBar;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;

public class DownloadDialog extends Dialog {

protected Object result;
protected Shell shell;
private String fileName;
private String savePath;
private URL url;
private Downloader downloader;

/**
* Create the dialog.
* @param parent
* @param style
*/
public DownloadDialog(Shell parent, int style, String url, String fileName, String savePath) {
super(parent, style);
this.fileName = fileName;
this.savePath = savePath;
setText("Downloading...");
try {
this.url = new URL(url);
this.downloader = new Downloader(parent, this.url, savePath);
} catch(MalformedURLException e) {
final MessageDialog dialog = new MessageDialog(shell, getStyle(), "Error", e.getMessage());
dialog.open();
}
}

/**
* Open the dialog.
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}

/**
* Create contents of the dialog.
*/
private void createContents() {
shell = new Shell(getParent(), getStyle());
shell.setSize(450, 148);
shell.setText(getText());

ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setBounds(10, 52, 424, 25);

Button btnCancel = new Button(shell, SWT.NONE);
btnCancel.setBounds(10, 83, 424, 25);
btnCancel.setText("Cancel");

Label lblFile = new Label(shell, SWT.WRAP | SWT.CENTER);
lblFile.setAlignment(SWT.CENTER);
lblFile.setText("Downloading file: \n" + fileName);
lblFile.setBounds(10, 10, 424, 36);

}
}
217 changes: 217 additions & 0 deletions src/me/puyodead1/filedownloader/Downloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package me.puyodead1.filedownloader;

import java.io.FileNotFoundException;
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 org.eclipse.swt.widgets.Shell;

public class Downloader 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 Shell parentShell;
private URL url;
private int size;
private int downloaded;
private int status;
private String output;

public Downloader(Shell parent, URL url, String output) {
this.parentShell = parent;
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.
try {
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 (Throwable t) {
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Error",
t.getMessage());
dialog.open();
}
});
error();
}
} catch (Exception e) {
error();
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
System.out.println(writer.toString());
} finally {
// Close file.
if (file != null) {
try {
file.close();
} catch (Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
System.out.println(writer.toString());
}
}

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

private void stateChanged() {
if (status == ERROR) {
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Error",
"An error occured and the download could not complete!");
dialog.open();

}
});
} else if (status == DOWNLOADING) {
// update progess bar
int progress = Math.round(this.getProgress());

Display.getDefault().asyncExec(new Runnable() {
public void run() {
// DownloadDialog.progressBar.setText(progress + "%");
System.out.println(progress + "%");
}
});
} else if (status == COMPLETE) {
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Complete",
"File download complete!");
dialog.open();

}
});
}
}
}
Loading

0 comments on commit 50336f1

Please sign in to comment.