Question

I am currently trying to perform some regex on the result of a DatagramPacket.getData() call.

Implemented as String myString = new String(thepkt.getData()):

But weirdly, java is dropping the end quotation that it uses to encapsulate all data(see linked image below).

When I click the field in the variable inspector during a debug session and don't change anything, when I click off the variable field it corrects itself again without me changing anything. It even highlights the variable inspection field in yellow to signal change. Its values are also displaying like it is still a byte array rather than a String object

http://i.imgur.com/8ZItsZI.png

It's throwing off my regex and I can't see anything that would cause it. It's a client server simulation and on the client side, the getData returns the data no problem.

Was it helpful?

Solution

I got it working by using the solution provided in:

https://stackoverflow.com/a/8557165/1700855

But I still don't understand how not specifying the length of the packet to the String constructor would cause it to drop the systematic end double quotes. Can anyone provide an explanation as I really like to understand solutions to my issues before moving on :)

OTHER TIPS

The problem is that you didn't read the spec for DatagramPacket.getData:

Returns the data buffer. The data received or the data to be sent starts from the offset in the buffer, and runs for length long.

So, to be correct, you should use

new String(thepkt.getData(), thepkt.getOffset(), thepht.getLength())

Or, to not use the default charset:

new String(thepkt.getData(), thepkt.getOffset(), thepht.getLength(), someCharset)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top