blob: cdef5cf81940d79b0d6c6ee6c9fb2674d23d7d06 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package com.it_jaros.networkScanner;
import java.util.BitSet;
import java.util.NoSuchElementException;
public class PortRange {
private static final int MIN_PORT = 1;
private static final int MAX_PORT = 65535;
private final BitSet portsRange = new BitSet(MAX_PORT);
private final BitSet available = new BitSet(MAX_PORT);
private int cursor;
private int done = 0;
private int total = 0;
public PortRange(String ports) {
if (ports == null || "".equals(ports)) {
// keep defaults
portsRange.set(1, 1024 + 1);
return;
}
String[] splitComma = ports.split(",");
for (String commaValue: splitComma) {
if (commaValue.contains("-")) {
String[] splitRange = commaValue.split("-");
if (splitRange.length > 2) {
throw new IllegalArgumentException(String.format("Argument contains too many '-'' %s", commaValue));
}
int start = Integer.parseInt(splitRange[0]);
int end = Integer.parseInt(splitRange[1]);
portsRange.set(start, end + 1);
} else {
portsRange.set(Integer.parseInt(commaValue));
}
}
available.or(portsRange);
cursor = available.nextSetBit(MIN_PORT);
}
public synchronized int next() {
int p = available.nextSetBit(cursor);
if (p > 0) {
available.clear(p);
cursor = p + 1;
done++;
return p;
}
throw new NoSuchElementException("Reached end of port range");
}
public synchronized boolean hasNext() {
return available.nextSetBit(cursor) > 0;
}
public int getTotal() {
return portsRange.cardinality();
}
public int getDone() {
return done;
}
}
|