[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()
This commit is contained in:
Enrico Joerns 2014-07-11 02:33:57 +02:00
parent e16a5d65df
commit 649a447c9a
6 changed files with 56 additions and 35 deletions

View file

@ -685,8 +685,8 @@ public class RadioLogger extends VisPlugin {
return; return;
} }
StringBuffer brief = new StringBuffer(); StringBuilder brief = new StringBuilder();
StringBuffer verbose = new StringBuffer(); StringBuilder verbose = new StringBuilder();
/* default analyzer */ /* default analyzer */
PacketAnalyzer.Packet packet = new PacketAnalyzer.Packet(data, PacketAnalyzer.MAC_LEVEL); 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; if (analyzers == null) return false;
try { try {
boolean analyze = true; boolean analyze = true;

View file

@ -39,8 +39,8 @@ public class ICMPv6Analyzer extends PacketAnalyzer {
"ROUTER RENUMBER", "NODE INFORMATION QUERY", "NODE INFORMATION RESPONSE"}; "ROUTER RENUMBER", "NODE INFORMATION QUERY", "NODE INFORMATION RESPONSE"};
@Override @Override
public int analyzePacket(Packet packet, StringBuffer brief, public int analyzePacket(Packet packet, StringBuilder brief,
StringBuffer verbose) { StringBuilder verbose) {
int type = packet.get(0) & 0xff; int type = packet.get(0) & 0xff;
int code = packet.get(1) & 0xff; int code = packet.get(1) & 0xff;
// int checksum = ((packet.get(2) & 0xff) << 8) | packet.get(3) & 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 mop = (packet.get(8) >> 3) & 0x07;
int dtsn = packet.get(9); int dtsn = packet.get(9);
verbose.append(" InstanceID: " + instanceID verbose.append(" InstanceID: ").append(instanceID)
+ " Version: " + version .append(", Version: ").append(version)
+ " Rank:" + rank .append(", Rank: ").append(rank)
+ " MOP: " + mop .append(", MOP: ").append(mop)
+ " DTSN: " + dtsn); .append(", DTSN: ").append(dtsn);
packet.consumeBytesStart(8); packet.consumeBytesStart(8);
break; break;

View file

@ -61,7 +61,7 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
*/ */
@Override @Override
public int analyzePacket(Packet packet, StringBuffer brief, StringBuffer verbose) { public int analyzePacket(Packet packet, StringBuilder brief, StringBuilder verbose) {
if (pcapExporter != null) { if (pcapExporter != null) {
try { try {
@ -141,14 +141,16 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
verbose.append("</b><br>From "); verbose.append("</b><br>From ");
if (srcPanID != 0) { 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(StringUtils.toHex((byte) (srcPanID & 0xff)))
.append('/'); .append('/');
} }
printAddress(verbose, srcAddrMode, sourceAddress); printAddress(verbose, srcAddrMode, sourceAddress);
verbose.append(" to "); verbose.append(" to ");
if (destPanID != 0) { 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(StringUtils.toHex((byte) (destPanID & 0xff)))
.append('/'); .append('/');
} }
@ -169,14 +171,26 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
return ANALYSIS_OK_CONTINUE; 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) { if (type == SHORT_ADDRESS) {
sb.append(StringUtils.toHex(addr)); sb.append("0x").append(StringUtils.toHex(addr));
} else if (type == LONG_ADDRESS) { } else if (type == LONG_ADDRESS) {
sb.append(StringUtils.toHex(addr[0]) + StringUtils.toHex(addr[1]) + ":" sb.append(StringUtils.toHex(addr[0]))
+ StringUtils.toHex(addr[2]) + StringUtils.toHex(addr[3]) + ":" .append(':')
+ StringUtils.toHex(addr[4]) + StringUtils.toHex(addr[5]) + ":" .append(StringUtils.toHex(addr[1]))
+ StringUtils.toHex(addr[6]) + StringUtils.toHex(addr[7])); .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]));
} }
} }
} }

View file

@ -67,8 +67,8 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer {
} }
@Override @Override
public int analyzePacket(Packet packet, StringBuffer brief, public int analyzePacket(Packet packet, StringBuilder brief,
StringBuffer verbose) { StringBuilder verbose) {
/* if packet has less than 3 bytes it is not interesting ... */ /* if packet has less than 3 bytes it is not interesting ... */
if (packet.size() < 3) return ANALYSIS_FAILED; if (packet.size() < 3) return ANALYSIS_FAILED;
@ -91,12 +91,18 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer {
/* need to decompress while analyzing - add that later... */ /* need to decompress while analyzing - add that later... */
verbose.append("<b>IPHC HC-06</b><br>"); verbose.append("<b>IPHC HC-06</b><br>");
verbose.append("tf = " + tf + " nhc = " + nhc + " hlim = " + hlim verbose.append("TF = ").append(tf)
+ " cid = " + cid + " sac = " + sac + " sam = " + sam .append(", NH = ").append(nhc)
+ " MCast = " + m + " dac = " + dac + " dam = " + dam); .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) { if (cid == 1) {
verbose.append("<br>Contexts: sci=" + (packet.get(2) >> 4) + " dci=" verbose.append("<br>Contexts: sci=").append(packet.get(2) >> 4).
+ (packet.get(2) & 0x0f)); append(" dci=").append(packet.get(2) & 0x0f);
sci = packet.get(2) >> 4; sci = packet.get(2) >> 4;
dci = packet.get(2) & 0x0f; dci = packet.get(2) & 0x0f;
} }
@ -412,8 +418,9 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer {
} }
verbose.append("<br><b>IPv6 ").append(protoStr) verbose.append("<br><b>IPv6 ").append(protoStr)
.append("</b> TC = " + trafficClass .append("</b> TC = ").append(trafficClass)
+ " FL: " + flowLabel + "<br>"); .append(" FL: ").append(flowLabel)
.append("<br>");
verbose.append("From "); verbose.append("From ");
printAddress(verbose, srcAddress); printAddress(verbose, srcAddress);
verbose.append(" to "); 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) { for (int i = 0; i < 16; i += 2) {
out.append(StringUtils.toHex((byte) (address[i] & 0xff)) out.append(StringUtils.toHex((byte) (address[i] & 0xff))
+ StringUtils.toHex((byte) (address[i + 1] & 0xff))); + StringUtils.toHex((byte) (address[i + 1] & 0xff)));

View file

@ -19,8 +19,8 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer {
} }
@Override @Override
public int analyzePacket(Packet packet, StringBuffer brief, public int analyzePacket(Packet packet, StringBuilder brief,
StringBuffer verbose) { StringBuilder verbose) {
/* if packet has less than 40 bytes it is not interesting ... */ /* if packet has less than 40 bytes it is not interesting ... */
if (packet.size() < 40) return ANALYSIS_FAILED; if (packet.size() < 40) return ANALYSIS_FAILED;
@ -57,8 +57,8 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer {
packet.pos += 41; packet.pos += 41;
verbose.append("<br><b>IPv6 ").append(protoStr) verbose.append("<br><b>IPv6 ").append(protoStr)
.append("</b> TC = " + trafficClass .append("</b> TC = ").append(trafficClass)
+ " FL: " + flowLabel + "<br>"); .append(" FL: ").append(flowLabel).append("<br>");
verbose.append("From "); verbose.append("From ");
printAddress(verbose, srcAddress); printAddress(verbose, srcAddress);
verbose.append(" to "); verbose.append(" to ");
@ -69,7 +69,7 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer {
return ANALYSIS_OK_CONTINUE; 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) { for (int i = 0; i < 16; i += 2) {
out.append(StringUtils.toHex((byte) (address[i] & 0xff)) out.append(StringUtils.toHex((byte) (address[i] & 0xff))
+ StringUtils.toHex((byte) (address[i + 1] & 0xff))); + StringUtils.toHex((byte) (address[i + 1] & 0xff)));

View file

@ -83,5 +83,5 @@ public abstract class PacketAnalyzer {
public abstract boolean matchPacket(Packet packet); 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);
} }