1 package org.hibernate.type; 3 4 import java.io.IOException ; 5 import java.io.Reader ; 6 import java.io.StringReader ; 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Types ; 11 12 import org.hibernate.HibernateException; 13 14 18 public class TextType extends ImmutableType { 19 20 public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 21 String str = (String ) value; 22 st.setCharacterStream( index, new StringReader (str), str.length() ); 23 } 24 25 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 26 27 Reader charReader = rs.getCharacterStream(name); 30 31 if (charReader==null) return null; 33 34 StringBuffer sb = new StringBuffer (); 36 try { 37 char[] buffer = new char[2048]; 38 while (true) { 39 int amountRead = charReader.read(buffer, 0, buffer.length); 40 if ( amountRead == -1 ) break; 41 sb.append(buffer, 0, amountRead); 42 } 43 } 44 catch (IOException ioe) { 45 throw new HibernateException( "IOException occurred reading text", ioe ); 46 } 47 finally { 48 try { 49 charReader.close(); 50 } 51 catch (IOException e) { 52 throw new HibernateException( "IOException occurred closing stream", e ); 53 } 54 } 55 56 return sb.toString(); 58 } 59 60 public int sqlType() { 61 return Types.CLOB; } 63 64 public Class getReturnedClass() { 65 return String .class; 66 } 67 68 public String getName() { return "text"; } 69 70 public String toString(Object val) { 71 return (String ) val; 72 } 73 public Object fromStringValue(String xml) { 74 return xml; 75 } 76 77 } 78 79 80 81 82 83 84 85 | Popular Tags |