From 6b9a6f3cd97e84ec887fcb4509cbcc76b0c71933 Mon Sep 17 00:00:00 2001 From: Wolfgang Nagele Date: Sun, 3 May 2015 16:00:01 +1000 Subject: [PATCH] Fix micro second timestamp --- .../src/main/java/net/ripe/hadoop/pcap/PcapReader.java | 10 ++++------ .../main/java/net/ripe/hadoop/pcap/packet/Packet.java | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/PcapReader.java b/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/PcapReader.java index a249348..dc8e3ab 100644 --- a/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/PcapReader.java +++ b/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/PcapReader.java @@ -74,8 +74,8 @@ public class PcapReader implements Iterable { private LinkType linkType; private long snapLen; private boolean caughtEOF = false; - // MathContext for BigDecimal to preserve only 16 decimal digits - private MathContext ts_mc = new MathContext(16); + + private MathContext tsUsecMc = new MathContext(16); //To read reversed-endian PCAPs; the header is the only part that switches private boolean reverseHeaderByteOrder = false; @@ -170,10 +170,8 @@ private Packet nextPacket() { long packetTimestampMicros = PcapReaderUtil.convertInt(pcapPacketHeader, TIMESTAMP_MICROS_OFFSET, reverseHeaderByteOrder); packet.put(Packet.TIMESTAMP_MICROS, packetTimestampMicros); - // Prepare the timestamp with a BigDecimal to include microseconds - BigDecimal packetTimestampUsec = new BigDecimal(packetTimestamp - + (double) packetTimestampMicros/1000000, ts_mc); - packet.put(Packet.TS_USEC, packetTimestampUsec.longValue()); + BigDecimal packetTimestampUsec = new BigDecimal(packetTimestamp + packetTimestampMicros / 1000000.0, tsUsecMc); + packet.put(Packet.TIMESTAMP_USEC, packetTimestampUsec.doubleValue()); long packetSize = PcapReaderUtil.convertInt(pcapPacketHeader, CAP_LEN_OFFSET, reverseHeaderByteOrder); packetData = new byte[(int)packetSize]; diff --git a/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/packet/Packet.java b/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/packet/Packet.java index fffa2ab..9df203d 100644 --- a/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/packet/Packet.java +++ b/hadoop-pcap-lib/src/main/java/net/ripe/hadoop/pcap/packet/Packet.java @@ -7,8 +7,8 @@ public class Packet extends HashMap { private static final long serialVersionUID = 8723206921174160146L; public static final String TIMESTAMP = "ts"; - public static final String TIMESTAMP_MICROS = "tsmicros"; - public static final String TS_USEC = "ts_usec"; + public static final String TIMESTAMP_USEC = "ts_usec"; + public static final String TIMESTAMP_MICROS = "ts_micros"; public static final String TTL = "ttl"; public static final String IP_VERSION = "ip_version"; public static final String IP_HEADER_LENGTH = "ip_header_length";