getBytes() returning too much data - Mailing list pgsql-jdbc

From Conor Beverland
Subject getBytes() returning too much data
Date
Msg-id Pine.LNX.4.58.0401071808330.18419@active45.doc.ic.ac.uk
Whole thread Raw
Responses Re: getBytes() returning too much data  (Barry Lind <blind@xythos.com>)
List pgsql-jdbc
Hi,

I'm having a problem retreiving images from a postgreSQL database using
ResultSet.getBytes() The image is stored in the database as a bytea
type. I'm running java version 1.4.1 and postgreSQL version 7.3.4

When I insert a file it seems to be the correct size inside the database
(checked using SQL - length(image))
However when I use getBytes() or even getBinaryStream() to get the data
back from the database it always returns too much data.
The returned bytes contain at least some of the correct data (perhaps
all) with a load of extra bytes throughout the file. The size increase
is consistent for each file.

Does anyone know what I'm doing wrong and/or how I might go about making
it work correctly?

Thanks,
Conor

I've inserted an image like this: -

File file = new File("myimage.gif");
try {
   FileInputStream fis = new FileInputStream(file);
   int length = (int)file.length();
   byte[] bytes = new byte[length];
   fis.read(bytes);
   fis.close();
   PreparedStatement ps = db.prepareStatement("INSERT INTO images
      (image) VALUES (?)");
   ps.setBytes(1, bytes);
} catch () { }

And I try to get the image back like this: -

try {
   Statement stmt = db.createStatement();
   rs = stmt.executeQuery("SELECT image, length(image) FROM images");
   while (rs.next()) {
      byte[] imgBytes = rs.getBytes(1);
   }
} catch () { }

pgsql-jdbc by date:

Previous
From: Barry Lind
Date:
Subject: Re: PreparedStatement parameters and mutable objects
Next
From: Thomas Kellerer
Date:
Subject: getTypeInfo() bug