summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/it_jaros/jscanner/Scan.java18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scan.java b/src/main/java/com/it_jaros/jscanner/Scan.java
index e0fec32..d276fd2 100644
--- a/src/main/java/com/it_jaros/jscanner/Scan.java
+++ b/src/main/java/com/it_jaros/jscanner/Scan.java
@@ -18,14 +18,20 @@ public class Scan {
private final Counter threadCounter;
private final Stream<String> hosts;
private final String ports;
+ private final boolean closeStream;
private Scan(Stream<String> hosts, String ports) {
+ this(hosts, ports, false);
+ }
+
+ private Scan(Stream<String> hosts, String ports, boolean closeStream) {
this.hosts = hosts;
this.ports = ports;
this.hostCounter = 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 {
@@ -45,11 +51,13 @@ public class Scan {
*/
public static Scan create(String sourceFile, String ports) throws IOException {
Stream<String> hosts;
+ boolean closeStream = false;
if ("-".equals(sourceFile)) {
BufferedReader stdinReader = new BufferedReader(new InputStreamReader(System.in));
hosts = stdinReader.lines();
} else {
BufferedReader reader = Files.newBufferedReader(Path.of(sourceFile));
+ closeStream = true;
hosts = reader.lines().onClose(() -> {
try {
reader.close();
@@ -59,11 +67,11 @@ public class Scan {
});
}
- return create(hosts, ports);
+ return create(hosts, ports, closeStream);
}
- private static Scan create(Stream<String> lines, String ports) {
- return new Scan(lines, ports);
+ 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) {
@@ -83,7 +91,9 @@ public class Scan {
}
public void stop() {
- hosts.close(); // close stream
+ if (closeStream) {
+ hosts.close(); // close stream
+ }
}
public Counter getThreadCounter() {