[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:
parent
e16a5d65df
commit
649a447c9a
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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("</b><br>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]));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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("<b>IPHC HC-06</b><br>");
|
||||
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("<br>Contexts: sci=" + (packet.get(2) >> 4) + " dci="
|
||||
+ (packet.get(2) & 0x0f));
|
||||
verbose.append("<br>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("<br><b>IPv6 ").append(protoStr)
|
||||
.append("</b> TC = " + trafficClass
|
||||
+ " FL: " + flowLabel + "<br>");
|
||||
.append("</b> TC = ").append(trafficClass)
|
||||
.append(" FL: ").append(flowLabel)
|
||||
.append("<br>");
|
||||
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)));
|
||||
|
|
|
@ -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("<br><b>IPv6 ").append(protoStr)
|
||||
.append("</b> TC = " + trafficClass
|
||||
+ " FL: " + flowLabel + "<br>");
|
||||
.append("</b> TC = ").append(trafficClass)
|
||||
.append(" FL: ").append(flowLabel).append("<br>");
|
||||
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)));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue