1 package org.hibernate.type; 3 4 import java.io.ByteArrayInputStream ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.sql.Types ; 12 13 import org.hibernate.EntityMode; 14 import org.hibernate.HibernateException; 15 import org.hibernate.cfg.Environment; 16 17 21 public class BinaryType extends MutableType { 22 23 public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 24 25 if ( Environment.useStreamsForBinary() ) { 26 st.setBinaryStream( index, new ByteArrayInputStream ( (byte[]) value ), ( (byte[]) value ).length ); 27 } 28 else { 29 st.setBytes( index, (byte[]) value ); 30 } 31 } 32 33 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 34 35 if ( Environment.useStreamsForBinary() ) { 36 37 InputStream inputStream = rs.getBinaryStream(name); 38 39 if (inputStream==null) return null; 41 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (2048); 42 byte[] buffer = new byte[2048]; 43 44 try { 45 while (true) { 46 int amountRead = inputStream.read(buffer); 47 if (amountRead == -1) { 48 break; 49 } 50 outputStream.write(buffer, 0, amountRead); 51 } 52 53 inputStream.close(); 54 outputStream.close(); 55 } 56 catch (IOException ioe) { 57 throw new HibernateException( "IOException occurred reading a binary value", ioe ); 58 } 59 60 return outputStream.toByteArray(); 61 62 } 63 else { 64 return rs.getBytes(name); 65 } 66 } 67 68 public int sqlType() { 69 return Types.VARBINARY; 70 } 71 72 public Class getReturnedClass() { 73 return byte[].class; 74 } 75 76 public boolean isEqual(Object x, Object y) { 77 return x==y || ( x!=null && y!=null && java.util.Arrays.equals( (byte[]) x, (byte[]) y ) ); 78 } 79 80 public int getHashCode(Object x, EntityMode entityMode) { 81 byte[] bytes = (byte[]) x; 82 int hashCode = 1; 83 for ( int j=0; j<bytes.length; j++ ) { 84 hashCode = 31 * hashCode + bytes[j]; 85 } 86 return hashCode; 87 } 88 89 public int compare(Object x, Object y, EntityMode entityMode) { 90 byte[] xbytes = (byte[]) x; 91 byte[] ybytes = (byte[]) y; 92 if ( xbytes.length < ybytes.length ) return -1; 93 if ( xbytes.length > ybytes.length ) return 1; 94 for ( int i=0; i<xbytes.length; i++ ) { 95 if ( xbytes[i] < ybytes[i] ) return -1; 96 if ( xbytes[i] > ybytes[i] ) return 1; 97 } 98 return 0; 99 } 100 101 public String getName() { return "binary"; } 102 103 public String toString(Object val) { 104 byte[] bytes = ( byte[] ) val; 105 StringBuffer buf = new StringBuffer (); 106 for ( int i=0; i<bytes.length; i++ ) { 107 String hexStr = Integer.toHexString( bytes[i] - Byte.MIN_VALUE ); 108 if ( hexStr.length()==1 ) buf.append('0'); 109 buf.append(hexStr); 110 } 111 return buf.toString(); 112 } 113 114 public Object deepCopyNotNull(Object value) { 115 byte[] bytes = (byte[]) value; 116 byte[] result = new byte[bytes.length]; 117 System.arraycopy(bytes, 0, result, 0, bytes.length); 118 return result; 119 } 120 121 public Object fromStringValue(String xml) throws HibernateException { 122 if (xml == null) 123 return null; 124 if (xml.length() % 2 != 0) 125 throw new IllegalArgumentException ("The string is not a valid xml representation of a binary content."); 126 byte[] bytes = new byte[xml.length() / 2]; 127 for (int i = 0; i < bytes.length; i++) { 128 String hexStr = xml.substring(i * 2, (i + 1) * 2); 129 bytes[i] = (byte) (Integer.parseInt(hexStr, 16) + Byte.MIN_VALUE); 130 } 131 return bytes; 132 } 133 134 } 135 136 137 138 139 140 141 142 | Popular Tags |