Skip to content

Commit

Permalink
speed fix file 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Noe GUTIERREZ committed Dec 9, 2022
1 parent 75a383a commit ec9eb5a
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/net/azib/ipscan/feeders/FileFeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.config.Version;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.util.InetAddressUtils;

import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.logging.Level.WARNING;
import static net.azib.ipscan.util.InetAddressUtils.HOSTNAME_REGEX;
Expand Down Expand Up @@ -55,25 +59,55 @@ public FileFeeder(String fileName) {
throw new FeederException("file.notExists");
}
}

// for reading 10 lines, everytime
public FileFeeder(Reader reader) {
findHosts(reader);
}

private String readMultiLine(BufferedReader fileReader) throws IOException {
int index = 1;
StringBuffer sb = new StringBuffer();
String fileLine;
while ((fileLine = fileReader.readLine()) != null) {
sb.append(fileLine);
index++;

if (index > 10) {
break;
}
}

return sb.toString();
}

private void findHosts(Reader reader) {
currentIndex = 0;
foundHosts = new LinkedHashMap<>();
Long startTime = System.currentTimeMillis();

// Import the collection flow of the local LAN port, first time , and just one time
Stream<NetworkInterface> interfaceStream = null;
try {
interfaceStream = NetworkInterface.networkInterfaces();
}
catch (SocketException e) {
e.printStackTrace();
}
System.out.println("Start to deal with the file, time used:" + startTime);
try (BufferedReader fileReader = new BufferedReader(reader)) {
String fileLine;
while ((fileLine = fileReader.readLine()) != null) {
while (!(fileLine = readMultiLine(fileReader)).equals("")) {
Long lineTime = System.currentTimeMillis();
Matcher matcher = HOSTNAME_REGEX.matcher(fileLine);
while (matcher.find()) {
try {
String host = matcher.group();
if (host.equals(Version.OWN_HOST)) continue;
ScanningSubject subject = foundHosts.get(host);
if (subject == null)
subject = new ScanningSubject(InetAddress.getByName(host));
if (subject == null){
InetAddress address = InetAddress.getByName(host);
//Call the new constructor( ScanningSubject) and pass in the LAN port obtained in advance above
subject = new ScanningSubject(address, InetAddressUtils.getInterface(address, interfaceStream));
}

if (!matcher.hitEnd() && fileLine.charAt(matcher.end()) == ':') {
// see if any valid port is requested
Expand All @@ -89,7 +123,9 @@ private void findHosts(Reader reader) {
LOG.log(WARNING, e.toString());
}
}
System.out.println("The time used for one line" + (System.currentTimeMillis() - lineTime) + "ms");
}
System.out.println("fini, total time used" + (System.currentTimeMillis() - startTime)+ "ms");
if (foundHosts.isEmpty()) {
throw new FeederException("file.nothingFound");
}
Expand Down

0 comments on commit ec9eb5a

Please sign in to comment.