pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
java & endianness [Re: Binary tx format for an array?]
4+ messages / 2 participants
[nested] [flat]

* java & endianness [Re: Binary tx format for an array?]
@ 2006-07-10 09:19  Marc Herbert <Marc.Herbert@continuent.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Marc Herbert @ 2006-07-10 09:19 UTC (permalink / raw)
  To: pgsql-jdbc

Mark Lewis <mark.lewis@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 ;-)

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?



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

Could you detail which ones? Thanks in advance.





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: java & endianness [Re: Binary tx format for an array?]
@ 2006-07-10 16:18  Mark Lewis <mark.lewis@mir3.com>
  parent: Marc Herbert <Marc.Herbert@continuent.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Mark Lewis @ 2006-07-10 16:18 UTC (permalink / raw)
  To: Marc Herbert <Marc.Herbert@continuent.com>; +Cc: pgsql-jdbc

On Mon, 2006-07-10 at 11:19 +0200, Marc Herbert wrote:
> Mark Lewis <mark.lewis@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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: java & endianness [Re: Binary tx format for an array?]
@ 2006-07-10 17:54  Marc Herbert <Marc.Herbert@continuent.com>
  parent: Mark Lewis <mark.lewis@mir3.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Marc Herbert @ 2006-07-10 17:54 UTC (permalink / raw)
  To: pgsql-jdbc

Mark Lewis <mark.lewis@mir3.com> writes:

>> >     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.

My point is that it IS universally true in the Java universe AND in
the networking universe. There simply cannot be two opposed networking
universes... that would mean we'd have two Internets for instance?

So I think the whole point of Java is that everything is big-endian, so
you do not need to know about endianness anymore. IMHO this is a
success.


> 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.

OK you can bypass/copy-paste some JDK code for performance
reasons. But then you can not honestly complain Java does a poor job
of hiding endianness when you fiddled with the machine internals
instead of safely staying outside.

Moreover you don't really need to think about endianness when simply
copy/pasting this code.


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

Interesting. Looks like you need this only when dealing with
badly-behaved applications that do not use the network order, right?
Should be very seldom, don't you think?


Thanks for answering.







^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: java & endianness [Re: Binary tx format for an array?]
@ 2006-07-10 18:59  Mark Lewis <mark.lewis@mir3.com>
  parent: Marc Herbert <Marc.Herbert@continuent.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Mark Lewis @ 2006-07-10 18:59 UTC (permalink / raw)
  To: Marc Herbert <Marc.Herbert@continuent.com>; +Cc: pgsql-jdbc

On Mon, 2006-07-10 at 19:54 +0200, Marc Herbert wrote:
> My point is that it IS universally true in the Java universe AND in
> the networking universe. There simply cannot be two opposed networking
> universes... that would mean we'd have two Internets for instance?
> 
> So I think the whole point of Java is that everything is big-endian, so
> you do not need to know about endianness anymore. IMHO this is a
> success.

Just because Java is big-endian and this particular protocol is big-
endian does not mean that the universe is big-endian.  There are
protocols which either always use little-endian, or allow the endian-
ness of parameters to be negotiated for performance.  One example is the
TDS protocol, so you may want to look at the open source jTDS JDBC
driver and see how they handle it.

Your point is however well taken that there are more big-endian
protocols out there than little-endian protocols, and Java works easily
with those.  

> Interesting. Looks like you need this only when dealing with
> badly-behaved applications that do not use the network order, right?
> Should be very seldom, don't you think?

It's not badly-behaving applications, it's protocols whose designers
chose to use little-endian formatting, usually for performance reasons
on common x86 hardware.

-- Mark



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2006-07-10 18:59 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-10 09:19 java & endianness [Re: Binary tx format for an array?] Marc Herbert <Marc.Herbert@continuent.com>
2006-07-10 16:18 ` Mark Lewis <mark.lewis@mir3.com>
2006-07-10 17:54   ` Marc Herbert <Marc.Herbert@continuent.com>
2006-07-10 18:59     ` Mark Lewis <mark.lewis@mir3.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox