File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes DatagramPacket.getData() weirdness Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DatagramPacket.getData() weirdness" Watch "DatagramPacket.getData() weirdness" New topic

DatagramPacket.getData() weirdness

Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
While working with a Datagram-based application, I've noticed what looks like a quirk in the getData() method for DatagramPackets. I'd been having all kinds of troubles comparing a byte array I'd created from a String to the byte array coming from an inbound DatagramPacket, where I knew exactly what the DatagramPacket should contain. When I looked a little closer, I found that although the printed Strings seemed equal, the byte array coming in was actually one byte longer, containing an odd little character at the end.
Basically, when I looked at this character -- either as System.out.println(array[9]), Byte.toString(), or Byte.intValue() -- it showed up as a "0". I figured I could solve my comparison problem by adding a "0" to the end of my home-grown String; when that didn't do it, I figured it was a null character, or "\0", and tried that. Neither worked.
Just to confound the issue further, when a String is created from the byte array generated from DatagramPacket.getData(), this last character doesn't print.
Any ideas what this character might be, and why it's there? Is this a problem with getData(), or something going on with the program sending me my DatagramPacket?
Alex Kirk
I agree. Here's the link:
subject: DatagramPacket.getData() weirdness
It's not a secret anymore!