-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathTinfoilUsbCli.java
More file actions
95 lines (78 loc) · 2.93 KB
/
Copy pathTinfoilUsbCli.java
File metadata and controls
95 lines (78 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.cli;
import nsusbloader.COM.USB.UsbCommunications;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TinfoilUsbCli {
private final String[] arguments;
private List<File> filesList;
public TinfoilUsbCli(String[] arguments) throws InterruptedException, IncorrectSetupException{
this.arguments = arguments;
checkArguments();
parseFilesArguments();
runTinfoilBackend();
}
private void checkArguments() throws IncorrectSetupException{
if (arguments == null || arguments.length == 0) {
throw new IncorrectSetupException("No files?\n" +
"Try 'ns-usbloader -h' for more information.");
}
}
private void parseFilesArguments() throws IncorrectSetupException{
filesList = new ArrayList<>();
File file;
for (String arg : arguments) {
file = new File(arg);
if (!file.exists()) {
continue;
}
if (file.isDirectory()) {
filesList.addAll(fillListOfFiles(file));
} else {
filesList.add(file);
}
}
if (filesList.size() == 0) {
throw new IncorrectSetupException("File(s) doesn't exist.\n" +
"Try 'ns-usbloader -n help' for more information.");
}
}
public List<File> fillListOfFiles(File rootDir) {
try (Stream<Path> stream = Files.walk(rootDir.toPath())) {
return stream
.map(Path::toFile)
.filter(File::isFile)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
private void runTinfoilBackend() throws InterruptedException{
Runnable task = new UsbCommunications(filesList, "TinFoil", false);
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
thread.join();
}
}