diff options
| author | 2026-07-03 16:38:12 +0200 | |
|---|---|---|
| committer | 2026-07-09 09:59:28 +0200 | |
| commit | 09035b78a2aa6f8f0702d40288ef44a76ef7a6ac (patch) | |
| tree | 2c50c61a7d7efb59d35c428ea5542717be28dc98 /src/main/java/com/it_jaros/jscanner/Scanner.java | |
| parent | c0e6e508a6816fe32b517e3a4881af9da5526231 (diff) | |
Added ability to read from stdin using Streams
Replaced useless counters with already existing hasNext() methods
Diffstat (limited to 'src/main/java/com/it_jaros/jscanner/Scanner.java')
| -rw-r--r-- | src/main/java/com/it_jaros/jscanner/Scanner.java | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scanner.java b/src/main/java/com/it_jaros/jscanner/Scanner.java index 8de423c..66ceb03 100644 --- a/src/main/java/com/it_jaros/jscanner/Scanner.java +++ b/src/main/java/com/it_jaros/jscanner/Scanner.java @@ -61,19 +61,18 @@ public class Scanner implements AutoCloseable { scan.start(); CompletionService<ScanResult> completionService = new ExecutorCompletionService<>(executor); - Queue<String> hostsInQueue = new ArrayDeque<>(scan.getHosts()); - int hostsToProcess = hostsInQueue.size(); + Iterator<String> queue = scan.getHosts().iterator(); int activeHostWorkers = 0; - while ((hostsToProcess > 0 && !cancelled) || activeHostWorkers > 0) { + while ((queue.hasNext() && !cancelled) || activeHostWorkers > 0) { while ( - !hostsInQueue.isEmpty() // continue as long as we have hosts to process + queue.hasNext() // continue as long as we have hosts to process && activeHostWorkers < maxHostsLimit // limit is not reached && !cancelled // and scanner not closed ) { // here we are filling the completion service // host <-> virtual thread - final String host = hostsInQueue.poll(); + final String host = queue.next(); activeHostWorkers++; completionService.submit(() -> scanHostPorts(host, scan.getPorts(), scan)); } @@ -85,7 +84,6 @@ public class Scanner implements AutoCloseable { continue; } activeHostWorkers--; - hostsToProcess--; scan.addScanResult(finishedHost.get()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -118,8 +116,7 @@ public class Scanner implements AutoCloseable { List<Throwable> errors = new ArrayList<>(); AtomicLong portSlotFactory = new AtomicLong(System.nanoTime()); int activePerHostWorkers = 0; - int portsToProcess = portRange.getTotal(); - while ((portsToProcess > 0 && !cancelled) || activePerHostWorkers > 0) { + while ((portRange.hasNext() && !cancelled) || activePerHostWorkers > 0) { while ( portRange.hasNext() // continue as long as we have ports && activePerHostWorkers < maxWorkersPerHost // and we have not reached our limit @@ -143,7 +140,6 @@ public class Scanner implements AutoCloseable { PortResult portResult = portResultFuture.get(); activePerHostWorkers--; - portsToProcess--; progress.done().incrementAndGet(); switch (portResult.getState()) { case OPEN -> { |
