Switched from Update to ResourceGrabber, next is cache support...
This commit is contained in:
parent
3ccf637838
commit
4cff6d2a68
|
@ -20,8 +20,6 @@
|
||||||
|
|
||||||
package org.moparscape.classloader;
|
package org.moparscape.classloader;
|
||||||
|
|
||||||
import org.moparscape.Update;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -108,7 +106,9 @@ public class CRCClassLoader extends ClassLoader {
|
||||||
// fos.close();
|
// fos.close();
|
||||||
|
|
||||||
// use Update instead
|
// use Update instead
|
||||||
new Update(backupURL, jarFileLoc, true);
|
//new Update(backupURL, jarFileLoc, true);
|
||||||
|
// use ResourceGrabber
|
||||||
|
org.moparscape.res.ResourceGrabber.getRG().downloadWaitCatch(backupURL, jarFileLoc);
|
||||||
|
|
||||||
setup(jarFileLoc);
|
setup(jarFileLoc);
|
||||||
|
|
||||||
|
|
|
@ -46,13 +46,16 @@ import java.util.List;
|
||||||
* This is for security reasons. The only exception to this rule is it will download java_client.exe for internal
|
* This is for security reasons. The only exception to this rule is it will download java_client.exe for internal
|
||||||
* purposes, but will only run it if the CRC is correct.
|
* purposes, but will only run it if the CRC is correct.
|
||||||
* <p/>
|
* <p/>
|
||||||
* This class is currently NOT thread-safe, so if you try and use it in a multi-threaded environment it will almost
|
* This class should be thread-safe.
|
||||||
* certainly break in unexpected and bad ways. Synchronize around it if you must.
|
* <p/>
|
||||||
|
* Only one instance of this class needs to be instantiated per-VM. Currently this is enforced via the constructors.
|
||||||
*
|
*
|
||||||
* @author moparisthebest
|
* @author moparisthebest
|
||||||
*/
|
*/
|
||||||
public class ResourceGrabber {
|
public class ResourceGrabber {
|
||||||
|
|
||||||
|
private static ResourceGrabber _instance = null;
|
||||||
|
|
||||||
private final Downloader[] downloaders;
|
private final Downloader[] downloaders;
|
||||||
|
|
||||||
private static final int delay = 500; //milliseconds
|
private static final int delay = 500; //milliseconds
|
||||||
|
@ -79,7 +82,7 @@ public class ResourceGrabber {
|
||||||
"You can specify as many downloads on the command line as you wish.");
|
"You can specify as many downloads on the command line as you wish.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ResourceGrabber rg = new ResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
|
ResourceGrabber rg = getResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
|
||||||
int[] uids = new int[args.length / 4];
|
int[] uids = new int[args.length / 4];
|
||||||
for (int x = 0; x < uids.length; ++x) {
|
for (int x = 0; x < uids.length; ++x) {
|
||||||
int argIndex = x * 4;
|
int argIndex = x * 4;
|
||||||
|
@ -133,7 +136,7 @@ public class ResourceGrabber {
|
||||||
for(String p : s.split(":"))
|
for(String p : s.split(":"))
|
||||||
System.out.println("part: '"+p.trim()+"'");
|
System.out.println("part: '"+p.trim()+"'");
|
||||||
if(true) return; */
|
if(true) return; */
|
||||||
ResourceGrabber rg = new ResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
|
ResourceGrabber rg = getResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
|
||||||
System.out.println("before downloads...");
|
System.out.println("before downloads...");
|
||||||
int clientZipUID = -1;
|
int clientZipUID = -1;
|
||||||
try {
|
try {
|
||||||
|
@ -152,7 +155,7 @@ public class ResourceGrabber {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceGrabber(String binDir, String title) throws FileNotFoundException {
|
private ResourceGrabber(String binDir, String title) throws FileNotFoundException {
|
||||||
if(title != null)
|
if(title != null)
|
||||||
this.title = title;
|
this.title = title;
|
||||||
File f = new File(binDir);
|
File f = new File(binDir);
|
||||||
|
@ -163,10 +166,30 @@ public class ResourceGrabber {
|
||||||
downloaders = new Downloader[]{new BTDownloader(binDir), new URLDownloader()};
|
downloaders = new Downloader[]{new BTDownloader(binDir), new URLDownloader()};
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceGrabber(String binDir) throws FileNotFoundException {
|
private ResourceGrabber(String binDir) throws FileNotFoundException {
|
||||||
this(binDir, null);
|
this(binDir, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized static ResourceGrabber getResourceGrabber(String binDir, String title) throws FileNotFoundException{
|
||||||
|
if(_instance == null)
|
||||||
|
_instance = new ResourceGrabber(binDir, title);
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized static ResourceGrabber getResourceGrabber() throws IllegalStateException{
|
||||||
|
if(_instance == null)
|
||||||
|
throw new IllegalStateException("Must call getResourceGrabber method with parameters first.");
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResourceGrabber getRG() throws IllegalStateException{
|
||||||
|
return getResourceGrabber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResourceGrabber getResourceGrabber(String binDir) throws FileNotFoundException{
|
||||||
|
return getResourceGrabber(binDir, null);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean wait(int uid) throws Exception {
|
public boolean wait(int uid) throws Exception {
|
||||||
// -1 is a special value meaning return immediately
|
// -1 is a special value meaning return immediately
|
||||||
// maybe because CRC was already correct and download not needed
|
// maybe because CRC was already correct and download not needed
|
||||||
|
@ -236,6 +259,35 @@ public class ResourceGrabber {
|
||||||
return uid;
|
return uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean downloadWait(String url, String savePath) throws Exception {
|
||||||
|
return this.downloadWait(url, savePath, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean downloadWait(String url, String savePath, boolean extract) throws Exception {
|
||||||
|
return this.downloadWait(url, savePath, extract, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean downloadWait(String url, String savePath, boolean extract, ChecksumInfo ci) throws Exception {
|
||||||
|
return this.wait(this.download(url, savePath, extract, ci));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean downloadWaitCatch(String url, String savePath) {
|
||||||
|
return this.downloadWaitCatch(url, savePath, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean downloadWaitCatch(String url, String savePath, boolean extract) {
|
||||||
|
return this.downloadWaitCatch(url, savePath, extract, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean downloadWaitCatch(String url, String savePath, boolean extract, ChecksumInfo ci) {
|
||||||
|
try{
|
||||||
|
return this.wait(this.download(url, savePath, extract, ci));
|
||||||
|
}catch(Exception e){
|
||||||
|
// ignore, just return false
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized int getUID() {
|
private synchronized int getUID() {
|
||||||
return this.currentUID++;
|
return this.currentUID++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,13 +52,18 @@ public class URLDownloader extends Downloader {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
// first make savedTo dir
|
// lets detect how we want to save this, if the path ends in a /, save the file named as-is in that folder
|
||||||
new File(savePath).mkdirs();
|
// but if it ends in anything else, save the file to that file exactly
|
||||||
String sp = savePath;
|
String saveTo;
|
||||||
if (!sp.endsWith("/"))
|
if (savePath.endsWith("/")) {
|
||||||
sp += "/";
|
// first make savePath dir
|
||||||
|
new File(savePath).mkdirs();
|
||||||
String savedTo = sp + url.substring(url.lastIndexOf('/') + 1);
|
saveTo = savePath + url.substring(url.lastIndexOf('/') + 1);
|
||||||
|
} else {
|
||||||
|
// create all directories except last file
|
||||||
|
new File(savePath.substring(0, savePath.lastIndexOf('/'))).mkdirs();
|
||||||
|
saveTo = savePath;
|
||||||
|
}
|
||||||
URLConnection uc = new URL(url).openConnection();
|
URLConnection uc = new URL(url).openConnection();
|
||||||
if (uc instanceof HttpURLConnection) {
|
if (uc instanceof HttpURLConnection) {
|
||||||
String userAgent = System.getProperty("http.agent");
|
String userAgent = System.getProperty("http.agent");
|
||||||
|
@ -70,14 +75,14 @@ public class URLDownloader extends Downloader {
|
||||||
InputStream in = uc.getInputStream();
|
InputStream in = uc.getInputStream();
|
||||||
|
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.starting("Downloading " + url, length, "to " + savedTo + "...");
|
callback.starting("Downloading " + url, length, "to " + saveTo + "...");
|
||||||
in = new ProgressInputStream(in, callback);
|
in = new ProgressInputStream(in, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
writeStream(in, new FileOutputStream(savedTo));
|
writeStream(in, new FileOutputStream(saveTo));
|
||||||
|
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.finished(sp, savedTo);
|
callback.finished(savePath, saveTo);
|
||||||
callback.stopped();
|
callback.stopped();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
Loading…
Reference in New Issue