Skip to content

Commit

Permalink
Merge pull request #1 from taggernation/Experimental
Browse files Browse the repository at this point in the history
Experimental DownloadManager
  • Loading branch information
toonystank authored Mar 17, 2022
2 parents b288a92 + 305e47c commit e2e4b2e
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.taggernation.taggernationlib.updatechecker.downloads;

import com.taggernation.taggernationlib.updatechecker.UpdateChecker;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class DownloadManager {

private final URL url;
private boolean overwrite;

public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}

public DownloadManager(UpdateChecker instance, URL url) {
this.url = url;
}
public void download() throws IOException {
File file = new File("rc24-java-logo.gif");
Bukkit.getLogger().info("Downloading file...");
if (checkFileExist(file) && !overwrite) {
Bukkit.getLogger().info("File already exists, skipping download.");
return;
}

ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(file);
FileChannel fileChannel = fileOutputStream.getChannel();
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
fileChannel.close();
fileOutputStream.close();
readableByteChannel.close();
Bukkit.getLogger().info("Downloaded file: " + file.getPath() + "/" + file.getName());

}
public boolean checkFileExist(File file) {
return file.exists();
}
public boolean Delete (File file) {
if (file.exists()) {
return file.delete();
}
return false;
}

}

0 comments on commit e2e4b2e

Please sign in to comment.