1 package org.hibernate.type; 3 4 import java.io.ByteArrayInputStream ; 5 import java.sql.Blob ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.sql.Types ; 10 import java.util.Map ; 11 12 import org.dom4j.Node; 13 import org.hibernate.EntityMode; 14 import org.hibernate.HibernateException; 15 import org.hibernate.MappingException; 16 import org.hibernate.engine.Mapping; 17 import org.hibernate.engine.SessionFactoryImplementor; 18 import org.hibernate.engine.SessionImplementor; 19 import org.hibernate.lob.BlobImpl; 20 import org.hibernate.util.ArrayHelper; 21 22 28 public class ByteArrayBlobType extends AbstractLobType { 29 30 public int[] sqlTypes(Mapping mapping) throws MappingException { 31 return new int[] { Types.BLOB }; 32 } 33 34 @Override public boolean isEqual(Object x, Object y, EntityMode entityMode, SessionFactoryImplementor factory) { 35 if (x == y) return true; 36 if (x == null || y == null) return false; 37 if ( x instanceof Byte [] ) { 38 Object [] o1 = ( Object [] ) x; 39 Object [] o2 = ( Object [] ) y; 40 return ArrayHelper.isEquals(o1, o2); 41 } 42 else { 43 byte[] c1 = ( byte[] ) x; 44 byte[] c2 = ( byte[] ) y; 45 return ArrayHelper.isEquals(c1, c2); 46 } 47 } 48 49 public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor factory) { 50 if ( x instanceof Character [] ) { 51 Object [] o = ( Object [] ) x; 52 return ArrayHelper.hash(o); 53 } else { 54 byte[] c = ( byte[] ) x; 55 return ArrayHelper.hash(c); 56 } 57 } 58 59 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) throws HibernateException { 60 if (value == null) return null; 61 if ( value instanceof Byte [] ) { 62 Byte [] array = ( Byte [] ) value; 63 int length = array.length; 64 Byte [] copy = new Byte [length]; 65 for (int index = 0 ; index < length ; index++) { 66 copy[index] = new Byte ( array[index].byteValue() ); 67 } 68 return copy; 69 } 70 else { 71 byte[] array = ( byte[] ) value; 72 int length = array.length; 73 byte[] copy = new byte[length]; 74 System.arraycopy(array, 0, copy, 0, length); 75 return copy; 76 } 77 } 78 79 public Class getReturnedClass() { 80 return Byte [].class; 81 } 82 83 protected Object get(ResultSet rs, String name) throws SQLException { 84 Blob blob = rs.getBlob( name ); 85 if ( rs.wasNull() ) return null; 86 int length = (int) blob.length(); 87 byte[] primaryResult = blob.getBytes( 1, length ); 88 return wrap( primaryResult ); 89 } 90 91 protected void set(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException { 92 if (value == null) { 93 st.setNull( index, sqlTypes(null)[0] ); 94 } 95 else { 96 byte[] toSet = unWrap( value ); 97 final boolean useInputStream = session.getFactory().getDialect().useInputStreamToInsertBlob(); 98 99 if ( useInputStream ) { 100 st.setBinaryStream( index, new ByteArrayInputStream ( toSet ), toSet.length ); 101 } 102 else { 103 st.setBlob(index, new BlobImpl( toSet ) ); 104 } 105 } 106 } 107 108 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { 109 node.setText( toString(value) ); 110 } 111 112 public String toString(Object val) { 113 byte[] bytes = unWrap( val ); 114 StringBuffer buf = new StringBuffer (); 115 for ( int i=0; i<bytes.length; i++ ) { 116 String hexStr = Integer.toHexString( bytes[i] - Byte.MIN_VALUE ); 117 if ( hexStr.length()==1 ) buf.append('0'); 118 buf.append(hexStr); 119 } 120 return buf.toString(); 121 } 122 123 public String toLoggableString(Object value, SessionFactoryImplementor factory) { 124 return value==null ? "null" : toString(value); 125 } 126 127 public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { 128 String xmlText = xml.getText(); 129 return xmlText == null || xmlText.length() == 0 ? null : fromString(xmlText); 130 } 131 132 private Object fromString(String xmlText) { 133 if (xmlText == null) 134 return null; 135 if (xmlText.length() % 2 != 0) 136 throw new IllegalArgumentException ("The string is not a valid xml representation of a binary content."); 137 byte[] bytes = new byte[xmlText.length() / 2]; 138 for (int i = 0; i < bytes.length; i++) { 139 String hexStr = xmlText.substring(i * 2, (i + 1) * 2); 140 bytes[i] = (byte) (Integer.parseInt(hexStr, 16) + Byte.MIN_VALUE); 141 } 142 return wrap( bytes ); 143 } 144 145 protected Object wrap(byte[] bytes) { 146 return wrapPrimitive( bytes ); 147 } 148 149 protected byte[] unWrap(Object bytes) { 150 return unwrapNonPrimitive( (Byte []) bytes ); 151 } 152 153 private byte[] unwrapNonPrimitive(Byte [] bytes) { 154 int length = bytes.length; 155 byte[] result = new byte[length]; 156 for (int i = 0 ; i < length ; i++) { 157 result[i] = bytes[i].byteValue(); 158 } 159 return result; 160 } 161 162 private Byte [] wrapPrimitive(byte[] bytes) { 163 int length = bytes.length; 164 Byte [] result = new Byte [length]; 165 for (int index = 0 ; index < length ; index++) { 166 result[index] = new Byte ( bytes[index] ); 167 } 168 return result; 169 } 170 171 public boolean isMutable() { 172 return true; 173 } 174 175 public Object replace( 176 Object original, 177 Object target, 178 SessionImplementor session, 179 Object owner, 180 Map copyCache) 181 throws HibernateException { 182 if ( isEqual( original, target, session.getEntityMode() ) ) return original; 183 return deepCopy( original, session.getEntityMode(), session.getFactory() ); 184 } 185 186 public boolean[] toColumnNullness(Object value, Mapping mapping) { 187 return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE; 188 } 189 } 190 | Popular Tags |