Thread: java.lang.NegativeArraySizeException at org.postgresql.jdbc1.AbstractJdbc1Statement.setBinaryStream()

Hello,

I've the following code :

        ...
        ByteArrayInputStream    is = new
ByteArrayInputStream(str.getBytes());
        prepStatement.setBinaryStream(1, is, str.getBytes().length); // can
be UTF-8
        ...

assuming that prepStatement is a correct query that updates a row containing
a TEXT field, in column 1, and str represents a Unicode string to store in
this TEXT field. When running this exact code with PostgreSQL 7.2 driver
using the "?compatible=7.1" switch, it was working just fine when called
with an empty str (str = ""). When I ported this application to use
PostgreSQL 7.3 driver without the "?compatible=7.1", this code produces a
java.lang.NegativeArraySizeException on the prepStatement.setBinaryStream()
line.

FYI, I noted that I had to change OID to BYTEA in my schema for the BLOBs in
order to upgrade from 7.1 to 7.3 compatibility mode, but didn't see anything
regarding TEXT fields (CLOB). I can also add that I tried all the different
7.3 drivers as well as the latest 7.4 dev driver with the exact same error.
Finally, I couldn't find, in the Java 1.3 documentation, anything regarding
the second and third parameters of PreparedStatement.setBinaryStream()
which, to me, can be null and 0 respectively.

Any help would be greatly appreciated,
Thanks in advance,
Frederic Thomas.


Re: java.lang.NegativeArraySizeException at

From
Kris Jurka
Date:

On Tue, 18 Nov 2003, Frederic Thomas wrote:

> Hello,
>
> I've the following code :
>
>         ...
>         ByteArrayInputStream    is = new
> ByteArrayInputStream(str.getBytes());
>         prepStatement.setBinaryStream(1, is, str.getBytes().length); // can
> be UTF-8
>         ...
>
> assuming that prepStatement is a correct query that updates a row containing
> a TEXT field, in column 1, and str represents a Unicode string to store in
> this TEXT field. When running this exact code with PostgreSQL 7.2 driver
> using the "?compatible=7.1" switch, it was working just fine when called
> with an empty str (str = ""). When I ported this application to use
> PostgreSQL 7.3 driver without the "?compatible=7.1", this code produces a
> java.lang.NegativeArraySizeException on the prepStatement.setBinaryStream()
> line.
>
> FYI, I noted that I had to change OID to BYTEA in my schema for the BLOBs in
> order to upgrade from 7.1 to 7.3 compatibility mode, but didn't see anything
> regarding TEXT fields (CLOB). I can also add that I tried all the different
> 7.3 drivers as well as the latest 7.4 dev driver with the exact same error.
> Finally, I couldn't find, in the Java 1.3 documentation, anything regarding
> the second and third parameters of PreparedStatement.setBinaryStream()
> which, to me, can be null and 0 respectively.
>

I think the real question is: why are you calling setBinaryStream on a
text field?  Shouldn't you just do:  prepStatement.setString(1,str);
instead of going through the BinaryStream?

In any case the driver shouldn't throw an Exception like that.  Here's a
patch that fixes that.

Kris Jurka


Attachment
> I think the real question is: why are you calling setBinaryStream on a
> text field?  Shouldn't you just do:  prepStatement.setString(1,str);
> instead of going through the BinaryStream?
The reason is (probably?) because prepStatement.setString() is used to
convert a String into an SQL VARCHAR or LONGVARCHAR value (see the Java 1.3
documentation) but in this particular table, we've TEXT fields and not
VARCHAR fields. Another reason would be that our product is multi-database,
and this code is shared in our SQL layer (by opposition to the database
specific layer, which offers compatibility with Oracle, DB2, SQLServer, and,
of course, PostgreSQL).

> In any case the driver shouldn't throw an Exception like that.  Here's a
> patch that fixes that.
Glad to see that it wasn't a serious problem. Thanks a lot for the patch.
However, is there a way to retrieve only the source code for the
pg73jdbc2.jar driver, by opposition to the whole product source code?

An additional question: do you know if a new binary version of the 7.3
driver will be available sometimes? I'd like to avoid compiling in a "non
professional" way a driver that I will ship to my customers, and would
prefer to rely on the "official" binary release of it.

Thanks again for your help,
Frederic Thomas.

-----Original Message-----
From: Kris Jurka [mailto:books@ejurka.com]
Sent: mardi 18 novembre 2003 08:50
To: Frederic Thomas
Cc: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] java.lang.NegativeArraySizeException at
org.postgresql.jdbc1.AbstractJdbc1Statement.setBinaryStream()




On Tue, 18 Nov 2003, Frederic Thomas wrote:

> Hello,
>
> I've the following code :
>
>         ...
>         ByteArrayInputStream    is = new
> ByteArrayInputStream(str.getBytes());
>         prepStatement.setBinaryStream(1, is, str.getBytes().length); //
can
> be UTF-8
>         ...
>
> assuming that prepStatement is a correct query that updates a row
containing
> a TEXT field, in column 1, and str represents a Unicode string to store in
> this TEXT field. When running this exact code with PostgreSQL 7.2 driver
> using the "?compatible=7.1" switch, it was working just fine when called
> with an empty str (str = ""). When I ported this application to use
> PostgreSQL 7.3 driver without the "?compatible=7.1", this code produces a
> java.lang.NegativeArraySizeException on the
prepStatement.setBinaryStream()
> line.
>
> FYI, I noted that I had to change OID to BYTEA in my schema for the BLOBs
in
> order to upgrade from 7.1 to 7.3 compatibility mode, but didn't see
anything
> regarding TEXT fields (CLOB). I can also add that I tried all the
different
> 7.3 drivers as well as the latest 7.4 dev driver with the exact same
error.
> Finally, I couldn't find, in the Java 1.3 documentation, anything
regarding
> the second and third parameters of PreparedStatement.setBinaryStream()
> which, to me, can be null and 0 respectively.
>

I think the real question is: why are you calling setBinaryStream on a
text field?  Shouldn't you just do:  prepStatement.setString(1,str);
instead of going through the BinaryStream?

In any case the driver shouldn't throw an Exception like that.  Here's a
patch that fixes that.

Kris Jurka



Re: java.lang.NegativeArraySizeException at

From
Kris Jurka
Date:

On Tue, 18 Nov 2003, Frederic Thomas wrote:

> > In any case the driver shouldn't throw an Exception like that.  Here's a
> > patch that fixes that.
> Glad to see that it wasn't a serious problem. Thanks a lot for the patch.
> However, is there a way to retrieve only the source code for the
> pg73jdbc2.jar driver, by opposition to the whole product source code?

You can retrieve just the 7.3 jdbc driver like so:

cvs -d :pserver:anoncvs@anoncvs.postgresql.org:/projects/cvsroot login

give anything but an empty string for a password and

cvs -d :pserver:anoncvs@anoncvs.postgresql.org:/projects/cvsroot co -r
REL7_3_STABLE server/src/interfaces/jdbc

but you won't be able to build it without hacking the Makefile (because it
wants to include a global Makefile that gets created by the main source
tree's configure process).

> An additional question: do you know if a new binary version of the 7.3
> driver will be available sometimes? I'd like to avoid compiling in a "non
> professional" way a driver that I will ship to my customers, and would
> prefer to rely on the "official" binary release of it.

I don't know if a new 7.3 binary will be released.  Barry Lind is in
charge of that.  Generally speaking fixes don't get backpatched to older
versions.  With 7.4 being released yesterday I imagine fixes will only go
into cvs head and the 7.4 branch.  On the other hand there has been talk
of putting out a 7.3.5 release and if that happens this fix should
probably go into that, but that's not my call.

Kris Jurka



Kris, following your remark regarding setBinaryStream() I revisited the code
around this, and finally used setCharacterStream() which not only solved my
problem with PostgreSQL jdbc driver in 7.3, but also allow me to cleanup our
code a little bit. So, once again, thanks for your help.

Frederic Thomas.

-----Original Message-----
From: Kris Jurka [mailto:books@ejurka.com]
Sent: mardi 18 novembre 2003 10:06
To: Frederic Thomas
Cc: pgsql-jdbc@postgresql.org
Subject: RE: [JDBC] java.lang.NegativeArraySizeException at
org.postgresql.jdbc1.AbstractJdbc1Statement.setBinaryStream()




On Tue, 18 Nov 2003, Frederic Thomas wrote:

> > In any case the driver shouldn't throw an Exception like that.  Here's a
> > patch that fixes that.
> Glad to see that it wasn't a serious problem. Thanks a lot for the patch.
> However, is there a way to retrieve only the source code for the
> pg73jdbc2.jar driver, by opposition to the whole product source code?

You can retrieve just the 7.3 jdbc driver like so:

cvs -d :pserver:anoncvs@anoncvs.postgresql.org:/projects/cvsroot login

give anything but an empty string for a password and

cvs -d :pserver:anoncvs@anoncvs.postgresql.org:/projects/cvsroot co -r
REL7_3_STABLE server/src/interfaces/jdbc

but you won't be able to build it without hacking the Makefile (because it
wants to include a global Makefile that gets created by the main source
tree's configure process).

> An additional question: do you know if a new binary version of the 7.3
> driver will be available sometimes? I'd like to avoid compiling in a "non
> professional" way a driver that I will ship to my customers, and would
> prefer to rely on the "official" binary release of it.

I don't know if a new 7.3 binary will be released.  Barry Lind is in
charge of that.  Generally speaking fixes don't get backpatched to older
versions.  With 7.4 being released yesterday I imagine fixes will only go
into cvs head and the 7.4 branch.  On the other hand there has been talk
of putting out a 7.3.5 release and if that happens this fix should
probably go into that, but that's not my call.

Kris Jurka