public Packet getNextPacket() throws NotOpenException {
if (!open) {
throw new NotOpenException();
}
pcap_pkthdr header = new pcap_pkthdr();
header.setAutoSynch(false);
Pointer packet;
if (!handleLock.readLock().tryLock()) {
throw new NotOpenException();
}
try {
if (!open) {
throw new NotOpenException();
}
packet = NativeMappings.pcap_next(handle, header);
} finally {
handleLock.readLock().unlock();
}
if (packet != null) {
Pointer headerP = header.getPointer();
timestampsInts.set(pcap_pkthdr.getTvSec(headerP).longValue());
timestampsMicros.set(pcap_pkthdr.getTvUsec(headerP).intValue());
byte[] ba = packet.getByteArray(0, pcap_pkthdr.getCaplen(headerP));
return PacketFactories.getFactory(Packet.class, DataLinkType.class)
.newInstance(ba, 0, ba.length, dlt);