KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > ByteArrayBlobType


1 //$Id: ByteArrayBlobType.java,v 1.4 2005/07/25 06:43:41 epbernard Exp $
2
package org.hibernate.type;
3
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.sql.Blob JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.Types JavaDoc;
10 import java.util.Map JavaDoc;
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 /**
23  * Map a Byte[] into a Blob
24  * Experimental
25  *
26  * @author Emmanuel Bernard
27  */

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 JavaDoc public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode, SessionFactoryImplementor factory) {
35         if (x == y) return true;
36         if (x == null || y == null) return false;
37         if ( x instanceof Byte JavaDoc[] ) {
38             Object JavaDoc[] o1 = ( Object JavaDoc[] ) x;
39             Object JavaDoc[] o2 = ( Object JavaDoc[] ) 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 JavaDoc x, EntityMode entityMode, SessionFactoryImplementor factory) {
50         if ( x instanceof Character JavaDoc[] ) {
51             Object JavaDoc[] o = ( Object JavaDoc[] ) x;
52             return ArrayHelper.hash(o);
53         } else {
54             byte[] c = ( byte[] ) x;
55             return ArrayHelper.hash(c);
56         }
57     }
58
59     public Object JavaDoc deepCopy(Object JavaDoc value, EntityMode entityMode, SessionFactoryImplementor factory) throws HibernateException {
60         if (value == null) return null;
61         if ( value instanceof Byte JavaDoc[] ) {
62             Byte JavaDoc[] array = ( Byte JavaDoc[] ) value;
63             int length = array.length;
64             Byte JavaDoc[] copy = new Byte JavaDoc[length];
65             for (int index = 0 ; index < length ; index++) {
66                 copy[index] = new Byte JavaDoc( 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 JavaDoc getReturnedClass() {
80         return Byte JavaDoc[].class;
81     }
82
83     protected Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
84         Blob JavaDoc 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 JavaDoc st, Object JavaDoc value, int index, SessionImplementor session) throws SQLException JavaDoc {
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 JavaDoc( toSet ), toSet.length );
101             }
102             else {
103                 st.setBlob(index, new BlobImpl( toSet ) );
104             }
105         }
106     }
107
108     public void setToXMLNode(Node node, Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
109         node.setText( toString(value) );
110     }
111
112     public String JavaDoc toString(Object JavaDoc val) {
113         byte[] bytes = unWrap( val );
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115         for ( int i=0; i<bytes.length; i++ ) {
116             String JavaDoc 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 JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory) {
124         return value==null ? "null" : toString(value);
125     }
126
127     public Object JavaDoc fromXMLNode(Node xml, Mapping factory) throws HibernateException {
128         String JavaDoc xmlText = xml.getText();
129         return xmlText == null || xmlText.length() == 0 ? null : fromString(xmlText);
130     }
131
132     private Object JavaDoc fromString(String JavaDoc xmlText) {
133         if (xmlText == null)
134             return null;
135         if (xmlText.length() % 2 != 0)
136             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc wrap(byte[] bytes) {
146         return wrapPrimitive( bytes );
147     }
148
149     protected byte[] unWrap(Object JavaDoc bytes) {
150         return unwrapNonPrimitive( (Byte JavaDoc[]) bytes );
151     }
152
153     private byte[] unwrapNonPrimitive(Byte JavaDoc[] 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 JavaDoc[] wrapPrimitive(byte[] bytes) {
163         int length = bytes.length;
164         Byte JavaDoc[] result = new Byte JavaDoc[length];
165         for (int index = 0 ; index < length ; index++) {
166             result[index] = new Byte JavaDoc( bytes[index] );
167         }
168         return result;
169     }
170
171     public boolean isMutable() {
172         return true;
173     }
174
175     public Object JavaDoc replace(
176         Object JavaDoc original,
177         Object JavaDoc target,
178         SessionImplementor session,
179         Object JavaDoc owner,
180         Map JavaDoc 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 JavaDoc value, Mapping mapping) {
187         return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
188     }
189 }
190
Popular Tags