Skip site navigation (1) Skip section navigation (2)

Blobs and length +- 1

From: Philip Crotwell <crotwell(at)seis(dot)sc(dot)edu>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Blobs and length +- 1
Date: 2001-03-01 14:31:22
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc

I am using Blobs with jdbc and postgres7.0.3 and noticed that my blobs
were coming out of the database 1 byte shorter than they went in! The
problem was that I was using 
byte[] b = blob.getBytes(1, (int)blob.length());
instead of 
byte[] b = blob.getBytes(0, (int)blob.length());

But the javadocs for java.sql.Blob says, that the position is 1 based:

Returns as an array of bytes part or all of the BLOB value that this Blob
object designates. The byte array contains up to length consecutive bytes
starting at position pos.
         pos - the ordinal position of the first byte in the BLOB value to
be extracted; the first byte is at position 1
         length - is the number of consecutive bytes to be copied

It seems to me that this says that getBytes(1, length) should be the
entire blob instead of all but the first byte.
Is this a postgres jdbc bug, or am I misreading the javadocs?

The fix would be to subtract one, ie:
    public byte[] getBytes(long pos,int length) throws SQLException {,LargeObject.SEEK_SET);


PS For what it is worth and if it is the case, I think it is a pretty dumb
thing for sun to make blob byte arrays 1 based. I would actually prefer 0
based, but being compatible is probably more important.

In response to

  • JBuilder4 at 2001-03-01 09:31:57 from Catalin CIOCOIU

pgsql-jdbc by date

Next:From: Peter MountDate: 2001-03-01 20:01:43
Subject: Re: Java class documentation
Previous:From: Catalin CIOCOIUDate: 2001-03-01 09:31:57
Subject: JBuilder4

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group