KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > Blob


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 import java.sql.SQLException JavaDoc;
26
27
28 /**
29  * The representation (mapping) in the JavaTM programming language of an SQL
30  * BLOB value. An SQL BLOB is a built-in type that stores a Binary Large
31  * Object as a column value in a row of a database table. The driver
32  * implements Blob using an SQL locator(BLOB), which means that a Blob object
33  * contains a logical pointer to the SQL BLOB data rather than the data
34  * itself. A Blob object is valid for the duration of the transaction in
35  * which is was created. Methods in the interfaces ResultSet,
36  * CallableStatement, and PreparedStatement, such as getBlob and setBlob
37  * allow a programmer to access an SQL BLOB value. The Blob interface
38  * provides methods for getting the length of an SQL BLOB (Binary Large
39  * Object) value, for materializing a BLOB value on the client, and for
40  * determining the position of a pattern of bytes within a BLOB value. This
41  * class is new in the JDBC 2.0 API.
42  *
43  * @author Mark Matthews
44  *
45  * @version $Id: Blob.java,v 1.9.2.6 2003/12/24 05:16:26 mmatthew Exp $
46  */

47 public class Blob implements java.sql.Blob JavaDoc, OutputStreamWatcher {
48     //~ Instance fields --------------------------------------------------------
49

50     /** The ResultSet that created this BLOB */
51     private ResultSet creatorResultSet;
52
53     //
54
// This is a real brain-dead implementation of BLOB. Once I add
55
// streamability to the I/O for MySQL this will be more efficiently
56
// implemented (except for the position() method, ugh).
57
//
58

59     /** The binary data that makes up this BLOB */
60     private byte[] binaryData = null;
61
62     /** The column that this BLOB came from */
63     private int columnIndex;
64
65     //~ Constructors -----------------------------------------------------------
66

67     /**
68      * Creates a BLOB encapsulating the given binary data
69      */

70     Blob(byte[] data) {
71         setBinaryData(data);
72         this.creatorResultSet = null;
73         this.columnIndex = 0;
74     }
75
76     /**
77      * Creates an updatable BLOB that can update in-place
78      * (not implemented yet).
79      */

80     Blob(byte[] data, ResultSet creatorResultSet, int columnIndex) {
81         setBinaryData(data);
82         this.creatorResultSet = creatorResultSet;
83         this.columnIndex = columnIndex;
84     }
85
86     //~ Methods ----------------------------------------------------------------
87

88     /**
89      * @see Blob#setBinaryStream(long)
90      */

91     public OutputStream JavaDoc setBinaryStream(long indexToWriteAt)
92         throws SQLException JavaDoc {
93         if (indexToWriteAt < 1) {
94             throw new SQLException JavaDoc("indexToWriteAt must be >= 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
95         }
96
97         WatchableOutputStream bytesOut = new WatchableOutputStream();
98         bytesOut.setWatcher(this);
99
100         if (indexToWriteAt > 0) {
101             bytesOut.write(this.binaryData, 0, (int) (indexToWriteAt - 1));
102         }
103
104         return bytesOut;
105     }
106
107     /**
108      * Retrieves the BLOB designated by this Blob instance as a stream.
109      *
110      * @return this BLOB represented as a binary stream of bytes.
111      *
112      * @throws SQLException if a database error occurs
113      */

114     public java.io.InputStream JavaDoc getBinaryStream() throws SQLException JavaDoc {
115         return new ByteArrayInputStream JavaDoc(getBinaryData());
116     }
117
118     /**
119      * @see Blob#setBytes(long, byte[], int, int)
120      */

121     public int setBytes(long writeAt, byte[] bytes, int offset, int length)
122         throws SQLException JavaDoc {
123         OutputStream JavaDoc bytesOut = setBinaryStream(writeAt);
124
125         try {
126             bytesOut.write(bytes, offset, length);
127         } catch (IOException JavaDoc ioEx) {
128             throw new SQLException JavaDoc("IO Error while writing bytes to blob",
129                 SQLError.SQL_STATE_GENERAL_ERROR);
130         } finally {
131             try {
132                 bytesOut.close();
133             } catch (IOException JavaDoc doNothing) {
134                 ; // do nothing
135
}
136         }
137
138         return length;
139     }
140
141     /**
142      * @see Blob#setBytes(long, byte[])
143      */

144     public int setBytes(long writeAt, byte[] bytes) throws SQLException JavaDoc {
145         return setBytes(writeAt, bytes, 0, bytes.length);
146     }
147
148     /**
149      * Returns as an array of bytes, part or all of the BLOB value that this
150      * Blob object designates.
151      *
152      * @param pos where to start the part of the BLOB
153      * @param length the length of the part of the BLOB you want returned.
154      *
155      * @return the bytes stored in the blob starting at position
156      * <code>pos</code> and having a length of <code>length</code>.
157      *
158      * @throws SQLException if a database error occurs
159      */

160     public byte[] getBytes(long pos, int length) throws SQLException JavaDoc {
161         if (pos < 1) {
162             throw new SQLException JavaDoc("Position 'pos' can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
163         }
164
165         byte[] newData = new byte[length];
166         System.arraycopy(getBinaryData(), (int) (pos - 1), newData, 0, length);
167
168         return newData;
169     }
170
171     /**
172      * Returns the number of bytes in the BLOB value designated by this Blob
173      * object.
174      *
175      * @return the length of this blob
176      *
177      * @throws SQLException if a database error occurs
178      */

179     public long length() throws SQLException JavaDoc {
180         return getBinaryData().length;
181     }
182
183     /**
184      * Finds the position of the given pattern in this BLOB.
185      *
186      * @param pattern the pattern to find
187      * @param start where to start finding the pattern
188      *
189      * @return the position where the pattern is found in the BLOB, -1 if not
190      * found
191      *
192      * @throws SQLException if a database error occurs
193      */

194     public long position(java.sql.Blob JavaDoc pattern, long start)
195         throws SQLException JavaDoc {
196         return position(pattern.getBytes(0, (int) pattern.length()), start);
197     }
198
199     /**
200      * @see java.sql.Blob#position(byte[], long)
201      */

202     public long position(byte[] pattern, long start) throws SQLException JavaDoc {
203         throw new SQLException JavaDoc("Not implemented");
204     }
205
206     /**
207      * @see com.mysql.jdbc.OutputStreamWatcher#streamClosed(byte[])
208      */

209     public void streamClosed(byte[] byteData) {
210         this.binaryData = byteData;
211     }
212
213     /**
214      * @see Blob#truncate(long)
215      */

216     public void truncate(long arg0) throws SQLException JavaDoc {
217         throw new NotImplemented();
218     }
219
220     private void setBinaryData(byte[] binaryData) {
221         this.binaryData = binaryData;
222     }
223
224     private byte[] getBinaryData() {
225         return binaryData;
226     }
227 }
228
Popular Tags