KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > in > co > daffodil > db > jdbc > DaffodilDBBlob


1 package in.co.daffodil.db.jdbc;
2
3 import java.io.*;
4 import java.sql.*;
5
6 /**
7  * The representation (mapping) in
8  * the Java<sup><font size=-2>TM</font></sup> programming
9  * language of an SQL
10  * <code>BLOB</code> value. An SQL <code>BLOB</code> is a built-in type
11  * that stores a Binary Large Object as a column value in a row of
12  * a database table. By default drivers implement <code>Blob</code> using
13  * an SQL <code>locator(BLOB)</code>, which means that a
14  * <code>Blob</code> object contains a logical pointer to the
15  * SQL <code>BLOB</code> data rather than the data itself.
16  * A <code>Blob</code> object is valid for the duration of the
17  * transaction in which is was created.
18  *
19  * <P>Methods in the interfaces {@link ResultSet},
20  * {@link CallableStatement}, and {@link PreparedStatement}, such as
21  * <code>getBlob</code> and <code>setBlob</code> allow a programmer to
22  * access an SQL <code>BLOB</code> value.
23  * The <code>Blob</code> interface provides methods for getting the
24  * length of an SQL <code>BLOB</code> (Binary Large Object) value,
25  * for materializing a <code>BLOB</code> value on the client, and for
26  * determining the position of a pattern of bytes within a
27  * <code>BLOB</code> value. In addition, this interface has methods for updating
28  * a <code>BLOB</code> value.
29  *
30  * @since 1.2
31  */

32
33 public class DaffodilDBBlob implements Blob,Serializable{
34     private Blob blob;
35
36
37     public DaffodilDBBlob(Blob blob){
38         this.blob = blob;
39     }
40
41     /**
42    * Returns the number of bytes in the <code>BLOB</code> value
43    * designated by this <code>Blob</code> object.
44    * @return length of the <code>BLOB</code> in bytes
45    * @exception SQLException if there is an error accessing the
46    * length of the <code>BLOB</code>
47    * @since 1.2
48    */

49     public long length() throws SQLException{
50         return blob.length();
51     }
52
53   /**
54    * Retrieves all or part of the <code>BLOB</code>
55    * value that this <code>Blob</code> object represents, as an array of
56    * bytes. This <code>byte</code> array contains up to <code>length</code>
57    * consecutive bytes starting at position <code>pos</code>.
58    *
59    * @param pos the ordinal position of the first byte in the
60    * <code>BLOB</code> value to be extracted; the first byte is at
61    * position 1
62    * @param length the number of consecutive bytes to be copied
63    * @return a byte array containing up to <code>length</code>
64    * consecutive bytes from the <code>BLOB</code> value designated
65    * by this <code>Blob</code> object, starting with the
66    * byte at position <code>pos</code>
67    * @exception SQLException if there is an error accessing the
68    * <code>BLOB</code> value
69    * @see #setBytes
70    * @since 1.2
71    */

72     public byte[] getBytes(long pos, int length) throws SQLException{
73         return blob.getBytes(pos,length);
74     }
75
76   /**
77    * Retrieves the <code>BLOB</code> value designated by this
78    * <code>Blob</code> instance as a stream.
79    *
80    * @return a stream containing the <code>BLOB</code> data
81    * @exception SQLException if there is an error accessing the
82    * <code>BLOB</code> value
83    * @see #setBinaryStream
84    * @since 1.2
85    */

86     public java.io.InputStream JavaDoc getBinaryStream() throws SQLException{
87         return blob.getBinaryStream();
88    }
89
90   /**
91    * Retrieves the byte position at which the specified byte array
92    * <code>pattern</code> begins within the <code>BLOB</code>
93    * value that this <code>Blob</code> object represents. The
94    * search for <code>pattern</code> begins at position
95    * <code>start</code>.
96    *
97    * @param pattern the byte array for which to search
98    * @param start the position at which to begin searching; the
99    * first position is 1
100    * @return the position at which the pattern appears, else -1
101    * @exception SQLException if there is an error accessing the
102    * <code>BLOB</code>
103    * @since 1.2
104    */

105     public long position(byte pattern[], long start) throws SQLException{
106         return blob.position(pattern,start);
107     }
108
109   /**
110    * Retrieves the byte position in the <code>BLOB</code> value
111    * designated by this <code>Blob</code> object at which
112    * <code>pattern</code> begins. The search begins at position
113    * <code>start</code>.
114    *
115    * @param pattern the <code>Blob</code> object designating
116    * the <code>BLOB</code> value for which to search
117    * @param start the position in the <code>BLOB</code> value
118    * at which to begin searching; the first position is 1
119    * @return the position at which the pattern begins, else -1
120    * @exception SQLException if there is an error accessing the
121    * <code>BLOB</code> value
122    * @since 1.2
123    */

124     public long position(Blob pattern, long start) throws SQLException{
125         return blob.position(pattern,start);
126     }
127
128
129     /**
130      * Writes the given array of bytes to the <code>BLOB</code> value that
131      * this <code>Blob</code> object represents, starting at position
132      * <code>pos</code>, and returns the number of bytes written.
133      *
134      * @param pos the position in the <code>BLOB</code> object at which
135      * to start writing
136      * @param bytes the array of bytes to be written to the <code>BLOB</code>
137      * value that this <code>Blob</code> object represents
138      * @return the number of bytes written
139      * @exception SQLException if there is an error accessing the
140      * <code>BLOB</code> value
141      * @see #getBytes
142      * @since 1.4
143      */

144     public int setBytes(long pos, byte[] bytes) throws SQLException{
145        return blob.setBytes(pos,bytes);
146     }
147
148     /**
149      * Writes all or part of the given <code>byte</code> array to the
150      * <code>BLOB</code> value that this <code>Blob</code> object represents
151      * and returns the number of bytes written.
152      * Writing starts at position <code>pos</code> in the <code>BLOB</code>
153      * value; <code>len</code> bytes from the given byte array are written.
154      *
155      * @param pos the position in the <code>BLOB</code> object at which
156      * to start writing
157      * @param bytes the array of bytes to be written to this <code>BLOB</code>
158      * object
159      * @param offset the offset into the array <code>bytes</code> at which
160      * to start reading the bytes to be set
161      * @param len the number of bytes to be written to the <code>BLOB</code>
162      * value from the array of bytes <code>bytes</code>
163      * @return the number of bytes written
164      * @exception SQLException if there is an error accessing the
165      * <code>BLOB</code> value
166      * @see #getBytes
167      * @since 1.4
168      */

169     public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException{
170        return blob.setBytes(pos,bytes,offset,len);
171     }
172
173     /**
174      * Retrieves a stream that can be used to write to the <code>BLOB</code>
175      * value that this <code>Blob</code> object represents. The stream begins
176      * at position <code>pos</code>.
177      *
178      * @param pos the position in the <code>BLOB</code> value at which
179      * to start writing
180      * @return a <code>java.io.OutputStream</code> object to which data can
181      * be written
182      * @exception SQLException if there is an error accessing the
183      * <code>BLOB</code> value
184      * @see #getBinaryStream
185      * @since 1.4
186      */

187     public java.io.OutputStream JavaDoc setBinaryStream(long pos) throws SQLException{
188       return blob.setBinaryStream(pos);
189     }
190
191     /**
192      * Truncates the <code>BLOB</code> value that this <code>Blob</code>
193      * object represents to be <code>len</code> bytes in length.
194      *
195      * @param len the length, in bytes, to which the <code>BLOB</code> value
196      * that this <code>Blob</code> object represents should be truncated
197      * @exception SQLException if there is an error accessing the
198      * <code>BLOB</code> value
199      * @since 1.4
200      */

201     public void truncate(long len) throws SQLException{
202         blob.truncate(len);;
203     }
204 }
205
Popular Tags