| 1 | /* |
| 2 | Copyright (C) 2002-2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 25 | package com.mysql.jdbc; |
| 26 | |
| 27 | import java.io.ByteArrayInputStream; |
| 28 | import java.io.IOException; |
| 29 | import java.io.OutputStream; |
| 30 | |
| 31 | import java.sql.SQLException; |
| 32 | |
| 33 | /** |
| 34 | * The representation (mapping) in the JavaTM programming language of an SQL |
| 35 | * BLOB value. An SQL BLOB is a built-in type that stores a Binary Large Object |
| 36 | * as a column value in a row of a database table. The driver implements Blob |
| 37 | * using an SQL locator(BLOB), which means that a Blob object contains a logical |
| 38 | * pointer to the SQL BLOB data rather than the data itself. A Blob object is |
| 39 | * valid for the duration of the transaction in which is was created. Methods in |
| 40 | * the interfaces ResultSet, CallableStatement, and PreparedStatement, such as |
| 41 | * getBlob and setBlob allow a programmer to access an SQL BLOB value. The Blob |
| 42 | * interface provides methods for getting the length of an SQL BLOB (Binary |
| 43 | * Large Object) value, for materializing a BLOB value on the client, and for |
| 44 | * determining the position of a pattern of bytes within a BLOB value. This |
| 45 | * class is new in the JDBC 2.0 API. |
| 46 | * |
| 47 | * @author Mark Matthews |
| 48 | * @version $Id: Blob.java 4489 2005-10-31 18:43:01 -0600 (Mon, 31 Oct 2005) mmatthews $ |
| 49 | */ |
| 50 | public class Blob implements java.sql.Blob, OutputStreamWatcher { |
| 51 | |
| 52 | // |
| 53 | // This is a real brain-dead implementation of BLOB. Once I add |
| 54 | // streamability to the I/O for MySQL this will be more efficiently |
| 55 | // implemented (except for the position() method, ugh). |
| 56 | // |
| 57 | |
| 58 | /** The binary data that makes up this BLOB */ |
| 59 | private byte[] binaryData = null; |
| 60 | |
| 61 | /** |
| 62 | * Creates a BLOB encapsulating the given binary data |
| 63 | * |
| 64 | * @param data |
| 65 | * DOCUMENT ME! |
| 66 | */ |
| 67 | Blob(byte[] data) { |
| 68 | setBinaryData(data); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates an updatable BLOB that can update in-place (not implemented yet). |
| 73 | * |
| 74 | * @param data |
| 75 | * DOCUMENT ME! |
| 76 | * @param creatorResultSetToSet |
| 77 | * DOCUMENT ME! |
| 78 | * @param columnIndexToSet |
| 79 | * DOCUMENT ME! |
| 80 | */ |
| 81 | Blob(byte[] data, ResultSet creatorResultSetToSet, int columnIndexToSet) { |
| 82 | setBinaryData(data); |
| 83 | } |
| 84 | |
| 85 | private byte[] getBinaryData() { |
| 86 | return this.binaryData; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Retrieves the BLOB designated by this Blob instance as a stream. |
| 91 | * |
| 92 | * @return this BLOB represented as a binary stream of bytes. |
| 93 | * |
| 94 | * @throws SQLException |
| 95 | * if a database error occurs |
| 96 | */ |
| 97 | public java.io.InputStream getBinaryStream() throws SQLException { |
| 98 | return new ByteArrayInputStream(getBinaryData()); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns as an array of bytes, part or all of the BLOB value that this |
| 103 | * Blob object designates. |
| 104 | * |
| 105 | * @param pos |
| 106 | * where to start the part of the BLOB |
| 107 | * @param length |
| 108 | * the length of the part of the BLOB you want returned. |
| 109 | * |
| 110 | * @return the bytes stored in the blob starting at position |
| 111 | * <code>pos</code> and having a length of <code>length</code>. |
| 112 | * |
| 113 | * @throws SQLException |
| 114 | * if a database error occurs |
| 115 | */ |
| 116 | public byte[] getBytes(long pos, int length) throws SQLException { |
| 117 | if (pos < 1) { |
| 118 | throw SQLError.createSQLException(Messages.getString("Blob.2"), //$NON-NLS-1$ |
| 119 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 120 | } |
| 121 | |
| 122 | byte[] newData = new byte[length]; |
| 123 | System.arraycopy(getBinaryData(), (int) (pos - 1), newData, 0, length); |
| 124 | |
| 125 | return newData; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the number of bytes in the BLOB value designated by this Blob |
| 130 | * object. |
| 131 | * |
| 132 | * @return the length of this blob |
| 133 | * |
| 134 | * @throws SQLException |
| 135 | * if a database error occurs |
| 136 | */ |
| 137 | public long length() throws SQLException { |
| 138 | return getBinaryData().length; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @see java.sql.Blob#position(byte[], long) |
| 143 | */ |
| 144 | public long position(byte[] pattern, long start) throws SQLException { |
| 145 | throw SQLError.createSQLException("Not implemented"); //$NON-NLS-1$ |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Finds the position of the given pattern in this BLOB. |
| 150 | * |
| 151 | * @param pattern |
| 152 | * the pattern to find |
| 153 | * @param start |
| 154 | * where to start finding the pattern |
| 155 | * |
| 156 | * @return the position where the pattern is found in the BLOB, -1 if not |
| 157 | * found |
| 158 | * |
| 159 | * @throws SQLException |
| 160 | * if a database error occurs |
| 161 | */ |
| 162 | public long position(java.sql.Blob pattern, long start) throws SQLException { |
| 163 | return position(pattern.getBytes(0, (int) pattern.length()), start); |
| 164 | } |
| 165 | |
| 166 | private void setBinaryData(byte[] newBinaryData) { |
| 167 | this.binaryData = newBinaryData; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @see Blob#setBinaryStream(long) |
| 172 | */ |
| 173 | public OutputStream setBinaryStream(long indexToWriteAt) |
| 174 | throws SQLException { |
| 175 | if (indexToWriteAt < 1) { |
| 176 | throw SQLError.createSQLException(Messages.getString("Blob.0"), //$NON-NLS-1$ |
| 177 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 178 | } |
| 179 | |
| 180 | WatchableOutputStream bytesOut = new WatchableOutputStream(); |
| 181 | bytesOut.setWatcher(this); |
| 182 | |
| 183 | if (indexToWriteAt > 0) { |
| 184 | bytesOut.write(this.binaryData, 0, (int) (indexToWriteAt - 1)); |
| 185 | } |
| 186 | |
| 187 | return bytesOut; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @see Blob#setBytes(long, byte[]) |
| 192 | */ |
| 193 | public int setBytes(long writeAt, byte[] bytes) throws SQLException { |
| 194 | return setBytes(writeAt, bytes, 0, bytes.length); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @see Blob#setBytes(long, byte[], int, int) |
| 199 | */ |
| 200 | public int setBytes(long writeAt, byte[] bytes, int offset, int length) |
| 201 | throws SQLException { |
| 202 | OutputStream bytesOut = setBinaryStream(writeAt); |
| 203 | |
| 204 | try { |
| 205 | bytesOut.write(bytes, offset, length); |
| 206 | } catch (IOException ioEx) { |
| 207 | throw SQLError.createSQLException(Messages.getString("Blob.1"), //$NON-NLS-1$ |
| 208 | SQLError.SQL_STATE_GENERAL_ERROR); |
| 209 | } finally { |
| 210 | try { |
| 211 | bytesOut.close(); |
| 212 | } catch (IOException doNothing) { |
| 213 | ; // do nothing |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return length; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @see com.mysql.jdbc.OutputStreamWatcher#streamClosed(byte[]) |
| 222 | */ |
| 223 | public void streamClosed(byte[] byteData) { |
| 224 | this.binaryData = byteData; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @see com.mysql.jdbc.OutputStreamWatcher#streamClosed(byte[]) |
| 229 | */ |
| 230 | public void streamClosed(WatchableOutputStream out) { |
| 231 | int streamSize = out.size(); |
| 232 | |
| 233 | if (streamSize < this.binaryData.length) { |
| 234 | out.write(this.binaryData, streamSize, this.binaryData.length |
| 235 | - streamSize); |
| 236 | } |
| 237 | |
| 238 | this.binaryData = out.toByteArray(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * @see Blob#truncate(long) |
| 243 | */ |
| 244 | public void truncate(long arg0) throws SQLException { |
| 245 | throw new NotImplemented(); |
| 246 | } |
| 247 | } |