Skip to content

Commit

Permalink
downloader: optimized observer
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Apr 11, 2022
1 parent b4e234e commit 7e699da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/itxtech/mcl/impl/DefaultDownloader.java
Expand Up @@ -4,6 +4,7 @@
import org.itxtech.mcl.component.DownloadObserver;
import org.itxtech.mcl.component.Downloader;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Proxy;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void download(String url, File file, DownloadObserver observer) {
var proxy = loader.getProxy();
var connection = proxy == null ? new URL(url).openConnection() : new URL(url).openConnection(new Proxy(Proxy.Type.HTTP, proxy));
var totalLen = connection.getContentLength();
var is = connection.getInputStream();
var is = new BufferedInputStream(connection.getInputStream());
var os = new FileOutputStream(file);
var len = 0;
var buff = new byte[1024];
Expand All @@ -58,6 +59,7 @@ public void download(String url, File file, DownloadObserver observer) {
}
}
os.close();
is.close();
} catch (Throwable e) {
loader.logger.logException(e);
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/itxtech/mcl/module/builtin/MDownloader.java
Expand Up @@ -13,6 +13,29 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/*
*
* Mirai Console Loader
*
* Copyright (C) 2020-2022 iTX Technologies
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author PeratX
* @website https://github.com/iTXTech/mirai-console-loader
*
*/
public class MDownloader extends MclModule {
private static final String MAX_THREADS_KEY = "mdownloader.max-threads";

Expand Down Expand Up @@ -82,13 +105,15 @@ public void download(String url, File file, DownloadObserver observer) {
if (observer != null) {
observer.updateProgress((int) len, sum);
}
Thread.sleep(100);
}
var os = new BufferedOutputStream(new FileOutputStream(file));
for (var task : list) {
os.write(task.out.toByteArray());
}
os.flush();
os.close();
executor.shutdownNow();
}
} catch (Exception e) {
loader.logger.error(e);
Expand Down Expand Up @@ -131,6 +156,7 @@ public void run() {
}
os.flush();
os.close();
is.close();
} catch (Throwable e) {
Loader.getInstance().logger.logException(e);
}
Expand Down
Expand Up @@ -66,7 +66,7 @@ public void boot() {
if (loader.config.moduleProps.getOrDefault(AUTO_CLEAR_KEY, "true").equals("true") &&
loader.repo instanceof RepoWithCache) {
((RepoWithCache) loader.repo).clearCache();
loader.logger.debug("RepoWithCache has been cleared");
loader.logger.debug("RepoCache has been cleared");
}
}

Expand Down

0 comments on commit 7e699da

Please sign in to comment.