-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUpdaterMain.java
168 lines (141 loc) · 5.13 KB
/
UpdaterMain.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.awt.Font;
import java.awt.SystemColor;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class UpdaterMain {
private JFrame frmUpdater;
private JProgressBar progressBar;
public UpdaterMain(final String downloadLink, final String fileName) {
try {
initialize();
Thread.sleep(1000L);
new Thread(new Runnable() {
public void run() {
download(downloadLink, fileName);
deleteOldFile(fileName);
close(fileName);
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(final String[] args) throws Exception {
if (args.length != 2) {
throw new Exception(args.length + " is not the correct amount of arguments");
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UpdaterMain(args[0], args[1]);
}
});
}
private void initialize() {
frmUpdater = new JFrame();
frmUpdater.setAlwaysOnTop(true);
frmUpdater.setResizable(false);
frmUpdater.setTitle("Keplerbot - Updating");
frmUpdater.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
JTextPane txtpnUpdating = new JTextPane();
txtpnUpdating.setBackground(SystemColor.menu);
txtpnUpdating.setFont(new Font("Trebuchet MS", Font.BOLD, 24));
txtpnUpdating.setEditable(false);
txtpnUpdating.setText("Updating...");
GroupLayout groupLayout = new GroupLayout(frmUpdater.getContentPane());
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE).addContainerGap()).addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup().addComponent(txtpnUpdating, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGap(126)))));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(txtpnUpdating, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGap(11).addComponent(progressBar, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
frmUpdater.getContentPane().setLayout(groupLayout);
frmUpdater.pack();
frmUpdater.setVisible(true);
}
private void download(String link, String filename) {
try {
URL url = new URL(link);
URLConnection conn = url.openConnection();
int size = conn.getContentLength();
progressBar.setMaximum(size);
progressBar.setValue(0);
progressBar.setIndeterminate(false);
int total = 0;
if (size < 0) {
throw new Exception("Unable to find remote file");
} else {
try {
byte abyte0[] = new byte[4096];
DataInputStream datainputstream = new DataInputStream(conn.getInputStream());
DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(new File("./" + filename + "_new")));
for (int i = 0; (i = datainputstream.read(abyte0)) >= 0;) {
total += i;
progressBar.setValue(total);
progressBar.repaint();
dataoutputstream.write(abyte0, 0, i);
}
datainputstream.close();
dataoutputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
conn.getInputStream().close();
} catch (Exception e){
e.printStackTrace();
}
try {
File newFile = new File(filename + "_new");
URL url = new URL(link);
URLConnection conection = url.openConnection();
conection.connect();
progressBar.setMaximum(conection.getContentLength());
FileOutputStream output = new FileOutputStream(newFile);
InputStream input = url.openStream();
byte[] buffer = new byte[1024];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
progressBar.setValue(progressBar.getValue() + bytesRead);
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
close(filename);
}
}
private void deleteOldFile(String filename) {
try {
File oldFile = new File(filename);
File newFile = new File(filename + "_new");
if (oldFile.delete()) {
newFile.renameTo(oldFile);
} else {
System.out.println("Failed to delete old jar");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void close(String filename) {
try {
Runtime.getRuntime().exec("java -jar " + filename);
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}