1 16 package com.blandware.atleap.persistence.hibernate.util; 17 18 import org.hibernate.Hibernate; 19 import org.hibernate.HibernateException; 20 import org.hibernate.cfg.Environment; 21 import org.hibernate.engine.SessionImplementor; 22 import org.hibernate.type.Type; 23 import org.hibernate.usertype.CompositeUserType; 24 import org.hibernate.util.PropertiesHelper; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.Serializable ; 30 import java.sql.Blob ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 35 45 public class BinaryBlobType implements CompositeUserType { 46 47 50 protected static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 51 52 55 protected boolean isBlob = false; 56 57 60 public BinaryBlobType() { 61 isBlob = "blob".equalsIgnoreCase(PropertiesHelper.getString("hibernate.binary_or_blob", Environment.getProperties(), "binary")); 62 } 63 64 65 71 public String [] getPropertyNames() { 72 return new String []{"value"}; 73 } 74 75 80 public Type[] getPropertyTypes() { 81 if (isBlob) 82 return new Type[]{Hibernate.BLOB}; 83 else 84 return new Type[]{Hibernate.BINARY}; 85 } 86 87 96 public Object getPropertyValue(Object component, int property) throws HibernateException { 97 return component; 98 } 99 100 109 public void setPropertyValue(Object component, int property, Object value) throws HibernateException { 110 } 111 112 117 public Class returnedClass() { 118 return Hibernate.BINARY.getReturnedClass(); 119 } 120 121 131 public boolean equals(Object x, Object y) throws HibernateException { 132 return Hibernate.BINARY.isEqual(x, y); 133 } 134 135 138 public int hashCode(Object x) throws HibernateException { 139 return Hibernate.BINARY.getHashCode(x, null); 140 } 141 142 155 public Object nullSafeGet(ResultSet rs, String [] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { 156 if (isBlob) { 157 Blob blob = (Blob )Hibernate.BLOB.nullSafeGet(rs, names, session, owner); 158 if (blob == null) 159 return null; 160 else 161 return copyData(blob.getBinaryStream()); 162 } else { 163 return Hibernate.BINARY.nullSafeGet(rs, names, session, owner); 164 } 165 } 166 167 180 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { 181 if (isBlob) { 182 if (value == null) 183 Hibernate.BLOB.nullSafeSet(st, value, index, session); 184 else { 185 Blob blob = Hibernate.createBlob((byte[])value); 186 Hibernate.BLOB.nullSafeSet(st, blob, index, session); 187 } 188 } else { 189 Hibernate.BINARY.nullSafeSet(st, value, index, session); 190 } 191 } 192 193 201 public Object deepCopy(Object value) throws HibernateException { 202 return Hibernate.BINARY.deepCopy(value, null, null); 203 } 204 205 210 public boolean isMutable() { 211 return Hibernate.BINARY.isMutable(); 212 } 213 214 226 public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException { 227 return Hibernate.BINARY.disassemble(value, session, null); 228 } 229 230 241 public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException { 242 return Hibernate.BINARY.assemble(cached, session, owner); 243 } 244 245 261 public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException { 262 return Hibernate.BINARY.replace(original, target, session, owner, null); 263 } 264 265 266 272 protected byte[] copyData(InputStream input) { 273 ByteArrayOutputStream output = null; 274 try { 275 output = new ByteArrayOutputStream (); 276 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 277 int n = 0; 278 while (-1 != (n = input.read(buffer))) { 279 output.write(buffer, 0, n); 280 } 281 return output.toByteArray(); 282 283 } catch (IOException ex) { 284 throw new RuntimeException ("Cannot copy data from InputStream into byte[]", ex); 285 } finally { 286 try { 287 input.close(); 288 } catch (IOException ex2) { 289 } 291 try { 292 output.close(); 293 } catch (IOException ex2) { 294 } 296 } 297 } 298 } 299 | Popular Tags |