From 649a447c9aafc0542ecef1c23e39aecc1ccc029f Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 11 Jul 2014 02:33:57 +0200 Subject: [PATCH] [cooja] plugins/analyzers: Replace StringBuffer with lightweight StringBuilder StringBuffer is a thread-safe version of StringBuilder that introduces unnecessary overhead. Additional replaced concatenations with .append() --- .../contikios/cooja/plugins/RadioLogger.java | 6 ++-- .../plugins/analyzers/ICMPv6Analyzer.java | 14 ++++---- .../plugins/analyzers/IEEE802154Analyzer.java | 32 +++++++++++++------ .../plugins/analyzers/IPHCPacketAnalyzer.java | 27 ++++++++++------ .../plugins/analyzers/IPv6PacketAnalyzer.java | 10 +++--- .../plugins/analyzers/PacketAnalyzer.java | 2 +- 6 files changed, 56 insertions(+), 35 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java b/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java index 70eba23bb..546b66566 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java @@ -685,8 +685,8 @@ public class RadioLogger extends VisPlugin { return; } - StringBuffer brief = new StringBuffer(); - StringBuffer verbose = new StringBuffer(); + StringBuilder brief = new StringBuilder(); + StringBuilder verbose = new StringBuilder(); /* default analyzer */ PacketAnalyzer.Packet packet = new PacketAnalyzer.Packet(data, PacketAnalyzer.MAC_LEVEL); @@ -713,7 +713,7 @@ public class RadioLogger extends VisPlugin { } } - private boolean analyzePacket(PacketAnalyzer.Packet packet, StringBuffer brief, StringBuffer verbose) { + private boolean analyzePacket(PacketAnalyzer.Packet packet, StringBuilder brief, StringBuilder verbose) { if (analyzers == null) return false; try { boolean analyze = true; diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/ICMPv6Analyzer.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/ICMPv6Analyzer.java index 7b1d03fd0..e5b9f3681 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/ICMPv6Analyzer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/ICMPv6Analyzer.java @@ -39,8 +39,8 @@ public class ICMPv6Analyzer extends PacketAnalyzer { "ROUTER RENUMBER", "NODE INFORMATION QUERY", "NODE INFORMATION RESPONSE"}; @Override - public int analyzePacket(Packet packet, StringBuffer brief, - StringBuffer verbose) { + public int analyzePacket(Packet packet, StringBuilder brief, + StringBuilder verbose) { int type = packet.get(0) & 0xff; int code = packet.get(1) & 0xff; // int checksum = ((packet.get(2) & 0xff) << 8) | packet.get(3) & 0xff; @@ -69,11 +69,11 @@ public class ICMPv6Analyzer extends PacketAnalyzer { int mop = (packet.get(8) >> 3) & 0x07; int dtsn = packet.get(9); - verbose.append(" InstanceID: " + instanceID - + " Version: " + version - + " Rank:" + rank - + " MOP: " + mop - + " DTSN: " + dtsn); + verbose.append(" InstanceID: ").append(instanceID) + .append(", Version: ").append(version) + .append(", Rank: ").append(rank) + .append(", MOP: ").append(mop) + .append(", DTSN: ").append(dtsn); packet.consumeBytesStart(8); break; diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java index ce94e8e14..68049c367 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java @@ -61,7 +61,7 @@ public class IEEE802154Analyzer extends PacketAnalyzer { */ @Override - public int analyzePacket(Packet packet, StringBuffer brief, StringBuffer verbose) { + public int analyzePacket(Packet packet, StringBuilder brief, StringBuilder verbose) { if (pcapExporter != null) { try { @@ -141,14 +141,16 @@ public class IEEE802154Analyzer extends PacketAnalyzer { verbose.append("
From "); if (srcPanID != 0) { - verbose.append(StringUtils.toHex((byte) (srcPanID >> 8))) + verbose.append("0x") + .append(StringUtils.toHex((byte) (srcPanID >> 8))) .append(StringUtils.toHex((byte) (srcPanID & 0xff))) .append('/'); } printAddress(verbose, srcAddrMode, sourceAddress); verbose.append(" to "); if (destPanID != 0) { - verbose.append(StringUtils.toHex((byte) (destPanID >> 8))) + verbose.append("0x") + .append(StringUtils.toHex((byte) (destPanID >> 8))) .append(StringUtils.toHex((byte) (destPanID & 0xff))) .append('/'); } @@ -169,14 +171,26 @@ public class IEEE802154Analyzer extends PacketAnalyzer { return ANALYSIS_OK_CONTINUE; } - private void printAddress(StringBuffer sb, int type, byte[] addr) { + private void printAddress(StringBuilder sb, int type, byte[] addr) { if (type == SHORT_ADDRESS) { - sb.append(StringUtils.toHex(addr)); + sb.append("0x").append(StringUtils.toHex(addr)); } else if (type == LONG_ADDRESS) { - sb.append(StringUtils.toHex(addr[0]) + StringUtils.toHex(addr[1]) + ":" - + StringUtils.toHex(addr[2]) + StringUtils.toHex(addr[3]) + ":" - + StringUtils.toHex(addr[4]) + StringUtils.toHex(addr[5]) + ":" - + StringUtils.toHex(addr[6]) + StringUtils.toHex(addr[7])); + sb.append(StringUtils.toHex(addr[0])) + .append(':') + .append(StringUtils.toHex(addr[1])) + .append(':') + .append(StringUtils.toHex(addr[2])) + .append(':') + .append(StringUtils.toHex(addr[3])) + .append(':') + .append(StringUtils.toHex(addr[4])) + .append(':') + .append(StringUtils.toHex(addr[5])) + .append(':') + .append(StringUtils.toHex(addr[6])) + .append(':') + .append(StringUtils.toHex(addr[7])); + } } } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPHCPacketAnalyzer.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPHCPacketAnalyzer.java index 7cbb97935..2a70fea6f 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPHCPacketAnalyzer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPHCPacketAnalyzer.java @@ -67,8 +67,8 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer { } @Override - public int analyzePacket(Packet packet, StringBuffer brief, - StringBuffer verbose) { + public int analyzePacket(Packet packet, StringBuilder brief, + StringBuilder verbose) { /* if packet has less than 3 bytes it is not interesting ... */ if (packet.size() < 3) return ANALYSIS_FAILED; @@ -91,12 +91,18 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer { /* need to decompress while analyzing - add that later... */ verbose.append("IPHC HC-06
"); - verbose.append("tf = " + tf + " nhc = " + nhc + " hlim = " + hlim - + " cid = " + cid + " sac = " + sac + " sam = " + sam - + " MCast = " + m + " dac = " + dac + " dam = " + dam); + verbose.append("TF = ").append(tf) + .append(", NH = ").append(nhc) + .append(", HLIM = ").append(hlim) + .append(", CID = ").append(cid) + .append(", SAC = ").append(sac) + .append(", SAM = ").append(sam) + .append(", MCast = ").append(m) + .append(", DAC = ").append(dac) + .append(", DAM = ").append(dam); if (cid == 1) { - verbose.append("
Contexts: sci=" + (packet.get(2) >> 4) + " dci=" - + (packet.get(2) & 0x0f)); + verbose.append("
Contexts: sci=").append(packet.get(2) >> 4). + append(" dci=").append(packet.get(2) & 0x0f); sci = packet.get(2) >> 4; dci = packet.get(2) & 0x0f; } @@ -412,8 +418,9 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer { } verbose.append("
IPv6 ").append(protoStr) - .append(" TC = " + trafficClass - + " FL: " + flowLabel + "
"); + .append(" TC = ").append(trafficClass) + .append(" FL: ").append(flowLabel) + .append("
"); verbose.append("From "); printAddress(verbose, srcAddress); verbose.append(" to "); @@ -431,7 +438,7 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer { } } - public static void printAddress(StringBuffer out, byte[] address) { + public static void printAddress(StringBuilder out, byte[] address) { for (int i = 0; i < 16; i += 2) { out.append(StringUtils.toHex((byte) (address[i] & 0xff)) + StringUtils.toHex((byte) (address[i + 1] & 0xff))); diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPv6PacketAnalyzer.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPv6PacketAnalyzer.java index 0dc34896e..0d33b0bd3 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPv6PacketAnalyzer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IPv6PacketAnalyzer.java @@ -19,8 +19,8 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer { } @Override - public int analyzePacket(Packet packet, StringBuffer brief, - StringBuffer verbose) { + public int analyzePacket(Packet packet, StringBuilder brief, + StringBuilder verbose) { /* if packet has less than 40 bytes it is not interesting ... */ if (packet.size() < 40) return ANALYSIS_FAILED; @@ -57,8 +57,8 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer { packet.pos += 41; verbose.append("
IPv6 ").append(protoStr) - .append(" TC = " + trafficClass - + " FL: " + flowLabel + "
"); + .append(" TC = ").append(trafficClass) + .append(" FL: ").append(flowLabel).append("
"); verbose.append("From "); printAddress(verbose, srcAddress); verbose.append(" to "); @@ -69,7 +69,7 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer { return ANALYSIS_OK_CONTINUE; } - public static void printAddress(StringBuffer out, byte[] address) { + public static void printAddress(StringBuilder out, byte[] address) { for (int i = 0; i < 16; i += 2) { out.append(StringUtils.toHex((byte) (address[i] & 0xff)) + StringUtils.toHex((byte) (address[i + 1] & 0xff))); diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PacketAnalyzer.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PacketAnalyzer.java index 73549cce3..3be8a8284 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PacketAnalyzer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PacketAnalyzer.java @@ -83,5 +83,5 @@ public abstract class PacketAnalyzer { public abstract boolean matchPacket(Packet packet); - public abstract int analyzePacket(Packet packet, StringBuffer brief, StringBuffer verbose); + public abstract int analyzePacket(Packet packet, StringBuilder brief, StringBuilder verbose); }