summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-08-03 10:26:10 +0200
committerGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-08-03 10:48:12 +0200
commite15290e5c28581320ab66839a5c98655e66035f7 (patch)
tree418e5dcf7dcb081ad98ef56f72519d43b92fff78 /src
parent991671a8bafc6f6200cfb42d3fed9c68445217a5 (diff)
Fixed weird closing Stream logic of Scan.java
Added ScanException in case file cannot be opened
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/it_jaros/jscanner/Scan.java61
-rw-r--r--src/main/java/com/it_jaros/jscanner/ScanException.java11
2 files changed, 44 insertions, 28 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scan.java b/src/main/java/com/it_jaros/jscanner/Scan.java
index d58e8d0..985da27 100644
--- a/src/main/java/com/it_jaros/jscanner/Scan.java
+++ b/src/main/java/com/it_jaros/jscanner/Scan.java
@@ -8,31 +8,32 @@ import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
-// todo object/data asymmetry
/**
* Object holder for stateful volatile data during scan
*/
public class Scan {
+ private final Stream<String> hosts;
+ private final String ports;
+ private final Runnable onDone;
private final Counter hostCounter;
private final Counter hostTotalCounter;
private final Counter socketCounter;
private final Counter threadCounter;
- private final Stream<String> hosts;
- private final String ports;
- private final boolean closeStream;
+ private long start;
+ private long stop;
private Scan(Stream<String> hosts, String ports) {
- this(hosts, ports, false);
+ this(hosts, ports, () -> {});
}
- private Scan(Stream<String> hosts, String ports, boolean closeStream) {
+ private Scan(Stream<String> hosts, String ports, Runnable onDone) {
this.hosts = hosts;
this.ports = ports;
+ this.onDone = onDone;
this.hostCounter = new Counter();
+ this.hostTotalCounter = new Counter();
this.socketCounter = new Counter();
this.threadCounter = new Counter();
- this.hostTotalCounter = new Counter();
- this.closeStream = closeStream;
}
public static Scan create(ScanOptions options) throws IOException {
@@ -50,29 +51,30 @@ public class Scan {
* @return
* @throws IOException
*/
- public static Scan create(String sourceFile, String ports) throws IOException {
+ public static Scan create(String sourceFile, String ports) {
Stream<String> hosts;
- boolean closeStream = false;
+ if (sourceFile == null || sourceFile.isEmpty()) {
+ throw new IllegalArgumentException("Source file is null or empty");
+ }
+
if ("-".equals(sourceFile)) {
BufferedReader stdinReader = new BufferedReader(new InputStreamReader(System.in));
hosts = stdinReader.lines();
- } else {
+ return new Scan(hosts, ports);
+ }
+
+ try {
BufferedReader reader = Files.newBufferedReader(Path.of(sourceFile));
- closeStream = true;
- hosts = reader.lines().onClose(() -> {
+ return new Scan(reader.lines(), ports, () -> {
try {
reader.close();
- } catch (IOException e) {
- // ignored
+ } catch (IOException ignore) {
+ // we ignore it because at this moment the program is shutting down anyway
}
});
+ } catch (IOException e) {
+ throw new ScanException("Error while trying to open source File", e);
}
-
- return create(hosts, ports, closeStream);
- }
-
- private static Scan create(Stream<String> lines, String ports, boolean closeStream) {
- return new Scan(lines, ports, closeStream);
}
public static Scan create(List<String> hostsArgv, String ports) {
@@ -87,14 +89,9 @@ public class Scan {
return ports;
}
- public void start() {
- // start
- }
-
public void stop() {
- if (closeStream) {
- hosts.close(); // close stream
- }
+ stop = System.currentTimeMillis();
+ onDone.run();
}
public Counter getThreadCounter() {
@@ -112,4 +109,12 @@ public class Scan {
public Counter getHostTotalCounter() {
return hostTotalCounter;
}
+
+ public void start() {
+ start = System.currentTimeMillis();
+ }
+
+ public long getDurationMillis() {
+ return stop - start;
+ }
}
diff --git a/src/main/java/com/it_jaros/jscanner/ScanException.java b/src/main/java/com/it_jaros/jscanner/ScanException.java
new file mode 100644
index 0000000..3b2cf24
--- /dev/null
+++ b/src/main/java/com/it_jaros/jscanner/ScanException.java
@@ -0,0 +1,11 @@
+package com.it_jaros.jscanner;
+
+public class ScanException extends java.lang.RuntimeException {
+ public ScanException(String message) {
+ super(message);
+ }
+
+ public ScanException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}