1 package org.hibernate.type; 3 4 import org.hibernate.HibernateException; 5 import org.hibernate.util.ArrayHelper; 6 import org.hibernate.usertype.UserType; 7 8 import java.io.CharArrayReader ; 9 import java.io.IOException ; 10 import java.io.Reader ; 11 import java.io.Serializable ; 12 import java.sql.PreparedStatement ; 13 import java.sql.ResultSet ; 14 import java.sql.SQLException ; 15 import java.sql.Types ; 16 import java.util.ArrayList ; 17 18 24 public class CharacterArrayClobType implements UserType { 25 public static final int BUFFER_SIZE = 4096; 26 27 public int[] sqlTypes() { 28 return new int[] { Types.CLOB }; 29 } 30 31 public Class returnedClass() { 32 return Character [].class; 33 } 34 35 public boolean equals(Object x, Object y) throws HibernateException { 36 if (x == y) return true; 37 if (x == null || y == null) return false; 38 if ( x instanceof Character [] ) { 39 Object [] o1 = ( Object [] ) x; 40 Object [] o2 = ( Object [] ) y; 41 return ArrayHelper.isEquals(o1, o2); 42 } 43 else { 44 char[] c1 = ( char[] ) x; 45 char[] c2 = ( char[] ) y; 46 return ArrayHelper.isEquals(c1, c2); 47 } 48 } 49 50 public int hashCode(Object x) throws HibernateException { 51 if ( x instanceof Character [] ) { 52 Object [] o = ( Object [] ) x; 53 return ArrayHelper.hash(o); 54 } else { 55 char[] c = ( char[] ) x; 56 return ArrayHelper.hash(c); 57 } 58 } 59 60 public Object nullSafeGet(ResultSet rs, String [] names, Object owner) throws HibernateException, SQLException { 61 Reader reader = rs.getCharacterStream( names[0] ); 62 if (reader == null) return null; 63 ArrayList result = new ArrayList (); 64 try { 65 char[] charbuf = new char[BUFFER_SIZE]; 66 for (int i = reader.read(charbuf) ; i > 0 ; i = reader.read(charbuf) ) { 67 result.ensureCapacity( result.size() + BUFFER_SIZE ); 68 for (int charIndex = 0 ; charIndex < i ; charIndex++) { 69 result.add( new Character ( charbuf[charIndex] ) ); 70 } 71 } 72 } catch (IOException e) { 73 throw new SQLException ( e.getMessage() ); 74 } 75 if ( returnedClass().equals( Character [].class ) ) { 76 return result.toArray( new Character [ result.size() ] ); 77 } 78 else { 79 int length = result.size(); 81 char[] chars = new char[length]; 82 for (int index = 0 ; index < length ; index++) { 83 chars[index] = ( (Character ) result.get(index)).charValue(); 84 } 85 return chars; 86 } 87 } 88 89 public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 90 if (value != null) { 91 char[] chars; 92 if ( value instanceof Character [] ) { 93 Character [] character = ( Character [] ) value; 94 int length = character.length; 95 chars = new char[length]; 96 for (int i = 0 ; i < length ; i++) { 97 chars[i] = character[i].charValue(); 98 } 99 } 100 else { 101 chars = ( char[] ) value; 102 } 103 CharArrayReader reader = new CharArrayReader (chars); 104 st.setCharacterStream( index, reader, chars.length ); 105 } 106 else { 107 st.setNull( index, sqlTypes()[0] ); 108 } 109 } 110 111 public Object deepCopy(Object value) throws HibernateException { 112 if (value == null) return null; 113 if ( value instanceof Character [] ) { 114 Character [] array = ( Character [] ) value; 115 int length = array.length; 116 Character [] copy = new Character [length]; 117 for (int index = 0 ; index < length ; index++) { 118 copy[index] = new Character ( array[index].charValue() ); 119 } 120 return copy; 121 } 122 else { 123 char[] array = ( char[] ) value; 124 int length = array.length; 125 char[] copy = new char[length]; 126 System.arraycopy(array, 0, copy, 0, length); 127 return copy; 128 } 129 } 130 131 public boolean isMutable() { 132 return true; 133 } 134 135 public Serializable disassemble(Object value) throws HibernateException { 136 return (Serializable ) deepCopy(value); 137 } 138 139 public Object assemble(Serializable cached, Object owner) throws HibernateException { 140 return deepCopy(cached); 141 } 142 143 public Object replace(Object original, Object target, Object owner) throws HibernateException { 144 return deepCopy(original); 145 } 146 } 147 | Popular Tags |