Now can download .pack.xz file for Forge library.

This commit is contained in:
huanghongxun
2015-07-28 20:36:41 +08:00
parent bc8cc93bd9
commit e31b5e3a28
103 changed files with 13147 additions and 460 deletions

View File

@@ -16,6 +16,7 @@
*/
package org.jackhuang.hellominecraft.tasks;
import java.util.ArrayList;
import java.util.Collection;
/**
@@ -65,6 +66,17 @@ public abstract class Task {
public void setParallelExecuting(boolean parallelExecuting) {
this.parallelExecuting = parallelExecuting;
}
ArrayList<DoingDoneListener<Task>> taskListener = new ArrayList();
public Task addTaskListener(DoingDoneListener<Task> l) {
taskListener.add(l);
return this;
}
public ArrayList<DoingDoneListener<Task>> getTaskListeners() {
return taskListener;
}
public abstract String getInfo();

View File

@@ -116,16 +116,22 @@ public class TaskList extends Thread {
HMCLog.log("Executing task: " + t.getInfo());
for (DoingDoneListener<Task> d : taskListener)
d.onDoing(t);
for (DoingDoneListener<Task> d : t.getTaskListeners())
d.onDoing(t);
if (t.executeTask()) {
HMCLog.log("Task finished: " + t.getInfo());
for (DoingDoneListener<Task> d : taskListener)
d.onDone(t);
for (DoingDoneListener<Task> d : t.getTaskListeners())
d.onDone(t);
processTasks(t.getAfterTasks());
} else {
HMCLog.err("Task failed: " + t.getInfo(), t.getFailReason());
for (DoingDoneListener<Task> d : taskListener)
d.onFailed(t);
for (DoingDoneListener<Task> d : t.getTaskListeners())
d.onFailed(t);
}
}

View File

@@ -68,9 +68,8 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch (GeneralSecurityException gse) {
}
if (sslContext != null) {
if (sslContext != null)
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
}
@@ -110,37 +109,33 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
private void closeFiles() {
// Close file.
if (file != null) {
if (file != null)
try {
file.close();
file = null;
} catch (IOException e) {
HMCLog.warn("Failed to close file", e);
}
}
// Close connection to server.
if (stream != null) {
if (stream != null)
try {
stream.close();
stream = null;
} catch (IOException e) {
HMCLog.warn("Failed to close stream", e);
}
}
}
// Download file.
@Override
public boolean executeTask() {
for (PreviousResult<String> p : al) {
for (PreviousResult<String> p : al)
this.url = IOUtils.parseURL(p.getResult());
}
for (int repeat = 0; repeat < 6; repeat++) {
if (repeat > 0) {
if (repeat > 0)
HMCLog.warn("Failed to download, repeat: " + repeat);
}
try {
// Open connection to URL.
@@ -167,16 +162,14 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
}
// Set the size for this download if it hasn't been already set.
if (size == -1) {
if (size == -1)
size = contentLength;
}
filePath.getParentFile().mkdirs();
File tempFile = new File(filePath.getAbsolutePath() + ".hmd");
if (!tempFile.exists()) {
if (!tempFile.exists())
tempFile.createNewFile();
}
// Open file and seek to the end of it.
file = new RandomAccessFile(tempFile, "rw");
@@ -195,23 +188,20 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1) {
if (read == -1)
break;
}
// Write buffer to file.
file.write(buffer, 0, read);
downloaded += read;
if (ppl != null) {
if (ppl != null)
ppl.setProgress(this, downloaded, size);
}
}
closeFiles();
tempFile.renameTo(filePath);
if (ppl != null) {
if (ppl != null)
ppl.onProgressProviderDone(this);
}
return true;
} catch (Exception e) {
setFailReason(new NetException(C.i18n("download.failed") + " " + url, e));

View File

@@ -16,6 +16,7 @@
*/
package org.jackhuang.hellominecraft.utils.system;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
@@ -25,6 +26,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.NetUtils;
@@ -73,7 +75,7 @@ public class FileUtils {
public static void cleanDirectory(File directory)
throws IOException {
if (!directory.exists()) {
//String message = directory + " does not exist";
//String message = directory + " does not exist";
//throw new IllegalArgumentException(message);
directory.mkdirs();
return;
@@ -429,4 +431,15 @@ public class FileUtils {
if (f.getName().endsWith(suffix)) al.add(f);
return al.toArray(new File[0]);
}
public static byte[] toByteArray(File file) throws IOException {
try (FileInputStream is = new FileInputStream(file)) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int n;
byte[] b = new byte[1024];
while ((n = is.read(b)) != -1) os.write(b, 0, n);
os.close();
return os.toByteArray();
}
}
}