Skip to content

Commit

Permalink
Merge pull request #130 from jlai/sdk_1.3-dev
Browse files Browse the repository at this point in the history
Improve performance of SSDP message parsing
  • Loading branch information
Jeremy White committed Jul 11, 2014
2 parents bba67a0 + 4a66826 commit 6690d1b
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/com/connectsdk/core/upnp/ssdp/SSDP.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.connectsdk.core.upnp.ssdp;

import java.net.DatagramPacket;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -73,22 +74,52 @@ public static class ParsedDatagram {
public Map<String, String> data = new HashMap<String, String>();
public String type;

static Charset ASCII_CHARSET = Charset.forName("US-ASCII");

public ParsedDatagram(DatagramPacket packet) {
this.dp = packet;

Scanner s = new Scanner(new String(dp.getData()));
String text = new String(dp.getData(), ASCII_CHARSET);

int pos = 0;
int eolPos = text.indexOf("\r\n");

type = s.nextLine();
// Get first line
type = text.substring(0, eolPos);
pos = eolPos + 2;

while (s.hasNextLine()) {
String line = s.nextLine();
while (pos < text.length()) {
eolPos = text.indexOf("\r\n", pos);

if (eolPos < 0) {
break;
}

String line = text.substring(pos, eolPos);
pos = eolPos + 2;

int index = line.indexOf(':');
if (index == -1) {
continue;
}

data.put(line.substring(0, index).toUpperCase(Locale.US), line.substring(index + 1).trim());
String key = asciiUpper(line.substring(0, index));
String value = line.substring(index + 1).trim();

data.put(key, value);
}
}

// Fast toUpperCase for ASCII strings
private static String asciiUpper(String text) {
char [] chars = text.toCharArray();

for (int i = 0; i < chars.length; i++) {
char c = chars[i];
chars[i] = (c >= 97 && c <= 122) ? (char) (c - 32) : c;
}

return new String(chars);
}
}
}

0 comments on commit 6690d1b

Please sign in to comment.