-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping a Blob to a byte[]: possible bug...
PostPosted: Tue Jul 05, 2005 5:13 pm 
Beginner
Beginner

Joined: Mon Jan 05, 2004 12:48 pm
Posts: 31
Document: http://www.hibernate.org/73.html
Code:
Quote:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException
{
Blob blob = rs.getBlob(names[0]);
return blob.getBytes(1, (int) blob.length());
}


Having this code I got StreamCorruptedException when trying to deserialize the object with ObjectInputStream.
After examining byte arrays before storing into a table (after serialization) and after retrieving from a table (before deserialization) I found that the latter array is short of the first byte!

So the code should be:
Quote:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException
{
Blob blob = rs.getBlob(names[0]);
return blob.getBytes(0, (int) blob.length());
}


With this code everything works fine.
However Java API on Blob says:

Quote:
Code:
public byte[] getBytes(long pos,
                       int length)

throws SQLExceptionRetrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. This byte array contains up to length consecutive bytes starting at position pos.

Parameters:
pos - the ordinal position of the first byte in the BLOB value to be extracted; the first byte is at position 1
length - the number of consecutive bytes to be copied


It looks like the Java API in that part is wrong!
Can somebody explain this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.