1 16 17 package org.springframework.orm.ibatis.support; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 28 import org.springframework.jdbc.support.lob.LobCreator; 29 import org.springframework.jdbc.support.lob.LobHandler; 30 31 44 public class BlobSerializableTypeHandler extends AbstractLobTypeHandler { 45 46 51 public BlobSerializableTypeHandler() { 52 super(); 53 } 54 55 58 protected BlobSerializableTypeHandler(LobHandler lobHandler) { 59 super(lobHandler); 60 } 61 62 protected void setParameterInternal( 63 PreparedStatement ps, int index, Object value, String jdbcType, LobCreator lobCreator) 64 throws SQLException , IOException { 65 66 if (value != null) { 67 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 68 ObjectOutputStream oos = new ObjectOutputStream (baos); 69 try { 70 oos.writeObject(value); 71 oos.flush(); 72 lobCreator.setBlobAsBytes(ps, index, baos.toByteArray()); 73 } 74 finally { 75 oos.close(); 76 } 77 } 78 else { 79 lobCreator.setBlobAsBytes(ps, index, null); 80 } 81 } 82 83 protected Object getResultInternal(ResultSet rs, int index, LobHandler lobHandler) 84 throws SQLException , IOException { 85 86 InputStream is = lobHandler.getBlobAsBinaryStream(rs, index); 87 if (is != null) { 88 ObjectInputStream ois = new ObjectInputStream (is); 89 try { 90 return ois.readObject(); 91 } 92 catch (ClassNotFoundException ex) { 93 throw new SQLException ("Could not deserialize BLOB contents: " + ex.getMessage()); 94 } 95 finally { 96 ois.close(); 97 } 98 } 99 else { 100 return null; 101 } 102 } 103 104 public Object valueOf(String s) { 105 return s; 106 } 107 108 } 109 | Popular Tags |