From 09035b78a2aa6f8f0702d40288ef44a76ef7a6ac Mon Sep 17 00:00:00 2001 From: Matthias Jaros Date: Fri, 3 Jul 2026 16:38:12 +0200 Subject: Added ability to read from stdin using Streams Replaced useless counters with already existing hasNext() methods --- src/main/java/com/it_jaros/jscanner/Scanner.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/main/java/com/it_jaros/jscanner/Scanner.java') 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 completionService = new ExecutorCompletionService<>(executor); - Queue hostsInQueue = new ArrayDeque<>(scan.getHosts()); - int hostsToProcess = hostsInQueue.size(); + Iterator 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 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 -> { -- cgit v1.3.1