/
Downloader.java
106 lines (93 loc) · 3.12 KB
/
Downloader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.bukkit.fillr;
import org.bukkit.Server;
import org.bukkit.plugin.SimplePluginManager;
import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Downloader {
private static final Logger logger = Logger.getLogger(Downloader.class.getName());
private final File pluginFolder;
private final File downloadFolder;
private final File backupFolder;
public Downloader(Server server) {
SimplePluginManager pm = (SimplePluginManager) server.getPluginManager();
pluginFolder = pm.getPluginFolder();
downloadFolder = new File(pluginFolder, "downloads");
backupFolder = new File(pluginFolder, "backups");
}
/**
* Downloads the plugin from the given URL. If it is a compressed archive,
* it tries to get the .jars out of it
*
* @param url The url to download from
* @return True on success, false otherwise
*/
public boolean download(URL url) {
if (!pluginFolder.exists()) {
pluginFolder.mkdir();
}
String path = url.getPath();
int index = path.lastIndexOf('/');
File file = new File(pluginFolder, path.substring(index + 1));
return download(url, file);
}
/**
* Downloads the file to a given location
*
* @param url The url of the file to download
* @param file The location to store the file
*/
private boolean download(URL url, File file) {
if (!downloadFolder.exists()) {
downloadFolder.mkdir();
}
File temp = new File(downloadFolder, file.getName());
if (temp.exists()) {
temp.delete();
}
try {
temp.createNewFile();
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temp));
InputStream inputStream = url.openStream();
copyInputStream(inputStream, outputStream);
outputStream.close();
inputStream.close();
} catch (IOException ex) {
logger.log(Level.SEVERE, "Download failed", ex);
return false;
}
if (file.exists()) {
backupFile(file);
file.delete();
}
temp.renameTo(file);
return true;
}
/**
* Copies to an output stream, from an input stream, until EOF
*
* @param in The input stream
* @param out The output stream
* @throws IOException Thrown when the read or write operation fails
*/
private static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
int len;
byte[] buffer = new byte[4096];
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
}
/**
* Moves the file to the backup folder.
*
* @param file The file to backup
*/
private void backupFile(File file) {
System.out.println("Backing up old file: " + file.getName());
if (!backupFolder.exists()) {
backupFolder.mkdir();
}
file.renameTo(new File(backupFolder, file.getName() + ".bak"));
}
}