22 messages in org.postgresql.pgsql-jdbcRe: java & endianness [Re: Binary tx ...
FromSent OnAttachments
Michael GuyverJun 21, 2006 10:26 am 
Dave CramerJun 21, 2006 12:55 pm 
Mark LewisJun 21, 2006 1:31 pm 
Tom LaneJun 21, 2006 3:40 pm 
Michael GuyverJun 22, 2006 1:16 am 
Tom LaneJun 22, 2006 9:54 am 
Mark LewisJun 22, 2006 12:15 pm 
Michael GuyverJun 23, 2006 1:22 am 
Dave CramerJun 23, 2006 4:43 am 
Markus SchaberJun 23, 2006 4:59 am 
Michael GuyverJun 23, 2006 5:46 am 
Dave CramerJun 23, 2006 5:56 am 
Dave CramerJun 23, 2006 6:16 am 
Tom LaneJun 23, 2006 9:18 am 
Mark LewisJun 23, 2006 1:31 pm 
Dave CramerJun 23, 2006 1:39 pm 
Kris JurkaJun 23, 2006 1:46 pm 
Mark LewisJun 23, 2006 1:59 pm 
Marc HerbertJul 10, 2006 2:19 am 
Mark LewisJul 10, 2006 9:18 am 
Marc HerbertJul 10, 2006 10:53 am 
Mark LewisJul 10, 2006 11:59 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: java & endianness [Re: Binary tx format for an array?]Actions...
From:Mark Lewis (mark@mir3.com)
Date:Jul 10, 2006 9:18:07 am
List:org.postgresql.pgsql-jdbc

On Mon, 2006-07-10 at 11:19 +0200, Marc Herbert wrote:

Mark Lewis <mark@mir3.com> writes:

Java tried so hard to hide endianness from you that it didn't provide any real support for those times when you DO need to be aware of it.

Could you give sample cases when you DO need it? I don't think that many people write hardware drivers in Java ;-)

Any time you're working with network protocols, as was the case with this example here. Try to send a 32-bit integer over the network, for the other end to receive it correctly, it needs to be in the byte-order the client expects.

I don't think network communication qualifies either, see below.

So the "convention" looks kind of like this (snipped from the PG JDBC driver):

public void SendInteger4(int val) throws IOException { SendChar((val >> 24)&255); SendChar((val >> 16)&255); SendChar((val >> 8)&255); SendChar(val&255); }

This code is like copied/pasted from the JDK:

* @since JDK1.0 DataOutputStream#writeInt()

Any reason for duplicating it in the driver?

Well, in the general case you can only use DataOutputStream's writeInt() method if everything is in big-endian byte order, which is true in this case but not universally so. Here it IS big-endian, so the choice is between duplicating one method a few lines long, or wrapping the main OutputStream in an extra DataOutputStream which would only be used when writing big-endian integers. Not sure if one solution is better than the other.

There finally were endian-aware buffer operations added in JDK 1.4,

Could you detail which ones? Thanks in advance.

It's in the java.nio stuff, look at the javadoc for ByteBuffer.order() for a starting point.

-- Mark