Skip to content

Commit 6eb5106

Browse files
author
Chortowod
committed
v1.0
Creating repository
1 parent 5a02949 commit 6eb5106

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# IntellijIdea files
2-
*.iml
32
.idea
43
out
4+
files/downloads

FirstJava.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="org.apache.commons:commons-io:1.3.2" level="project" />
11+
</component>
12+
</module>

files/to_download.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
http://www.lindberg.no/hires/mqa-cd-2018/2L-120_01_stereo.mqacd.mqa.flac Carl_Nielsen_Chaconne
2+
http://www.lindberg.no/hires/mqa-cd-2018/2L-120_01_stereo.mqacd.mqa.flac Carl_Nielsen_Chaconne_copy
3+
http://www.lindberg.no/hires/mqa-cd-2018/2L-120_01_stereo.mqacd.mqa.flac Carl_Nielsen_Chaconne_copy2
4+
http://kremlin.ru/static/pdf/constitution.pdf Конституция
5+
http://pozdravok.ru/cards/prazdniki/pozdravok_ru.gif Открытка
6+
http://www.lindberg.no/hires/mqa-cd-2018/2L-145_01_stereo.mqacd.mqa.flac Hoff_Innocence
7+
http://pozdravok.ru/cards/prazdniki/pozdravok_ru.gif Открытка2
8+
http://www.lindberg.no/hires/test/2L-125_stereo-44k-16b_04.flac Britten_Frank_Bridge_Variations_Romance
9+
http://kremlin.ru/static/pdf/constitution.pdf Конституция2

src/META-INF/MANIFEST.MF

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.company.Main
3+

src/com/company/Downloader.java

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.company;
2+
3+
import java.io.*;
4+
import java.net.URL;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.nio.file.StandardCopyOption;
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.concurrent.ExecutorService;
14+
import java.util.concurrent.Executors;
15+
import java.util.concurrent.TimeUnit;
16+
17+
public class Downloader {
18+
public static final String RESET = "\u001B[0m";
19+
public static final String GREEN = "\u001B[32m";
20+
public static final String YELLOW = "\u001B[33m";
21+
public static final String CYAN = "\u001B[36m";
22+
23+
static private Map<URL, List<String>> store = new HashMap<>();
24+
25+
private static long download(URL url, String outputFile) throws IOException {
26+
BufferedInputStream in = new BufferedInputStream(url.openStream());
27+
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
28+
byte[] dataBuffer = new byte[1024];
29+
int bytesRead;
30+
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1)
31+
fileOutputStream.write(dataBuffer, 0, bytesRead);
32+
return Files.size(Paths.get(outputFile)) / 1024;
33+
}
34+
35+
private static void copyFile(String originalFilePath, String copyPathFile) throws IOException {
36+
Files.copy(Paths.get(originalFilePath), Paths.get(copyPathFile), StandardCopyOption.REPLACE_EXISTING);
37+
}
38+
39+
private static String getExtension(String fileName) {
40+
String extension = "";
41+
int i = fileName.lastIndexOf('.');
42+
if (i >= 0)
43+
extension = fileName.substring(i);
44+
return extension;
45+
}
46+
47+
private static void parseToStore(File inputFile) throws IOException {
48+
BufferedReader br = new BufferedReader(new FileReader(inputFile));
49+
String line;
50+
while ((line = br.readLine()) != null) {
51+
String[] temp = line.split(" ");
52+
URL url = new URL(temp[0]);
53+
if (store.containsKey(url))
54+
store.get(url)
55+
.add(temp[1] + getExtension(temp[0]));
56+
else
57+
store.put(url, new ArrayList<>() {{add(temp[1] + getExtension(temp[0]));}});
58+
}
59+
60+
}
61+
62+
public static void downloadFromFile(int threads, String inputFile, String outputFolder) throws IOException, InterruptedException {
63+
final long[] totalTime = {0};
64+
final long[] totalSize = {0};
65+
File inputtedFile = new File(inputFile);
66+
Path path = Paths.get(outputFolder);
67+
68+
if (!Files.isDirectory(path))
69+
Files.createDirectory(path);
70+
71+
// if (!Files.exists(Paths.get(outputFolder)))
72+
// Files.createDirectories(Paths.get(outputFolder));
73+
74+
75+
76+
parseToStore(inputtedFile);
77+
ExecutorService executor = Executors.newFixedThreadPool(threads);
78+
79+
for(Map.Entry<URL, List<String>> entry : store.entrySet()) {
80+
URL url = entry.getKey();
81+
List<String> list = entry.getValue();
82+
executor.execute(() -> {
83+
long time = System.currentTimeMillis();
84+
long size = 0;
85+
try {
86+
System.out.printf("%sЗагружается файл %s%s\n", YELLOW, list.get(0), RESET);
87+
size = download(url, outputFolder + list.get(0));
88+
}
89+
catch (IOException e) { e.printStackTrace(); }
90+
time = (System.currentTimeMillis() - time) / 1000;
91+
System.out.printf("%s[Готово!] Загрузка файла %s завершена: %d KB за %d секунд%s\n", GREEN, list.get(0), size, time, RESET);
92+
if (totalTime[0] < time) totalTime[0] = time;
93+
// если есть одинаковые ссылки, то мы копируем уже скачанный файл
94+
// столько раз, сколько одинаковых ссылок
95+
if (list.size() > 1) {
96+
for (int i = 1; i < list.size(); i++) {
97+
try { copyFile(outputFolder + list.get(0), outputFolder + list.get(i)); }
98+
catch (IOException e) { e.printStackTrace(); }
99+
System.out.printf("%s[Инфо] Файл %s уже был загружен ранее, но сохранен как копия под указанным именем: %s%s\n",
100+
CYAN, list.get(0), list.get(i), RESET);
101+
}
102+
}
103+
totalSize[0] += size;
104+
});
105+
}
106+
107+
executor.shutdown();
108+
executor.awaitTermination(24, TimeUnit.HOURS);
109+
110+
System.out.printf("\nЗавершено: 100%%\nЗагружено: %d файлов, %.1f MB\nВремя: %d секунд\nСредняя скорость: %.1f MB/s\n",
111+
store.size(), totalSize[0]/1024d, totalTime[0], totalSize[0]/1024d/totalTime[0]);
112+
113+
}
114+
}

src/com/company/Main.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.company;
2+
3+
import org.apache.commons.io.FileUtils;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
public class Main {
12+
public static void main(String[] s) throws IOException, InterruptedException {
13+
String inputFile = "files/to_download.txt";
14+
String outputFile = "files/downloads/";
15+
Downloader.downloadFromFile(7, inputFile, outputFile);
16+
}
17+
}

0 commit comments

Comments
 (0)