KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rmijdbc > RJBlob


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) ExperLog 1999-2000
5  *
6  * @version 1.0
7  * @author Pierre-Yves Gibello (pierreyves.gibello@experlog.com)
8  */

9
10 package org.objectweb.rmijdbc;
11
12 import java.sql.*;
13 import java.io.InputStream JavaDoc;
14 import java.rmi.RemoteException JavaDoc;
15
16 /**
17  * The representation (mapping) in the JavaTM programming language of an
18  * SQL BLOB. An SQL BLOB is
19  * a built-in type that stores a Binary Large Object as a column value in
20  * a row of a database table. The
21  * driver implements Blob using an SQL locator(BLOB), which means that
22  * a Blob object contains a logical pointer to the SQL BLOB data rather
23  * than the data itself. A Blob object is valid for
24  * the duration of the transaction in which is was created.
25  *
26  * Methods in the interfaces ResultSet, CallableStatement, and
27  * PreparedStatement, such as getBlob and setBlob allow a programmer
28  * to access the SQL BLOB. The Blob interface provides methods for getting
29  * the length of an SQL BLOB (Binary Large Object) value, for
30  * materializing a BLOB value on the client, and for determining the position
31  * of a pattern of bytes within a BLOB value.
32  */

33
34 public class RJBlob implements java.sql.Blob JavaDoc, java.io.Serializable JavaDoc
35 {
36
37   RJBlobInterface rmiBlob_;
38
39   public RJBlob(RJBlobInterface b) {
40     rmiBlob_ = b;
41   }
42
43   public long length() throws SQLException {
44     try {
45       return rmiBlob_.length();
46     } catch(RemoteException JavaDoc e) {
47       throw new java.sql.SQLException JavaDoc(e.getMessage());
48     }
49   }
50
51   public byte[] getBytes(long pos, int length) throws SQLException {
52     try {
53       return rmiBlob_.getBytes(pos, length);
54     } catch(RemoteException JavaDoc e) {
55       throw new java.sql.SQLException JavaDoc(e.getMessage());
56     }
57   }
58
59 // TBD There's a hack there (InputStream not serializable)
60
public InputStream JavaDoc getBinaryStream() throws SQLException {
61     try {
62       byte[] val = rmiBlob_.getBinaryStream();
63       return RJSerializer.toInputStream(val);
64     } catch(Exception JavaDoc e) {
65       throw new java.sql.SQLException JavaDoc(e.getMessage());
66     }
67   }
68
69   public long position(byte[] pattern, long start) throws SQLException {
70     try {
71       return rmiBlob_.position(pattern, start);
72     } catch(RemoteException JavaDoc e) {
73       throw new java.sql.SQLException JavaDoc(e.getMessage());
74     }
75   }
76
77   public long position(Blob pattern, long start) throws SQLException {
78     try {
79       // Serialize the blob by calling its getBytes() method, then call the
80
// other position() method - the one that receives a byte array.
81
return rmiBlob_.position(pattern.getBytes(0, (int)pattern.length()), start);
82     } catch(RemoteException JavaDoc e) {
83       throw new java.sql.SQLException JavaDoc(e.getMessage());
84     }
85   }
86
87 // -------------------------- JDBC 3.0 -----------------------------------
88

89     public int setBytes(long pos, byte[] bytes) throws SQLException
90     {
91         try {
92           return rmiBlob_.setBytes(pos, bytes);
93         } catch(RemoteException JavaDoc e) {
94           throw new java.sql.SQLException JavaDoc(e.getMessage());
95         }
96     }
97     
98     public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException
99     {
100         try {
101           return rmiBlob_.setBytes(pos, bytes, offset, len);
102         } catch(RemoteException JavaDoc e) {
103           throw new java.sql.SQLException JavaDoc(e.getMessage());
104         }
105     }
106
107     public java.io.OutputStream JavaDoc setBinaryStream(long pos) throws SQLException
108     {
109         try {
110           return rmiBlob_.setBinaryStream(pos);
111         } catch(Exception JavaDoc e) {
112           throw new java.sql.SQLException JavaDoc(e.getMessage());
113         }
114     }
115         
116     public void truncate(long len) throws SQLException
117     {
118         try {
119           rmiBlob_.truncate(len);
120         } catch(RemoteException JavaDoc e) {
121           throw new java.sql.SQLException JavaDoc(e.getMessage());
122         }
123     }
124   
125 };
126
127
Popular Tags