Update
This commit is contained in:
parent
ea84166cf5
commit
9af4edfae3
|
|
@ -68,9 +68,10 @@ public class AliYun {
|
||||||
private final ReentrantLock lock;
|
private final ReentrantLock lock;
|
||||||
private final Cache cache;
|
private final Cache cache;
|
||||||
|
|
||||||
|
private MultiThreadedDownloader downloader;
|
||||||
private ScheduledExecutorService service;
|
private ScheduledExecutorService service;
|
||||||
private AlertDialog dialog;
|
|
||||||
private String refreshToken;
|
private String refreshToken;
|
||||||
|
private AlertDialog dialog;
|
||||||
private Share share;
|
private Share share;
|
||||||
|
|
||||||
private static class Loader {
|
private static class Loader {
|
||||||
|
|
@ -515,7 +516,9 @@ public class AliYun {
|
||||||
if (thread == 1) {
|
if (thread == 1) {
|
||||||
return new Object[]{ProxyVideo.proxy(downloadUrl, headers)};
|
return new Object[]{ProxyVideo.proxy(downloadUrl, headers)};
|
||||||
} else {
|
} else {
|
||||||
return new Object[]{new MultiThreadedDownloader(downloadUrl, headers, thread).start()};
|
if (downloader != null) downloader.destory();
|
||||||
|
downloader = new MultiThreadedDownloader(downloadUrl, headers, thread);
|
||||||
|
return new Object[]{downloader.start()};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ import java.net.URLEncoder;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
|
||||||
|
|
||||||
public class MultiThread {
|
public class MultiThread {
|
||||||
|
|
||||||
public static String url(String url, int thread) {
|
public static String url(String url, int thread) {
|
||||||
|
|
@ -20,8 +18,7 @@ public class MultiThread {
|
||||||
Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||||
for (String key : params.keySet()) headers.put(key, params.get(key));
|
for (String key : params.keySet()) headers.put(key, params.get(key));
|
||||||
MultiThreadedDownloader downloader = new MultiThreadedDownloader(url, removeHeaders(headers), thread);
|
MultiThreadedDownloader downloader = new MultiThreadedDownloader(url, removeHeaders(headers), thread);
|
||||||
NanoHTTPD.Response response = downloader.start();
|
return new Object[]{downloader.start()};
|
||||||
return new Object[]{response};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> removeHeaders(Map<String, String> headers) {
|
private static Map<String, String> removeHeaders(Map<String, String> headers) {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
|
||||||
import com.github.catvod.net.OkHttp;
|
import com.github.catvod.net.OkHttp;
|
||||||
|
import com.github.catvod.spider.Init;
|
||||||
|
|
||||||
import java.io.PipedInputStream;
|
import java.io.PipedInputStream;
|
||||||
import java.io.PipedOutputStream;
|
import java.io.PipedOutputStream;
|
||||||
|
|
@ -12,6 +13,8 @@ import java.util.HashMap;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
|
|
@ -27,6 +30,7 @@ public class MultiThreadedDownloader {
|
||||||
//已开始下载的 chunk 队列
|
//已开始下载的 chunk 队列
|
||||||
private final BlockingQueue<Chunk> readyChunkQueue;
|
private final BlockingQueue<Chunk> readyChunkQueue;
|
||||||
private final Map<String, String> headers;
|
private final Map<String, String> headers;
|
||||||
|
private final ExecutorService executor;
|
||||||
private final String url;
|
private final String url;
|
||||||
private final Lock lock;
|
private final Lock lock;
|
||||||
//最多缓存多少个未被取走的chunk
|
//最多缓存多少个未被取走的chunk
|
||||||
|
|
@ -54,6 +58,7 @@ public class MultiThreadedDownloader {
|
||||||
this.numThreads = numThreads;
|
this.numThreads = numThreads;
|
||||||
this.lock = new ReentrantLock();
|
this.lock = new ReentrantLock();
|
||||||
this.readyChunkQueue = new LinkedBlockingQueue<>();
|
this.readyChunkQueue = new LinkedBlockingQueue<>();
|
||||||
|
this.executor = Executors.newFixedThreadPool(numThreads);
|
||||||
}
|
}
|
||||||
|
|
||||||
//开始下载
|
//开始下载
|
||||||
|
|
@ -125,7 +130,7 @@ public class MultiThreadedDownloader {
|
||||||
//开启多线程下载
|
//开启多线程下载
|
||||||
running = true;
|
running = true;
|
||||||
for (int i = 0; i < numThreads; ++i) {
|
for (int i = 0; i < numThreads; ++i) {
|
||||||
new Thread(MultiThreadedDownloader.this::worker).start();
|
executor.execute(this::worker);
|
||||||
}
|
}
|
||||||
|
|
||||||
//构造response
|
//构造response
|
||||||
|
|
@ -140,7 +145,7 @@ public class MultiThreadedDownloader {
|
||||||
}
|
}
|
||||||
|
|
||||||
//搬运数据流
|
//搬运数据流
|
||||||
new Thread(() -> {
|
Init.execute(() -> {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
byte[] buffer = read();
|
byte[] buffer = read();
|
||||||
|
|
@ -159,7 +164,7 @@ public class MultiThreadedDownloader {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
|
||||||
return mResponse;
|
return mResponse;
|
||||||
}
|
}
|
||||||
|
|
@ -194,6 +199,9 @@ public class MultiThreadedDownloader {
|
||||||
|
|
||||||
private void worker() {
|
private void worker() {
|
||||||
while (running) {
|
while (running) {
|
||||||
|
//打斷技能
|
||||||
|
if (Thread.interrupted()) break;
|
||||||
|
|
||||||
//生成下一个chunk
|
//生成下一个chunk
|
||||||
Chunk chunk = null;
|
Chunk chunk = null;
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
@ -268,6 +276,11 @@ public class MultiThreadedDownloader {
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void destory() {
|
||||||
|
running = false;
|
||||||
|
executor.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
private static class Chunk {
|
private static class Chunk {
|
||||||
|
|
||||||
private final long startOffset;
|
private final long startOffset;
|
||||||
|
|
@ -283,7 +296,7 @@ public class MultiThreadedDownloader {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(byte[] buffer) throws InterruptedException {
|
public void put(byte[] buffer) {
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1 +1 @@
|
||||||
9060b67dfec8782aca6e79ba17c0979b
|
b298a8b958138c4e48aa0858886e7880
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue