KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: BinaryType.java,v 1.6 2005/02/16 12:50:18 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.sql.Types JavaDoc;
12
13 import org.hibernate.EntityMode;
14 import org.hibernate.HibernateException;
15 import org.hibernate.cfg.Environment;
16
17 /**
18  * <tt>binary</tt>: A type that maps an SQL VARBINARY to a Java byte[].
19  * @author Gavin King
20  */

21 public class BinaryType extends MutableType {
22
23     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
24
25         if ( Environment.useStreamsForBinary() ) {
26             st.setBinaryStream( index, new ByteArrayInputStream JavaDoc( (byte[]) value ), ( (byte[]) value ).length );
27         }
28         else {
29             st.setBytes( index, (byte[]) value );
30         }
31     }
32
33     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
34
35         if ( Environment.useStreamsForBinary() ) {
36
37             InputStream JavaDoc inputStream = rs.getBinaryStream(name);
38
39             if (inputStream==null) return null; // is this really necessary?
40

41             ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc(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 JavaDoc 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 JavaDoc getReturnedClass() {
73         return byte[].class;
74     }
75
76     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
77         return x==y || ( x!=null && y!=null && java.util.Arrays.equals( (byte[]) x, (byte[]) y ) );
78     }
79     
80     public int getHashCode(Object JavaDoc 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 JavaDoc x, Object JavaDoc 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 JavaDoc getName() { return "binary"; }
102
103     public String JavaDoc toString(Object JavaDoc val) {
104         byte[] bytes = ( byte[] ) val;
105         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106         for ( int i=0; i<bytes.length; i++ ) {
107             String JavaDoc 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 JavaDoc deepCopyNotNull(Object JavaDoc 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 JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
122         if (xml == null)
123             return null;
124         if (xml.length() % 2 != 0)
125             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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