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,