Modify Filesplitter. Add digest to Configuration dialog
[splitter-ng] / Splitter-ng-filesplitter / src / Filesplitter.java
1 import java.io.File;
2 import java.io.IOException;
3 import java.nio.file.DirectoryStream;
4 import java.nio.file.Files;
5 import java.nio.file.LinkOption;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Scanner;
15 import java.util.SortedSet;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
18
19 import org.ini4j.Ini;
20 import org.ini4j.Profile.Section;
21
22 import splitterng.CodingTask;
23 import splitterng.DecodingTask;
24 import splitterng.EncodingTask;
25 import splitterng.Splitter;
26 import splitterng.SplitterCallback;
27 import splitterng.plugin.CodecDescription;
28 import splitterng.plugin.PluginDescription;
29
30 public class Filesplitter implements SplitterCallback {
31         /*
32          * public static final String ANSI_RESET = ""; public static final String
33          * ANSI_BLACK = ""; public static final String ANSI_RED = ""; public static
34          * final String ANSI_GREEN = ""; public static final String ANSI_YELLOW =
35          * ""; public static final String ANSI_BLUE = ""; public static final String
36          * ANSI_PURPLE = ""; public static final String ANSI_CYAN = ""; public
37          * static final String ANSI_WHITE = ""; public static final String ANSI_BOLD
38          * = "";
39          */
40         public static final String ANSI_RESET = "\u001B[0m";
41         public static final String ANSI_BLACK = "\u001B[30m";
42         public static final String ANSI_RED = "\u001B[31m";
43         public static final String ANSI_GREEN = "\u001B[32m";
44         public static final String ANSI_YELLOW = "\u001B[33m";
45         public static final String ANSI_BLUE = "\u001B[34m";
46         public static final String ANSI_PURPLE = "\u001B[35m";
47         public static final String ANSI_CYAN = "\u001B[36m";
48         public static final String ANSI_WHITE = "\u001B[37m";
49         public static final String ANSI_BOLD = "\u001B[1m";
50
51         public Ini config;
52         public static String CONFIGFILE = "filesplitter.ini";
53
54         private List<CodingTask> finished_encoding_task = new LinkedList<CodingTask>();
55         private List<CodingTask> finished_decoding_task = new LinkedList<CodingTask>();
56         private List<CodingTask> running_tasks = new LinkedList<CodingTask>();
57         private List<String> codeclist = new LinkedList<String>();
58         private List<String> codeclist_configured = new LinkedList<String>();
59         private List<String> codeclist_failed = new LinkedList<String>();
60         // in der Map werden die Metadaten-vorlage für jeden Codec gespeichert
61         private List<Splitter> splitters = new LinkedList<Splitter>();
62         private Map<Splitter, Map<String, Object>> splitter_metadata_map = new HashMap<Splitter, Map<String, Object>>();
63
64         public static void main(String[] args) throws InterruptedException {
65                 Filesplitter filesplitter = new Filesplitter();
66                 filesplitter.run(args);
67
68                 // Thread.sleep(50000);
69                 // System.exit(0);
70         }
71
72         public String readCommandline() {
73                 return readCommandline("",false);
74         }
75         public String readCommandline(String string) {
76                 return readCommandline(string,false);
77         }
78         public String readCommandline(boolean allowblanklines) {
79                 return readCommandline("",allowblanklines);
80         }
81         public String readCommandline(String string ,boolean allowblanklines) {
82                 Scanner in;
83                 String line = null;
84                 in = new Scanner(System.in);
85                 while (line == null || (!allowblanklines && line.isEmpty())) {
86                         System.out.print(ANSI_BOLD +string+ ANSI_RESET);
87                         line = in.nextLine();
88                 }
89                 return line;
90
91         }
92
93         public void run(String[] args) {
94                 init_config();
95                 print_codecs();
96
97                 println_std("\nProcessing the following files:");
98                 Path srcDir = Paths.get("srcDir");
99                 List<Path> srcFileList = getFileList(srcDir);;
100                 for (String s:args){
101                         Path p = Paths.get(s);
102                         if (Files.exists(p, LinkOption.NOFOLLOW_LINKS)){
103                                 srcFileList.add(p);
104                         }
105                         else {
106                                 println_err("- " + p.toString() + " doesn't exists");
107                         }
108                         
109                 }
110                 for (Path entry : srcFileList) {
111                         println_std("- " + entry.toString());
112                 }
113
114                 // restoreDir anlegen bzw aufräumen
115                 Path splitDir = Paths.get("splitDir");
116                 checkDirectory(splitDir);
117                 for (Path entry : getFileList(splitDir)) {
118                         try {
119                                 Files.delete(entry);
120                         } catch (IOException e) {
121                                 // TODO Auto-generated catch block
122                                 e.printStackTrace();
123                         }
124                 }
125
126                 // restoreDir anlegen bzw aufräumen
127                 Path restoreDir = Paths.get("restoreDir");
128                 checkDirectory(restoreDir);
129                 for (Path entry : getFileList(restoreDir)) {
130                         try {
131                                 Files.delete(entry);
132                         } catch (IOException e) {
133                                 e.printStackTrace();
134                         }
135                 }
136                 // Jede Datei ...
137                 for (Path srcfile : srcFileList) {
138                         // ... mit jedem initialisierten Splitter bearbeiten
139                         for (Splitter splitter : splitters) {
140                                 encode(srcfile, splitter);
141
142                         }
143
144                 }
145
146                 println_info("-----");
147                 //println_info("All CodingTasks submitted");
148
149                 println_info("Use 'h' or 'help' to display commands");
150                 // printUsage();
151
152                 // dateien zusammenbauen
153                 String commandline;
154                 while (true) {
155                         commandline = readCommandline(">>>");
156                         if (commandline == null) {
157                                 println_std("Exit");
158                                 System.exit(0);
159                         }
160                         Scanner in = new Scanner(commandline);
161                         switch (in.next()) {
162                         //case "EOF":
163                         case "q":
164                         case "quit":
165                         case "exit":
166                                 System.exit(0);
167                         case "h":
168                         case "help":
169                                 printUsage();
170                                 break;
171                         case "ls":
172                                 if (in.hasNext()) {
173                                         switch (in.next()) {
174                                         case "enc":
175                                                 for (CodingTask t : finished_encoding_task) {
176                                                         println_ok(""
177                                                                         + finished_encoding_task.lastIndexOf(t)
178                                                                         + "\t" + t.getFilePath().getFileName());
179                                                 }
180                                                 break;
181                                         case "dec":
182                                                 for (CodingTask t : finished_decoding_task) {
183                                                         println_ok(""
184                                                                         + finished_decoding_task.lastIndexOf(t)
185                                                                         + "\t" + t.getFilePath().getFileName());
186                                                 }
187                                                 break;
188
189                                         case "frag":
190                                                 try {
191                                                         for (Entry<Integer, Path> e : finished_encoding_task
192                                                                         .get(Integer.parseInt(in.next()))
193                                                                         .getFragmentPaths().entrySet()) {
194                                                                 String color = Files.exists(e.getValue()) ? ANSI_GREEN
195                                                                                 : ANSI_RED;
196                                                                 System.out.format(color + "\t %-5s %s %s\n"
197                                                                                 + ANSI_RESET, e.getKey(), e.getValue()
198                                                                                 .toString(),
199                                                                                 (Files.exists(e.getValue()) ? "OK"
200                                                                                                 : "Fehlt"));
201                                                                 // println_std(++"\t"+e.getValue().toString());
202                                                         }
203                                                 } catch (Exception e) {
204                                                         println_err("Index not found. Use 'ls enc' to show valid indices");
205                                                 }
206                                                 break;
207
208                                         case "splitter":
209                                         case "splitters":
210                                                 print_splitter();
211                                                 break;
212
213                                         case "codec":
214                                         case "codecs":
215                                                         print_codecs();
216                                                 break;
217                                         }
218
219                                 }
220                                 break;
221
222                         case "meta":
223                                 if (in.hasNext()) {
224                                         switch (in.next()) {
225                                         case "enc":
226                                                 try {
227                                                         for (Entry<String, Object> e : finished_encoding_task
228                                                                         .get(Integer.parseInt(in.next()))
229                                                                         .getMetadata().entrySet()) {
230                                                                 println_ok(String.format("\t %-25s %-25s %s",
231                                                                                 e.getKey(), e.getValue().toString(), e
232                                                                                                 .getValue().getClass()
233                                                                                                 .toString()));
234                                                                 // println_std(++"\t"+e.getValue().toString());
235                                                         }
236                                                 } catch (Exception e) {
237                                                         println_err("Index not found. Use 'ls enc' to show valid indices");
238                                                 }
239                                                 break;
240
241                                         case "dec":
242                                                 try {
243                                                         for (Entry<String, Object> e : finished_decoding_task
244                                                                         .get(Integer.parseInt(in.next()))
245                                                                         .getMetadata().entrySet()) {
246                                                                 println_ok(String.format("\t %-25s %s",
247                                                                                 e.getKey(), e.getValue().toString()));
248                                                                 // println_std(++"\t"+e.getValue().toString());
249                                                         }
250                                                 } catch (Exception e) {
251                                                         println_err("Index not found. Use 'ls dec' to show valid indices");
252                                                 }
253                                                 break;
254                                         }
255                                 }
256                                 break;
257
258                         case "rm":
259                                 try {
260                                         int tasknr = Integer.parseInt(in.next());
261                                         int fragmentnr = Integer.parseInt(in.next());
262                                         Map<Integer, Path> fragmentPaths = finished_encoding_task
263                                                         .get(tasknr).getFragmentPaths();
264                                         Path path = fragmentPaths.get(fragmentnr);
265                                         try {
266                                                 Files.delete(path);
267                                         } catch (Exception e) {
268                                                 println_err("unable to delete fragment: "
269                                                                 + e.getMessage());
270
271                                         }
272
273                                 } catch (Exception e) {
274                                         println_err("Index not found. Use 'ls frag <index> ' to show valid indices");
275                                 }
276                                 break;
277
278                         case "dec":
279                                 try {
280                                         int tasknr = Integer.parseInt(in.next());
281                                         decode(restoreDir, tasknr);
282                                 } catch (Exception e) {
283                                         // e.printStackTrace();
284                                         println_err("Index not found. Use 'ls enc' to show valid indices");
285                                 }
286
287                                 break;
288                         case "enc":
289                                 String filename;
290                                 Path srcfile = null;
291                                 try {
292                                         filename = in.next();
293                                         srcfile = Paths.get(filename);
294                                         if (Files.notExists(srcfile, LinkOption.NOFOLLOW_LINKS)) {
295                                                 in.close();
296                                                 throw new Exception();
297                                         }
298                                 } catch (Exception e1) {
299                                         println_err("no valid Filename. Use 'enc <Filename> <splitternr>'");
300                                         break;
301                                 }
302                                 int splitternr;
303                                 Splitter splitter;
304                                 try {
305                                         splitternr = in.nextInt();
306                                         splitter = splitters.get(splitternr);
307                                 } catch (Exception e1) {
308                                         // TODO Auto-generated catch block
309                                         println_err("no valid Splitter-Infex. Use 'ls splitter' to show valid indices");
310                                         break;
311                                 }
312                                 try {
313                                         encode(srcfile, splitter);
314                                 } catch (Exception e1) {
315                                         // TODO Auto-generated catch block
316                                         println_err(e1.getMessage()
317                                                         + "\nEncoding failed. Use 'enc <splitternr> <Filename>'");
318                                 }
319                                 break;
320
321                         case "init":
322                                 try {
323                                         int codecnr = Integer.parseInt(in.next());
324                                         configure_codec(codecnr);
325                                 } catch (Exception e) {
326                                         println_err("Index not found. Use 'ls codec' to show valid indices");
327                                 }
328                                 break;
329                         case "reset":
330                                 try {
331                                         int codecnr = Integer.parseInt(in.next());
332                                         reset_codec(codecnr);
333                                 } catch (Exception e) {
334                                         println_err("Index not found. Use 'ls codec' to show valid indices");
335                                 }
336
337                                 break;
338                         default:
339                                 printUsage();
340                         }
341                         in.close();
342                 }
343
344         }
345
346         public void decode(Path restoreDir, int tasknr) {
347                 try {
348                         CodingTask enctask = (CodingTask) finished_encoding_task
349                                         .get(tasknr);
350                         Path restorefile = restoreDir.resolve(""
351                                         + System.currentTimeMillis()
352                                         + enctask.getFilePath().getFileName());
353
354                         Map<Integer, Path> fragmentPaths = enctask.getFragmentPaths();
355                         for (int i = 0; i < fragmentPaths.size(); i++) {
356                                 Path path = fragmentPaths.get(i);
357                                 if (path == null || !Files.exists(path)) {
358                                         fragmentPaths.put(i, null);
359                                 }
360
361                         }
362                         DecodingTask dectask = new DecodingTask(fragmentPaths, restorefile,
363                                         this, enctask.getMetadataAsDeepCopy());
364                         for (Splitter spl : splitters) {
365                                 if (spl.getCurrentPlugin().getPluginDescription().getPluginID() == (String) enctask
366                                                 .getMetadata("pluginid")
367                                                 && spl.getCurrentCodecDescription().getCodecID() == (String) enctask
368                                                                 .getMetadata("codecid")) {
369                                         println_ok("Auftrag angenommen..." + restorefile);
370                                         spl.decode(dectask);
371                                         running_tasks.add(dectask);
372                                 }
373                         }
374
375                 } catch (Exception e) {
376                         // e.printStackTrace();
377                         println_err("Index not found. Use 'ls enc' to show valid indices");
378                 }
379         }
380
381         public void print_splitter() {
382                 for (Splitter splitter : splitters) {
383                         String pid = splitter.getCurrentPlugin().getPluginDescription()
384                                         .getPluginID();
385                         String cid = splitter.getCurrentCodecDescription().getCodecID();
386                         println_ok(splitters.indexOf(splitter) + "\t" + pid + "/" + cid
387                                         + "  \t(" + splitter.getTaskQueueLength()
388                                         + " active Tasks)");
389                 }
390         }
391
392         public void encode(Path srcfile, Splitter splitter) {
393                 println_ok("Encoding: "
394                                 + srcfile
395                                 + "  with "
396                                 + splitter.getCurrentPlugin().getPluginDescription()
397                                                 .getPluginID() + "/"
398                                 + splitter.getCurrentCodecDescription().getCodecID());
399                 try {
400
401                         Map<String, Object> metadata = new TreeMap<String, Object>();
402
403                         for (Entry<String, Object> e : splitter_metadata_map.get(splitter)
404                                         .entrySet()) {
405                                 metadata.put(e.getKey(), e.getValue());
406                         }
407
408                         metadata.put("time_encoding_submitted", System.currentTimeMillis());
409
410                         // Hier wird codiert. Der Encodingtask
411                         EncodingTask task = new EncodingTask(srcfile,
412                                         Paths.get("splitDir"), this, metadata);
413                         splitter.encode(task);
414                         running_tasks.add(task);
415                 } catch (InterruptedException | IOException e) {
416                         e.printStackTrace();
417                 }
418         }
419
420         public void saveconfig() {
421                 try {
422                         config.store();
423                         println_ok("Configuration saved");
424                 } catch (IOException e1) {
425                         // TODO Auto-generated catch block
426                         e1.printStackTrace();
427                 }
428         }
429
430         public void init_config() {
431                 try {
432                         File configfile = new File(CONFIGFILE);
433                         if (!configfile.exists()) {
434                                 configfile.createNewFile();
435                         }
436                         config = new Ini(configfile);
437                 } catch (Exception e) {
438                         // TODO Auto-generated catch block
439                         e.printStackTrace();
440                 }
441                 for (PluginDescription p : Splitter.getPluginDescriptions()) {
442                         for (CodecDescription c : p.getCodecDescriptions()) {
443                                 String id = p.getPluginID() + "/" + c.getCodecID();
444                                 codeclist.add(id);
445                                 if (isInConfig(id)) {
446                                         init_codec(id);
447                                 }
448                         }
449                 }
450                 Collections.sort(codeclist);
451         }
452
453         private boolean isInConfig(String id) {
454                 Section s = config.get(id);
455                 if (s != null && s.containsKey("k") && s.containsKey("m")) {
456                         return true;
457                 }
458                 return false;
459         }
460
461         private void init_codec(String id) {
462                 Section section;
463                 section = config.get(id);
464
465                 // Build codecparameter map
466                 Map<String, Integer> codecparameter = new HashMap<>();
467                 for (Entry<String, String> e : section.entrySet()) {
468                         try {
469                                 codecparameter.put(e.getKey(), Integer.parseInt(e.getValue()));
470                         } catch (Exception ex) {
471                                 // nothing
472                         }
473                 }
474
475                 // build Metadata Template. Try tu use Integer, if it fails use String
476                 section = config.get(id + "_MetadataTemplate");
477                 Map<String, Object> metadatatemplate = new HashMap<>();
478                 if (section != null) {
479                         for (Entry<String, String> entry : section.entrySet()) {
480                                 try {
481                                         metadatatemplate.put(entry.getKey(),
482                                                         Integer.parseInt(entry.getValue()));
483                                 } catch (NumberFormatException ex) {
484                                         try {
485                                                 metadatatemplate.put(entry.getKey(), entry.getValue());
486                                         } catch (Exception e2) {
487                                                 // TODO: handle exception
488                                         }
489                                 }
490                         }
491                 }
492
493                 for (PluginDescription p : Splitter.getPluginDescriptions()) {
494                         for (CodecDescription c : p.getCodecDescriptions()) {
495                                 if (id.equals(p.getPluginID() + "/" + c.getCodecID())) {
496                                         try {
497                                                 Splitter splitter = new Splitter(p.getPluginID(),
498                                                                 c.getCodecID(), codecparameter);
499                                                 splitters.add(splitter);
500                                                 splitter_metadata_map.put(splitter, metadatatemplate);
501                                                 codeclist_configured.add(id);
502                                                 if (codeclist_failed.contains(id)){
503                                                         codeclist_failed.remove(id);
504                                                 }
505                                                 println_ok("Splitter created from config: " + id);
506                                         } catch (Exception e) {
507                                                 println_err("NO Splitter created for: " + id + "\n"
508                                                                 + e.getMessage());
509                                                 codeclist_failed.add(id);
510                                                 if (codeclist_configured.contains(id)){
511                                                         codeclist_configured.remove(id);
512                                                 }
513                                         }
514                                 }
515                         }
516                 }
517         }
518
519         private void reset_codec(int i) {
520                 String id = getCodecIDfromIndex(i);
521                 if (id == null) {
522                         println_err("Codec " + i + " existiert nicht");
523                         print_codecs();
524                         return;
525                 }
526                 config.remove(config.get(id));
527                 config.remove(config.get(id+"_MetadataTemplate"));
528                 saveconfig();
529                 if (codeclist_configured.contains(id)){
530                         codeclist_configured.remove(id);
531                 }
532                 if (codeclist_failed.contains(id)){
533                         codeclist_failed.remove(id);
534                 }
535                 for (Splitter splitter : splitters) {
536                         String pid = splitter.getCurrentPlugin().getPluginDescription()
537                                         .getPluginID();
538                         String cid = splitter.getCurrentCodecDescription().getCodecID();
539                         if (id.equals(pid + "/" + cid)) {
540                                 splitters.remove(splitter);
541                                 splitter_metadata_map.remove(splitter);
542                                 splitter.stop();
543                         }
544                 }
545         }
546
547         public void configure_codec(int i) {
548                 String id = getCodecIDfromIndex(i);
549                 if (id == null) {
550                         println_err("Codec " + i + " existiert nicht");
551                         print_codecs();
552                         return;
553                 }
554
555                 for (PluginDescription p : Splitter.getPluginDescriptions()) {
556                         for (CodecDescription c : p.getCodecDescriptions()) {
557                                 if (id.equals(p.getPluginID() + "/" + c.getCodecID())) {
558                                         Section section = config.get(id);
559                                         if (section == null) {
560                                                 section = config.add(id);
561                                         }
562                                         print_codecdescription(p, c);
563                                         println_std(id + " Configuration:");
564                                         Map<String, Integer[]> mapminmax = c.getCodecParameterMinMax();
565                                         Map<String, Integer> mapdefault = c.getCodecParameterDefaults();
566                                         SortedSet<String> keyset = new TreeSet<>(mapminmax.keySet());
567                                         Integer[] minmax;
568                                         Integer defaultval;
569                                         
570                                         String key = "k";
571                                         minmax = mapminmax.get(key);
572                                         print_std(key);
573                                         print_std("\tmin="+ minmax[0]);
574                                         print_std("\tmax="+ minmax[1]);
575                                         section.put(key, readCommandline("\t"+key+"=",false));
576                                         
577                                         key = "m";
578                                         minmax = mapminmax.get(key);
579                                         print_std(key);
580                                         print_std("\tmin="+ minmax[0]);
581                                         print_std("\tmax="+ minmax[1]);
582                                         section.put(key, readCommandline("\t"+key+"=",false));
583                                         
584                                         for ( String key1 : keyset) {
585                                                 if (key1 != "k" && key1 != "m") {
586                                                         print_std(key1);
587                                                         minmax = mapminmax.get(key1);
588                                                         defaultval = mapdefault.get(key1);
589                                                         print_std("\tmin=" + minmax[0]);
590                                                         print_std("\tmax=" + minmax[1]);
591                                                         if (defaultval != null) {
592                                                                 print_std("\tdefault=" + defaultval);
593                                                                 section.put(
594                                                                                 key1,
595                                                                                 readCommandline("\t" + key1 + "=", true));
596                                                         } else {
597                                                                 section.put(
598                                                                                 key1,
599                                                                                 readCommandline("\t" + key1 + "=",
600                                                                                                 false));
601                                                         }
602
603                                                 }
604                                 }
605
606                                         section = config.get(id + "_MetadataTemplate");
607                                         if (section == null) {
608                                                 section = config.add(id + "_MetadataTemplate");
609                                         }
610                                         key = "fragmentsize";
611                                         defaultval = 4096;
612                                         print_std(key);
613                                         print_std("\tdefault=" + defaultval);
614                                         String result = readCommandline("\t" + key + "=",true);
615                                         try {
616                                                 section.put(key, Integer.parseInt(result));
617                                         } catch (Exception e) {
618                                                 section.put(key, defaultval);
619                                         }
620                                         
621                                         key = "digest";
622                                         String defval = "none";
623                                         print_std(key+" (MD5, SHA-1, ...) ");
624                                         print_std("\tdefault=" + defval);
625                                         result = readCommandline("\t" + key + "=",true);
626                                         try {
627                                                 if (!result.isEmpty()) section.put(key, result);
628                                         } catch (Exception e) {
629                                                 //nothing
630                                         }
631                                         
632                                         println_std("");
633                                         saveconfig();
634                                         init_codec(id);
635                                 }
636                         }
637                 }
638
639         }
640
641         public String getCodecIDfromIndex(int i) {
642                 String id;
643                 try {
644                         id = codeclist.get(i);
645                 } catch (Exception e) {
646                         return null;
647                 }
648                 return id;
649         }
650
651         public void print_codecs() {
652                 println_info("----available codecs----");
653                 for (String c : codeclist) {
654                         if (codeclist_configured.contains(c)) {
655                                 println_ok(String.format("%-6s%-45s configured",
656                                                 codeclist.indexOf(c), c));
657                         } else if (codeclist_failed.contains(c)) {
658                                 println_err(String.format("%-6s%-45s invalid configuration",
659                                                 codeclist.indexOf(c), c));
660                         }
661                         else {
662                                 println_info(String.format("%-6s%-45s not configured",
663                                                 codeclist.indexOf(c), c));
664                         }
665                 }
666                 println_std("use 'init <nr>' to configure codecs and 'reset <n>' to delete configuration");
667         }
668
669         public void print_codecdescription(PluginDescription p, CodecDescription c) {
670                 println_info("\t|---------------------------------------------\n\t|"
671                                 + c.getCodecID() + "\t" + c.getCodecDescriptionText());
672                 Map<String, Integer[]> codecParameterMinMax = c
673                                 .getCodecParameterMinMax();
674                 for (Entry<String, Integer[]> e : codecParameterMinMax.entrySet()) {
675                         println_info(String.format(
676                                         "\t|\t%15s\tmin:%15s\tmax:%15s\tdefault:%15s", e.getKey(),
677                                         e.getValue()[0], e.getValue()[1], c
678                                                         .getCodecParameterDefaults().get(e.getKey())));
679                 }
680         }
681
682         private static List<Path> getFileList(Path path) {
683                 List<Path> files = new LinkedList<Path>();
684                 checkDirectory(path);
685                 try (DirectoryStream<Path> stream = Files.newDirectoryStream(path,
686                                 new DirectoryStream.Filter<Path>() {
687                                         @Override
688                                         public boolean accept(Path entry) throws IOException {
689                                                 return !Files.isDirectory(entry);
690                                         }
691                                 });) {
692                         for (Path entry : stream) {
693                                 files.add(entry);
694                         }
695                 } catch (IOException e) {
696                         e.printStackTrace();
697                 }
698                 return files;
699         }
700
701         private static void checkDirectory(Path path) {
702                 if (!Files.exists(path)) {
703                         try {
704                                 Files.createDirectory(path);
705                         } catch (IOException e) {
706                                 System.err.println("Verzeichnis kann nicht erstellt werden: "
707                                                 + path.toUri());
708                                 e.printStackTrace();
709                         }
710                 }
711         }
712
713         // private static void initSplitter(Splitter splitter) {
714         //
715         // println_std("Liste der Codecs:");
716         // Collection<String> codecids = splitter.getCodecIDs();
717         // Iterator<String> it = codecids.iterator();
718         // while (it.hasNext()){
719         // println_std("- " + it.next().toString());
720         // }
721         // if (codecids.contains("jerasureparity")){
722         // SplitterConfiguration config = new SplitterConfiguration();
723         // config.setCodecID("jerasureparity");
724         // splitter.setSplitterConfiguration(config);
725         // }
726         // println_std();
727         // }
728
729         @Override
730         public void codingFailed(CodingTask task, String reason) {
731                 synchronized (task) { // damit die Ausgabe nicht verwürfelt wird
732                         running_tasks.remove(task);
733                         println_err("Coding Failed: " + task.getFilePath().toString()
734                                         + "  Reason:" + reason);
735                 }
736
737         }
738
739         @Override
740         public synchronized void codingSuccessfull(CodingTask task) {
741                 synchronized (task) { // damit die Ausgabe nicht verwürfelt wird
742                         long duration = 0;
743                         running_tasks.remove(task);
744                         if (task instanceof EncodingTask) {
745                                 finished_encoding_task.add(task);
746                                 duration = (long) task.getMetadata("time_encode_end")
747                                                 - (long) task.getMetadata("time_encode_start");
748                         } else if (task instanceof DecodingTask) {
749                                 finished_decoding_task.add(task);
750                                 duration = (long) task.getMetadata("time_decode_end")
751                                                 - (long) task.getMetadata("time_decode_start");
752                         }
753
754                         println_ok("CodingTask " + finished_encoding_task.indexOf(task)
755                                         + " OK: " + task.getFilePath().toString() + "  Codec: "
756                                         + task.getMetadata("pluginid") + "/"
757                                         + task.getMetadata("codecid") + "  T(ms):" + duration);
758                 }
759         }
760
761         public void printUsage() {
762                 println_std("Bedienung:");
763                 println_std("init");
764                 println_std("reset");
765                 println_std("ls splitters           list all initiated splitters");
766                 println_std("ls codecs              list all available codecs");
767                 println_std("ls enc                 list all encoded Tasks");
768                 println_std("ls dec                 list all decoded Tasks");
769                 println_std("ls frag <nr>           list fragments of encoded task <nr>");
770                 println_std("rm <nr> <frac_nr>      delete fragment <frac_nr> of encoded task <nr>");
771                 println_std("dec <nr>               recover original file from encoded task <nr> into ./restoreDir");
772                 //println_std("enc <file>             encode file with all splitters");
773                 println_std("enc <file> <sp_nr>     encode file with a specific splitter");
774                 println_std("meta enc <nr>          show metada of encoded task <nr>");
775                 println_std("meta dec <nr>          show metada of decoded task <nr>");
776                 println_std("q, quit oder exit      Exit");
777                 // +" \nh oder help\tzeigt die Hilfe an"
778                 println_std("");
779         }
780
781         public void println_std(String str) {
782                 print_std(str + "\n");
783         }
784
785         public void print_std(String str) {
786                 System.out.print(ANSI_RESET + str + ANSI_RESET);
787         }
788
789         public void println_err(String str) {
790                 System.out.println(ANSI_RED + str + ANSI_RESET);
791         }
792
793         public void print_ok(String str) {
794                 System.out.print(ANSI_GREEN + str + ANSI_RESET);
795         }
796
797         public void println_ok(String str) {
798                 print_ok(str + "\n");
799         }
800
801         public void println_dbg(String str) {
802                 System.out.println(ANSI_RESET + str + ANSI_RESET);
803         }
804
805         public void println_info(String str) {
806                 System.out.println(ANSI_BOLD + ANSI_YELLOW + str + ANSI_RESET);
807         }
808 }