-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to save Blob using ByteArrayInputStream?
PostPosted: Thu Oct 23, 2003 3:55 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Hi,

I am trying to save Blob data using ByteArrayInputStream instead of byte[]
as in BinaryBlobType example in http://www.hibernate.org/73.html.

My BinaryBlobType code as following:

<Code>
package sample;

import java.io.ByteArrayInputStream;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class BinaryBlobType implements UserType
{
public int[] sqlTypes()
{
return new int[] { Types.BLOB };
}

public Class returnedClass()
{
//return byte[].class;
return ByteArrayInputStream.class;
}

public boolean equals(Object x, Object y)
{
//ByteArrayInputStream x = (ByteArrayInputStream)a;
//ByteArrayInputStream y = (ByteArrayInputStream)b;

return (x == y) //ClassCastException here ************
|| (x != null
&& y != null
&& java.util.Arrays.equals((byte[]) x, (byte[]) y));
}

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());
return (ByteArrayInputStream) blob.getBinaryStream();
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException
{
//st.setBlob(index, Hibernate.createBlob((byte[]) value));
final byte[] buf = (byte[]) value;
ByteArrayInputStream in = new ByteArrayInputStream(buf);
st.setBinaryStream(index, in, buf.length);
}

public Object deepCopy(Object value)
{
if (value == null) return null;

//byte[] bytes = (byte[]) value;
//byte[] result = new byte[bytes.length];
//System.arraycopy(bytes, 0, result, 0, bytes.length);
final byte[] buf = (byte[]) value;
ByteArrayInputStream result = new ByteArrayInputStream(buf);

return result;
}

public boolean isMutable()
{
return true;
}

}

</Code>

Then I set type = "sample.BinaryBlobType". I have tried numerous ways to transform the above code from using byte[] to ByteArrayInputStream.
But I keep getting ClassCastException inside equals method. I am wondering if it is feasible to use ByteArrayInputStream to do the job (and to improve performance)?

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 11:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
public boolean equals(Object x, Object y)
{
//ByteArrayInputStream x = (ByteArrayInputStream)a;
//ByteArrayInputStream y = (ByteArrayInputStream)b;

return (x == y) //ClassCastException here ************
|| (x != null
&& y != null
&& java.util.Arrays.equals((byte[]) x, (byte[]) y));
}


Not sure if this is do to bad copy/paste, but your UserType is defining the returned class as type ByteArrayInputStream, but then in the equals() above it tries to cast the ByteArrayInputStream params to byte[] in java.util.Arrays.equals((byte[]) x, (byte[]) y).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 1:29 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
First note, you can simply use x.equals(y), which ByteArrayInputStreams inherit from Object.

Secondly, here's a bit of code that you could use to convert to a String and back again.

Code:
Blob blob;
PreparedStatement ps;
ByteArrayInputStream bais;
ByteArrayInputStream bais2;
String s;
StringBuffer sb;
...

bais = new ByteArrayInputStream(blob.getBytes());
sb = new StringBuffer();
int size  = bais.available();
for (int i=0; i < size; i++) {
    sb.append(bais.read());
}
s = sb.toString();
//here we have a String...
bais2 = new ByteArrayInputStream(s.getBytes());
ps.setBinaryStream(1, bais2, bais2.available());
...


-G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 2:52 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Thanks, guys for your input:

for the following method:

Code:
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());
}


from API:
names - the column names

so:

names[0] means the first column ? This should be the table column storing Blob, right? For example, in my db, if my Blob data is stored in column 6, the I should use :

Blob blob = rs.getBlob(names[6]);

Is that right?

I tried the above, but it gave me "java.lang.ArrayIndexOutOfBoundsException: 6"

regards,[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 10:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The names array only "covers" columns spanned by your type. If you only map one column using this type, then the names array will be length of one...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 10:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Oh, and the sixth element in a zero-based array would be index 5

:)


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

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.