I'm not sure about the DatagramPacket
class, but this certainly fixes some performance problems related to String
manipulation and LinkedList.get
. It might just be that your program runs really slowly?
StringBuilder dataBuilder = new StringBuilder();
Iterator<DatagramPacket> queueIter = queue.iterator();
while(queueIter.hasNext()) {
DatagramPacket next = queueIter.next();
System.out.println("1: This prints.");
System.out.println("2: This doesn't: " + new String(next.getData()));
int start = (new String(next.getData())).indexOf("\r\n") + "\r\n".length();
dataBuilder.append(new String(next.getData()).substring(start));
}
data = dataBuilder.toString();
What if you tried this:
public class Foo {
// instead of LinkedList<DatagramPacket>
public LinkedList<String> queue = new LinkedList<String>();
public void addPacket(DatagramPacket additional) {
queue.add(new String(additional.getData()));
}
}
}