X-Original-To: pgsql-jdbc-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 2E7BA9FB274 for ; Mon, 10 Jul 2006 13:18:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 96950-04 for ; Mon, 10 Jul 2006 13:18:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mir3-fs.mir3.com (mail.mir3.com [65.208.188.100]) by postgresql.org (Postfix) with ESMTP id 2EA499FB1BB for ; Mon, 10 Jul 2006 13:18:32 -0300 (ADT) Received: mir3-fs.mir3.com 172.16.1.11 from 172.16.2.68 172.16.2.68 via HTTP with MS-WebStorage 6.0.6249 Received: from archimedes.mirlogic.com by mir3-fs.mir3.com; 10 Jul 2006 09:18:31 -0700 Subject: Re: java & endianness [Re: Binary tx format for an array?] From: Mark Lewis To: Marc Herbert Cc: pgsql-jdbc@postgresql.org In-Reply-To: References: <30b57570606211026o66e337c9s658387db0b32aeed@mail.gmail.com> <17160.1150929667@sss.pgh.pa.us> <30b57570606220116h2f216c1eue74e5cfbb41bd5bc@mail.gmail.com> <1048.1150995309@sss.pgh.pa.us> <1151003773.21238.13.camel@archimedes> Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: MIR3, Inc. Date: Mon, 10 Jul 2006 09:18:30 -0700 Message-Id: <1152548310.30994.260.camel@archimedes> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-27) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/26 X-Sequence-Number: 15452 On Mon, 2006-07-10 at 11:19 +0200, Marc Herbert wrote: > Mark Lewis 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