1 16 17 package org.springframework.orm.hibernate3.support; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.io.Serializable ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.Types ; 30 31 import javax.transaction.TransactionManager ; 32 33 import org.hibernate.HibernateException; 34 35 import org.springframework.jdbc.support.lob.LobCreator; 36 import org.springframework.jdbc.support.lob.LobHandler; 37 38 51 public class BlobSerializableType extends AbstractLobType { 52 53 59 private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 1024; 60 61 67 public BlobSerializableType() { 68 super(); 69 } 70 71 75 protected BlobSerializableType(LobHandler lobHandler, TransactionManager jtaTransactionManager) { 76 super(lobHandler, jtaTransactionManager); 77 } 78 79 public int[] sqlTypes() { 80 return new int[] {Types.BLOB}; 81 } 82 83 public Class returnedClass() { 84 return Serializable .class; 85 } 86 87 public boolean isMutable() { 88 return true; 89 } 90 91 public Object deepCopy(Object value) throws HibernateException { 92 try { 93 ByteArrayOutputStream baos = new ByteArrayOutputStream (OUTPUT_BYTE_ARRAY_INITIAL_SIZE); 95 ObjectOutputStream oos = new ObjectOutputStream (baos); 96 try { 97 oos.writeObject(value); 98 } 99 finally { 100 oos.close(); 101 } 102 103 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 105 ObjectInputStream ois = new ObjectInputStream (bais); 106 try { 107 return ois.readObject(); 108 } 109 finally { 110 ois.close(); 111 } 112 } 113 catch (ClassNotFoundException ex) { 114 throw new HibernateException("Couldn't clone BLOB contents", ex); 115 } 116 catch (IOException ex) { 117 throw new HibernateException("Couldn't clone BLOB contents", ex); 118 } 119 } 120 121 protected Object nullSafeGetInternal( 122 ResultSet rs, String [] names, Object owner, LobHandler lobHandler) 123 throws SQLException , IOException , HibernateException { 124 125 InputStream is = lobHandler.getBlobAsBinaryStream(rs, names[0]); 126 if (is != null) { 127 ObjectInputStream ois = new ObjectInputStream (is); 128 try { 129 return ois.readObject(); 130 } 131 catch (ClassNotFoundException ex) { 132 throw new HibernateException("Could not deserialize BLOB contents", ex); 133 } 134 finally { 135 ois.close(); 136 } 137 } 138 else { 139 return null; 140 } 141 } 142 143 protected void nullSafeSetInternal( 144 PreparedStatement ps, int index, Object value, LobCreator lobCreator) 145 throws SQLException , IOException { 146 147 if (value != null) { 148 ByteArrayOutputStream baos = new ByteArrayOutputStream (OUTPUT_BYTE_ARRAY_INITIAL_SIZE); 149 ObjectOutputStream oos = new ObjectOutputStream (baos); 150 try { 151 oos.writeObject(value); 152 oos.flush(); 153 lobCreator.setBlobAsBytes(ps, index, baos.toByteArray()); 154 } 155 finally { 156 oos.close(); 157 } 158 } 159 else { 160 lobCreator.setBlobAsBytes(ps, index, null); 161 } 162 } 163 164 } 165 | Popular Tags |