Parse network packets (UDP, to be specific)

Hi,

I am working on some networking code and what puzzles me is that I have been unable to find any sensible way of parsing the payload of UDP packets. I need to get to some 32 bit integers (longs in Java), some bit values and a few 3 bit and 5 bit values in these packets.

The best I have come up with is to treat the payload as byte[] and do tons of bitshifting, masking and casting.

Is there a clever way of doing this, or a library that does it for me, that I have overlooked? Something to make this less painstaking? Ideally I'd like to define the structure, feed the packet to a parser and have it come out as something with structure on the other end. Pretty much like you do this in C.

[719 byte] By [arnekea] at [2007-11-26 16:00:55]
# 1
You might want to look at taking the array of bytes putting that in a ByteArrayInputStream and chaining that to a DataInputStream.
cotton.ma at 2007-7-8 22:22:25 > top of Java-index,Java Essentials,Java Programming...
# 2
What's the payload? Is it some standard type that someone might have created utilities for?Are you creating the packets (on the other host) using Java?
paulcwa at 2007-7-8 22:22:25 > top of Java-index,Java Essentials,Java Programming...
# 3
"32 bit integers (longs in Java)"You must have a different Java version than me, my longs are 64 bits.
-Kayaman-a at 2007-7-8 22:22:25 > top of Java-index,Java Essentials,Java Programming...