1. /*
  2. * @(#)SerialBlob.java 1.8 04/05/29
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sql.rowset.serial;
  8. import java.sql.*;
  9. import java.io.*;
  10. import java.lang.reflect.*;
  11. /**
  12. * A serialized mapping in the Java programming language of an SQL
  13. * <code>BLOB</code> value.
  14. * <P>
  15. * The <code>SerialBlob</code> class provides a constructor for creating
  16. * an instance from a <code>Blob</code> object. Note that the
  17. * <code>Blob</code>
  18. * object should have brought the SQL <code>BLOB</code> value's data over
  19. * to the client before a <code>SerialBlob</code> object
  20. * is constructed from it. The data of an SQL <code>BLOB</code> value can
  21. * be materialized on the client as an array of bytes (using the method
  22. * <code>Blob.getBytes</code>) or as a stream of uninterpreted bytes
  23. * (using the method <code>Blob.getBinaryStream</code>).
  24. * <P>
  25. * <code>SerialBlob</code> methods make it possible to make a copy of a
  26. * <code>SerialBlob</code> object as an array of bytes or as a stream.
  27. * They also make it possible to locate a given pattern of bytes or a
  28. * <code>Blob</code> object within a <code>SerialBlob</code> object
  29. * and to update or truncate a <code>Blob</code> object.
  30. *
  31. * @author Jonathan Bruce
  32. */
  33. public class SerialBlob implements Blob, Serializable, Cloneable {
  34. /**
  35. * A serialized array of uninterpreted bytes representing the
  36. * value of this <code>SerialBlob</code> object.
  37. * @serial
  38. */
  39. private byte buf[];
  40. /**
  41. * The internal representation of the <code>Blob</code> object on which this
  42. * <code>SerialBlob</code> object is based.
  43. */
  44. private Blob blob;
  45. /**
  46. * The number of bytes in this <code>SerialBlob</code> object's
  47. * array of bytes.
  48. * @serial
  49. */
  50. private long len;
  51. /**
  52. * The orginal number of bytes in this <code>SerialBlob</code> object's
  53. * array of bytes when it was first established.
  54. * @serial
  55. */
  56. private long origLen;
  57. /**
  58. * Constructs a <code>SerialBlob</code> object that is a serialized version of
  59. * the given <code>byte</code> array.
  60. * <p>
  61. * The new <code>SerialBlob</code> object is initialized with the data from the
  62. * <code>byte</code> array, thus allowing disconnected <code>RowSet</code>
  63. * objects to establish serialized <code>Blob</code> objects without
  64. * touching the data source.
  65. *
  66. * @param b the <code>byte</code> array containing the data for the
  67. * <code>Blob</code> object to be serialized
  68. * @throws SerialException if an error occurs during serialization
  69. * @throws SQLException if a SQL errors occurs
  70. */
  71. public SerialBlob(byte[] b) throws SerialException, SQLException {
  72. len = b.length;
  73. buf = new byte[(int)len];
  74. for(int i = 0; i < len; i++) {
  75. buf[i] = b[i];
  76. }
  77. origLen = len;
  78. }
  79. /**
  80. * Constructs a <code>SerialBlob</code> object that is a serialized
  81. * version of the given <code>Blob</code> object.
  82. * <P>
  83. * The new <code>SerialBlob</code> object is initialized with the
  84. * data from the <code>Blob</code> object; therefore, the
  85. * <code>Blob</code> object should have previously brought the
  86. * SQL <code>BLOB</code> value's data over to the client from
  87. * the database. Otherwise, the new <code>SerialBlob</code> object
  88. * will contain no data.
  89. *
  90. * @param blob the <code>Blob</code> object from which this
  91. * <code>SerialBlob</code> object is to be constructed;
  92. * cannot be null.
  93. * @throws SerialException if an error occurs during serialization
  94. * @throws SQLException if the <code>Blob</code> passed to this
  95. * to this constructor is a <code>null</code>.
  96. * @see java.sql.Blob
  97. */
  98. public SerialBlob (Blob blob) throws SerialException, SQLException {
  99. if (blob == null) {
  100. throw new SQLException("Cannot instantiate a SerialBlob " +
  101. "object with a null Blob object");
  102. }
  103. len = blob.length();
  104. buf = blob.getBytes(1, (int)len );
  105. this.blob = blob;
  106. //if ( len < 10240000)
  107. // len = 10240000;
  108. origLen = len;
  109. }
  110. /**
  111. * Copies the specified number of bytes, starting at the given
  112. * position, from this <code>SerialBlob</code> object to
  113. * another array of bytes.
  114. * <P>
  115. * Note that if the given number of bytes to be copied is larger than
  116. * the length of this <code>SerialBlob</code> object's array of
  117. * bytes, the given number will be shortened to the array's length.
  118. *
  119. * @param pos the ordinal position of the first byte in this
  120. * <code>SerialBlob</code> object to be copied;
  121. * numbering starts at <code>1</code> must not be less
  122. * than <code>1</code> and must be less than or equal
  123. * to the length of this <code>SerialBlob</code> object
  124. * @param length the number of bytes to be copied
  125. * @return an array of bytes that is a copy of a region of this
  126. * <code>SerialBlob</code> object, starting at the given
  127. * position and containing the given number of consecutive bytes
  128. * @throws SerialException if the given starting position is out of bounds
  129. */
  130. public byte[] getBytes(long pos, int length) throws SerialException {
  131. if (length > len) {
  132. length = (int)len;
  133. }
  134. if (pos < 1 || length - pos < 0 ) {
  135. throw new SerialException("Invalid arguments: position cannot be less that 1");
  136. }
  137. pos--; // correct pos to array index
  138. byte[] b = new byte[length];
  139. for (int i = 0; i < length; i++) {
  140. b[i] = this.buf[(int)pos];
  141. pos++;
  142. }
  143. return b;
  144. }
  145. /**
  146. * Retrieves the number of bytes in this <code>SerialBlob</code>
  147. * object's array of bytes.
  148. *
  149. * @return a <code>long</code> indicating the length in bytes of this
  150. * <code>SerialBlob</code> object's array of bytes
  151. * @throws SerialException if an error occurs
  152. */
  153. public long length() throws SerialException {
  154. return len;
  155. }
  156. /**
  157. * Returns this <code>SerialBlob</code> object as an input stream.
  158. * Unlike the related method, <code>setBinaryStream</code>,
  159. * a stream is produced regardless of whether the <code>SerialBlob</code>
  160. * was created with a <code>Blob</code> object or a <code>byte</code> array.
  161. *
  162. * @return a <code>java.io.InputStream</code> object that contains
  163. * this <code>SerialBlob</code> object's array of bytes
  164. * @throws SerialException if an error occurs
  165. * @see #setBinaryStream
  166. */
  167. public java.io.InputStream getBinaryStream() throws SerialException {
  168. InputStream stream = new ByteArrayInputStream(buf);
  169. return (java.io.InputStream)stream;
  170. }
  171. /**
  172. * Returns the position in this <code>SerialBlob</code> object where
  173. * the given pattern of bytes begins, starting the search at the
  174. * specified position.
  175. *
  176. * @param pattern the pattern of bytes for which to search
  177. * @param start the position of the byte in this
  178. * <code>SerialBlob</code> object from which to begin
  179. * the search; the first position is <code>1</code>
  180. * must not be less than <code>1</code> nor greater than
  181. * the length of this <code>SerialBlob</code> object
  182. * @return the position in this <code>SerialBlob</code> object
  183. * where the given pattern begins, starting at the specified
  184. * position; <code>-1</code> if the pattern is not found
  185. * or the given starting position is out of bounds; position
  186. * numbering for the return value starts at <code>1</code>
  187. * @throws SerialException if an error occurs when serializing the blob
  188. * @throws SQLException if there is an error accessing the <code>BLOB</code>
  189. * value from the database
  190. */
  191. public long position(byte[] pattern, long start)
  192. throws SerialException, SQLException {
  193. if (start < 1 || start > len) {
  194. return -1;
  195. }
  196. int pos = (int)start-1; // internally Blobs are stored as arrays.
  197. int i = 0;
  198. long patlen = pattern.length;
  199. while (pos < len) {
  200. if (pattern[i] == buf[pos]) {
  201. if (i + 1 == patlen) {
  202. return (pos + 1) - (patlen - 1);
  203. }
  204. i++; pos++; // increment pos, and i
  205. } else if (pattern[i] != buf[pos]) {
  206. pos++; // increment pos only
  207. }
  208. }
  209. return -1; // not found
  210. }
  211. /**
  212. * Returns the position in this <code>SerialBlob</code> object where
  213. * the given <code>Blob</code> object begins, starting the search at the
  214. * specified position.
  215. *
  216. * @param pattern the <code>Blob</code> object for which to search;
  217. * @param start the position of the byte in this
  218. * <code>SerialBlob</code> object from which to begin
  219. * the search; the first position is <code>1</code>
  220. * must not be less than <code>1</code> nor greater than
  221. * the length of this <code>SerialBlob</code> object
  222. * @return the position in this <code>SerialBlob</code> object
  223. * where the given <code>Blob</code> object begins, starting
  224. * at the specified position; <code>-1</code> if the pattern is
  225. * not found or the given starting position is out of bounds;
  226. * position numbering for the return value starts at <code>1</code>
  227. * @throws SerialException if an error occurs when serializing the blob
  228. * @throws SQLException if there is an error accessing the <code>BLOB</code>
  229. * value from the database
  230. */
  231. public long position(Blob pattern, long start)
  232. throws SerialException, SQLException {
  233. return position(pattern.getBytes(1, (int)(pattern.length())), start);
  234. }
  235. /**
  236. * Writes the given array of bytes to the <code>BLOB</code> value that
  237. * this <code>Blob</code> object represents, starting at position
  238. * <code>pos</code>, and returns the number of bytes written.
  239. *
  240. * @param pos the position in the SQL <code>BLOB</code> value at which
  241. * to start writing. The first position is <code>1</code>
  242. * must not be less than <code>1</code> nor greater than
  243. * the length of this <code>SerialBlob</code> object.
  244. * @param bytes the array of bytes to be written to the <code>BLOB</code>
  245. * value that this <code>Blob</code> object represents
  246. * @return the number of bytes written
  247. * @throws SerialException if there is an error accessing the
  248. * <code>BLOB</code> value; or if an invalid position is set; if an
  249. * invalid offset value is set
  250. * @throws SQLException if there is an error accessing the <code>BLOB</code>
  251. * value from the database
  252. * @see #getBytes
  253. */
  254. public int setBytes(long pos, byte[] bytes)
  255. throws SerialException, SQLException {
  256. return (setBytes(pos, bytes, 0, bytes.length));
  257. }
  258. /**
  259. * Writes all or part of the given <code>byte</code> array to the
  260. * <code>BLOB</code> value that this <code>Blob</code> object represents
  261. * and returns the number of bytes written.
  262. * Writing starts at position <code>pos</code> in the <code>BLOB</code>
  263. * value; <i>len</i> bytes from the given byte array are written.
  264. *
  265. * @param pos the position in the <code>BLOB</code> object at which
  266. * to start writing. The first position is <code>1</code>
  267. * must not be less than <code>1</code> nor greater than
  268. * the length of this <code>SerialBlob</code> object.
  269. * @param bytes the array of bytes to be written to the <code>BLOB</code>
  270. * value
  271. * @param offset the offset in the <code>byte</code> array at which
  272. * to start reading the bytes. The first offset position is
  273. * <code>0</code> must not be less than <code>0</code> nor greater
  274. * than the length of the <code>byte</code> array
  275. * @param length the number of bytes to be written to the
  276. * <code>BLOB</code> value from the array of bytes <i>bytes</i>.
  277. *
  278. * @return the number of bytes written
  279. * @throws SerialException if there is an error accessing the
  280. * <code>BLOB</code> value; if an invalid position is set; if an
  281. * invalid offset value is set; if number of bytes to be written
  282. * is greater than the <code>SerialBlob</code> length; or the combined
  283. * values of the length and offset is greater than the Blob buffer
  284. * @throws SQLException if there is an error accessing the <code>BLOB</code>
  285. * value from the database.
  286. * @see #getBytes
  287. */
  288. public int setBytes(long pos, byte[] bytes, int offset, int length)
  289. throws SerialException, SQLException {
  290. if (offset < 0 || offset > bytes.length) {
  291. throw new SerialException("Invalid offset in byte array set");
  292. }
  293. if (pos < 1 || pos > this.length()) {
  294. throw new SerialException("Invalid position in BLOB object set");
  295. }
  296. if ((long)(length) > origLen) {
  297. throw new SerialException("Buffer is not sufficient to hold the value");
  298. }
  299. if ((length + offset) > bytes.length) {
  300. throw new SerialException("Invalid OffSet. Cannot have combined offset " +
  301. "and length that is greater that the Blob buffer");
  302. }
  303. int i = 0;
  304. pos--; // correct to array indexing
  305. while ( i < length || (offset + i +1) < (bytes.length-offset) ) {
  306. this.buf[(int)pos + i] = bytes[offset + i ];
  307. i++;
  308. }
  309. return i;
  310. }
  311. /**
  312. * Retrieves a stream that can be used to write to the <code>BLOB</code>
  313. * value that this <code>Blob</code> object represents. The stream begins
  314. * at position <code>pos</code>. This method forwards the
  315. * <code>setBinaryStream()</code> call to the underlying <code>Blob</code> in
  316. * the event that this <code>SerialBlob</code> object is instantiated with a
  317. * <code>Blob</code>. If this <code>SerialBlob</code> is instantiated with
  318. * a <code>byte</code> array, a <code>SerialException</code> is thrown.
  319. *
  320. * @param pos the position in the <code>BLOB</code> value at which
  321. * to start writing
  322. * @return a <code>java.io.OutputStream</code> object to which data can
  323. * be written
  324. * @throws SQLException if there is an error accessing the
  325. * <code>BLOB</code> value
  326. * @throws SerialException if the SerialBlob in not instantiated with a
  327. * <code>Blob</code> object that supports <code>setBinaryStream()</code>
  328. * @see #getBinaryStream
  329. */
  330. public java.io.OutputStream setBinaryStream(long pos)
  331. throws SerialException, SQLException {
  332. if (this.blob.setBinaryStream(pos) != null) {
  333. return this.blob.setBinaryStream(pos);
  334. } else {
  335. throw new SerialException("Unsupported operation. SerialBlob cannot " +
  336. "return a writable binary stream, unless instantiated with a Blob object " +
  337. "that provides a setBinaryStream() implementation");
  338. }
  339. }
  340. /**
  341. * Truncates the <code>BLOB</code> value that this <code>Blob</code>
  342. * object represents to be <code>len</code> bytes in length.
  343. *
  344. * @param length the length, in bytes, to which the <code>BLOB</code>
  345. * value that this <code>Blob</code> object represents should be
  346. * truncated
  347. * @throws SerialException if there is an error accessing the Blob value;
  348. * or the length to truncate is greater that the SerialBlob length
  349. */
  350. public void truncate(long length) throws SerialException {
  351. if (length > len) {
  352. throw new SerialException
  353. ("Length more than what can be truncated");
  354. } else if((int)length == 0) {
  355. buf = new byte[0];
  356. len = length;
  357. } else {
  358. len = length;
  359. buf = this.getBytes(1, (int)len);
  360. }
  361. }
  362. /**
  363. * The identifier that assists in the serialization of this <code>SerialBlob</code>
  364. * object.
  365. */
  366. static final long serialVersionUID = -8144641928112860441L;
  367. }