Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java and Binary Packets, Network Byte Order

Status
Not open for further replies.

CTIEng2004

Technical User
Feb 18, 2004
19
0
0
US
Coming from a C/C++ background, I am still wondering why Java doesn't provide network byte order conversion routines like htonl(), nthol(), htons(), ntohs() etc. Seems like most Java apps I see use ASCII streams. But what if you have to interface w/ a legacy app that uses variable length packets w/ shorts and integers? Now you see my problem.

Writing these functions yourself is doable, but since you have to take into account the processor achitecture (big-endian versus little endian) and the fact they are used so heavily in potential real-time or near real-time scenarios, such routines can actually be fairly tricky and become inefficient.

I am wondering if someone has a very efficient Java routine or set of Java routines that perform network byte order and host byte order conversions?

Thanks

mike_luster@hotmail.com
 
Are you talking of
Code:
java.nio.ByteOrder.LITTLE_ENDIAN && java.nio.ByteOrder.LITTLE_ENDIAN

I don't know
Code:
htonl(), nthol(), htons(), ntohs()
but I if they're widely used, I would expect them to be findable in Java.
Java.nio (which means: new IO) was introduced in java-1.4.

file:///$JAVA_HOME/docs/api/java/nio/package-summary.html
 
Like Stefan Wagner said, you'll have to take a look at java.nio...
e.g. ByteBuffer --> getShort(), getInt(), getLong(), ...
... the bytes will be marshaled to or from the requested primitive data type according to the current byte-order setting in effect for the buffer.
ByteBuffer has a method order(...) to set the ByteOrder
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top