blob: 66909572261fcbeff5a416b315db50fe896a76c7 (
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
|
package com.it_jaros.jscanner;
import java.util.BitSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static com.it_jaros.jscanner.PortRange.MAX_PORT;
import static com.it_jaros.jscanner.PortRange.MIN_PORT;
public class PortRangeIterator implements Iterator<Integer> {
private int currentPortCursor;
private int done = 0;
private final BitSet availablePorts = new BitSet(MAX_PORT);
PortRangeIterator(BitSet specifiedPorts) {
availablePorts.or(specifiedPorts);
currentPortCursor = availablePorts.nextSetBit(MIN_PORT);
}
@Override
public Integer next() {
int p = availablePorts.nextSetBit(currentPortCursor);
if (p > 0) {
availablePorts.clear(p);
currentPortCursor = p + 1;
done++;
return p;
}
throw new NoSuchElementException("Reached end of port range");
}
@Override
public boolean hasNext() {
return availablePorts.nextSetBit(currentPortCursor) > 0;
}
}
|