https://github.com/core-lib/network
Java HTTP/HTTPS Downloader
https://github.com/core-lib/network
Last synced: 9 months ago
JSON representation
Java HTTP/HTTPS Downloader
- Host: GitHub
- URL: https://github.com/core-lib/network
- Owner: core-lib
- License: apache-2.0
- Created: 2018-01-19T07:15:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-26T02:21:42.000Z (over 8 years ago)
- Last Synced: 2025-02-23T13:51:21.573Z (over 1 year ago)
- Language: Java
- Size: 74.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Network
=======
Java HTTP / HTTPS Downloader Implementation
> ## Installation
```
...
jitpack.io
https://jitpack.io
...
...
com.github.core-lib
network
v1.3.0
...
```
> ## Usages
* ### Block Mode
```
@Test
public void testBlock() throws Exception {
Network.download(URL)
.block()
.to(File.createTempFile("network", ".tmp"));
}
```
* ### Async Mode
```
@Test
public void testAsynchronous() throws Exception {
final Object lock = new Object();
Network.download(URL)
.asynchronous()
.callback(new CallbackAdapter() {
@Override
public void complete(AsynchronousDownloader> downloader, boolean success, Exception exception) {
synchronized (lock) {
lock.notify();
}
}
})
.to(File.createTempFile("network", ".tmp"));
synchronized (lock) {
lock.wait();
}
}
```
* ### Resumable Mode
```
@Test
public void testResumable() throws Exception {
final Object lock = new Object();
Network.download(URL)
.resumable(3) // max retry 3 times if error occur while downloading
.callback(new CallbackAdapter() {
@Override
public void complete(AsynchronousDownloader> downloader, boolean success, Exception exception) {
synchronized (lock) {
lock.notify();
}
}
})
.to(File.createTempFile("network", ".tmp"));
synchronized (lock) {
lock.wait();
}
}
```
* ### Concurrent Mode
```
@Test
public void testConcurrent() throws Exception {
final Object lock = new Object();
Network.download(URL)
.concurrent(3) // use 3 threads to download a resource in same time, but the server must supports it
.times(3) // every thread max retry 3 times if error occur while downloading
.callback(new CallbackAdapter() {
@Override
public void complete(AsynchronousDownloader> downloader, boolean success, Exception exception) {
synchronized (lock) {
lock.notify();
}
}
})
.to(File.createTempFile("network", ".tmp"));
synchronized (lock) {
lock.wait();
}
}
```
> ## Download progress listening
```
@Test
public void testListen() throws Exception {
final Object lock = new Object();
Network.download(URL)
.asynchronous()
.listener(new ListenerAdapter() {
@Override
public void start(Downloader> downloader, long total) {
System.out.println("download started and resource size is " + total + " bytes");
}
@Override
public void progress(Downloader> downloader, long total, long downloaded) {
System.out.println("downloading " + downloaded + " / " + total);
}
@Override
public void finish(Downloader> downloader, long total) {
System.out.println("download finished");
}
})
.callback(new CallbackAdapter() {
@Override
public void complete(AsynchronousDownloader> downloader, boolean success, Exception exception) {
synchronized (lock) {
lock.notify();
}
}
})
.to(File.createTempFile("network", ".tmp"));
synchronized (lock) {
lock.wait();
}
}
```
> ## Download to OutputStream / Output
```
@Test
public void testToOutputStream() throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Network.download(URL)
.block()
.to(out);
}
@Test
public void testToOutput() throws Exception {
final OutputStream out = new FileOutputStream(File.createTempFile("network", ".tmp"));
final DataOutput output = new DataOutputStream(out);
Network.download(URL)
.block()
.to(output);
}
```
> ## Specify download thread pool
```
@Test
public void testSpecifyDefaultThreadPool() throws Exception {
final ExecutorService executor = Executors.newFixedThreadPool(12);
Network.setDefaultExecutor(executor);
}
@Test
public void testSpecifyCustomThreadPool() throws Exception {
final ExecutorService executor = Executors.newFixedThreadPool(12);
final Object lock = new Object();
Network.download(URL)
.asynchronous(executor)
.callback(new CallbackAdapter() {
@Override
public void complete(AsynchronousDownloader> downloader, boolean success, Exception exception) {
synchronized (lock) {
lock.notify();
}
}
})
.to(File.createTempFile("network", ".tmp"));
synchronized (lock) {
lock.wait();
}
}
```
> ## Lambda
```
private synchronized void lock() throws InterruptedException {
this.wait();
}
private synchronized void open() {
this.notify();
}
@Test
public void testLambda() throws Exception {
Network.download(URL)
.asynchronous()
.start((downloader, total) -> System.out.println("download started and size is " + total + " bytes"))
.progress((downloader, total, downloaded) -> System.out.println(downloaded + " / " + total))
.finish((downloader, total) -> System.out.println("download finished"))
.success(downloader -> System.out.println("download success"))
.failure((downloader, exception) -> exception.printStackTrace())
.complete((downloader, success, exception) -> open())
.to(File.createTempFile("network", ".tmp"));
lock();
}
```