1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sql.rowset.serial;
  27 
  28 import java.sql.*;
  29 import java.io.*;
  30 import java.lang.reflect.*;
  31 import java.util.Arrays;
  32 
  33 
  34 /**
  35  * A serialized mapping in the Java programming language of an SQL
  36  * <code>BLOB</code> value.
  37  * <P>
  38  * The <code>SerialBlob</code> class provides a constructor for creating
  39  * an instance from a <code>Blob</code> object.  Note that the
  40  * <code>Blob</code>
  41  * object should have brought the SQL <code>BLOB</code> value's data over
  42  * to the client before a <code>SerialBlob</code> object
  43  * is constructed from it.  The data of an SQL <code>BLOB</code> value can
  44  * be materialized on the client as an array of bytes (using the method
  45  * <code>Blob.getBytes</code>) or as a stream of uninterpreted bytes
  46  * (using the method <code>Blob.getBinaryStream</code>).
  47  * <P>
  48  * <code>SerialBlob</code> methods make it possible to make a copy of a
  49  * <code>SerialBlob</code> object as an array of bytes or as a stream.
  50  * They also make it possible to locate a given pattern of bytes or a
  51  * <code>Blob</code> object within a <code>SerialBlob</code> object
  52  * and to update or truncate a <code>Blob</code> object.
  53  *
  54  * <h3> Thread safety </h3>
  55  *
  56  * <p> A SerialBlob is not safe for use by multiple concurrent threads.  If a
  57  * SerialBlob is to be used by more than one thread then access to the SerialBlob
  58  * should be controlled by appropriate synchronization.
  59  *
  60  * @author Jonathan Bruce
  61  * @since 1.5
  62  */
  63 public class SerialBlob implements Blob, Serializable, Cloneable {
  64 
  65     /**
  66      * A serialized array of uninterpreted bytes representing the
  67      * value of this <code>SerialBlob</code> object.
  68      * @serial
  69      */
  70     private byte buf[];
  71 
  72     /**
  73      * The internal representation of the <code>Blob</code> object on which this
  74      * <code>SerialBlob</code> object is based.
  75      */
  76     private Blob blob;
  77 
  78     /**
  79      * The number of bytes in this <code>SerialBlob</code> object's
  80      * array of bytes.
  81      * @serial
  82      */
  83     private long len;
  84 
  85     /**
  86      * The original number of bytes in this <code>SerialBlob</code> object's
  87      * array of bytes when it was first established.
  88      * @serial
  89      */
  90     private long origLen;
  91 
  92     /**
  93      * Constructs a <code>SerialBlob</code> object that is a serialized version of
  94      * the given <code>byte</code> array.
  95      * <p>
  96      * The new <code>SerialBlob</code> object is initialized with the data from the
  97      * <code>byte</code> array, thus allowing disconnected <code>RowSet</code>
  98      * objects to establish serialized <code>Blob</code> objects without
  99      * touching the data source.
 100      *
 101      * @param b the <code>byte</code> array containing the data for the
 102      *        <code>Blob</code> object to be serialized
 103      * @throws SerialException if an error occurs during serialization
 104      * @throws SQLException if a SQL errors occurs
 105      */
 106     public SerialBlob(byte[] b) throws SerialException, SQLException {
 107 
 108         len = b.length;
 109         buf = new byte[(int)len];
 110         for(int i = 0; i < len; i++) {
 111            buf[i] = b[i];
 112         }
 113         origLen = len;
 114     }
 115 
 116 
 117     /**
 118      * Constructs a <code>SerialBlob</code> object that is a serialized
 119      * version of the given <code>Blob</code> object.
 120      * <P>
 121      * The new <code>SerialBlob</code> object is initialized with the
 122      * data from the <code>Blob</code> object; therefore, the
 123      * <code>Blob</code> object should have previously brought the
 124      * SQL <code>BLOB</code> value's data over to the client from
 125      * the database. Otherwise, the new <code>SerialBlob</code> object
 126      * will contain no data.
 127      *
 128      * @param blob the <code>Blob</code> object from which this
 129      *     <code>SerialBlob</code> object is to be constructed;
 130      *     cannot be null.
 131      * @throws SerialException if an error occurs during serialization
 132      * @throws SQLException if the <code>Blob</code> passed to this
 133      *     to this constructor is a <code>null</code>.
 134      * @see java.sql.Blob
 135      */
 136     public SerialBlob (Blob blob) throws SerialException, SQLException {
 137 
 138         if (blob == null) {
 139             throw new SQLException("Cannot instantiate a SerialBlob " +
 140                  "object with a null Blob object");
 141         }
 142 
 143         len = blob.length();
 144         buf = blob.getBytes(1, (int)len );
 145         this.blob = blob;
 146 
 147          //if ( len < 10240000)
 148          // len = 10240000;
 149         origLen = len;
 150     }
 151 
 152     /**
 153      * Copies the specified number of bytes, starting at the given
 154      * position, from this <code>SerialBlob</code> object to
 155      * another array of bytes.
 156      * <P>
 157      * Note that if the given number of bytes to be copied is larger than
 158      * the length of this <code>SerialBlob</code> object's array of
 159      * bytes, the given number will be shortened to the array's length.
 160      *
 161      * @param pos the ordinal position of the first byte in this
 162      *            <code>SerialBlob</code> object to be copied;
 163      *            numbering starts at <code>1</code>; must not be less
 164      *            than <code>1</code> and must be less than or equal
 165      *            to the length of this <code>SerialBlob</code> object
 166      * @param length the number of bytes to be copied
 167      * @return an array of bytes that is a copy of a region of this
 168      *         <code>SerialBlob</code> object, starting at the given
 169      *         position and containing the given number of consecutive bytes
 170      * @throws SerialException if the given starting position is out of bounds;
 171      * if {@code free} had previously been called on this object
 172      */
 173     public byte[] getBytes(long pos, int length) throws SerialException {
 174         isValid();
 175         if (length > len) {
 176             length = (int)len;
 177         }
 178 
 179         if (pos < 1 || len - pos < 0 ) {
 180             throw new SerialException("Invalid arguments: position cannot be "
 181                     + "less than 1 or greater than the length of the SerialBlob");
 182         }
 183 
 184         pos--; // correct pos to array index
 185 
 186         byte[] b = new byte[length];
 187 
 188         for (int i = 0; i < length; i++) {
 189             b[i] = this.buf[(int)pos];
 190             pos++;
 191         }
 192         return b;
 193     }
 194 
 195     /**
 196      * Retrieves the number of bytes in this <code>SerialBlob</code>
 197      * object's array of bytes.
 198      *
 199      * @return a <code>long</code> indicating the length in bytes of this
 200      *         <code>SerialBlob</code> object's array of bytes
 201      * @throws SerialException if an error occurs;
 202      * if {@code free} had previously been called on this object
 203      */
 204     public long length() throws SerialException {
 205         isValid();
 206         return len;
 207     }
 208 
 209     /**
 210      * Returns this <code>SerialBlob</code> object as an input stream.
 211      * Unlike the related method, <code>setBinaryStream</code>,
 212      * a stream is produced regardless of whether the <code>SerialBlob</code>
 213      * was created with a <code>Blob</code> object or a <code>byte</code> array.
 214      *
 215      * @return a <code>java.io.InputStream</code> object that contains
 216      *         this <code>SerialBlob</code> object's array of bytes
 217      * @throws SerialException if an error occurs;
 218      * if {@code free} had previously been called on this object
 219      * @see #setBinaryStream
 220      */
 221     public java.io.InputStream getBinaryStream() throws SerialException {
 222         isValid();
 223         InputStream stream = new ByteArrayInputStream(buf);
 224         return stream;
 225     }
 226 
 227     /**
 228      * Returns the position in this <code>SerialBlob</code> object where
 229      * the given pattern of bytes begins, starting the search at the
 230      * specified position.
 231      *
 232      * @param pattern the pattern of bytes for which to search
 233      * @param start the position of the byte in this
 234      *              <code>SerialBlob</code> object from which to begin
 235      *              the search; the first position is <code>1</code>;
 236      *              must not be less than <code>1</code> nor greater than
 237      *              the length of this <code>SerialBlob</code> object
 238      * @return the position in this <code>SerialBlob</code> object
 239      *         where the given pattern begins, starting at the specified
 240      *         position; <code>-1</code> if the pattern is not found
 241      *         or the given starting position is out of bounds; position
 242      *         numbering for the return value starts at <code>1</code>
 243      * @throws SerialException if an error occurs when serializing the blob;
 244      * if {@code free} had previously been called on this object
 245      * @throws SQLException if there is an error accessing the <code>BLOB</code>
 246      *         value from the database
 247      */
 248     public long position(byte[] pattern, long start)
 249                 throws SerialException, SQLException {
 250         isValid();
 251         if (start < 1 || start > len) {
 252             return -1;
 253         }
 254 
 255         int pos = (int)start-1; // internally Blobs are stored as arrays.
 256         int i = 0;
 257         long patlen = pattern.length;
 258 
 259         while (pos < len) {
 260             if (pattern[i] == buf[pos]) {
 261                 if (i + 1 == patlen) {
 262                     return (pos + 1) - (patlen - 1);
 263                 }
 264                 i++; pos++; // increment pos, and i
 265             } else if (pattern[i] != buf[pos]) {
 266                 pos++; // increment pos only
 267             }
 268         }
 269         return -1; // not found
 270     }
 271 
 272     /**
 273      * Returns the position in this <code>SerialBlob</code> object where
 274      * the given <code>Blob</code> object begins, starting the search at the
 275      * specified position.
 276      *
 277      * @param pattern the <code>Blob</code> object for which to search;
 278      * @param start the position of the byte in this
 279      *              <code>SerialBlob</code> object from which to begin
 280      *              the search; the first position is <code>1</code>;
 281      *              must not be less than <code>1</code> nor greater than
 282      *              the length of this <code>SerialBlob</code> object
 283      * @return the position in this <code>SerialBlob</code> object
 284      *         where the given <code>Blob</code> object begins, starting
 285      *         at the specified position; <code>-1</code> if the pattern is
 286      *         not found or the given starting position is out of bounds;
 287      *         position numbering for the return value starts at <code>1</code>
 288      * @throws SerialException if an error occurs when serializing the blob;
 289      * if {@code free} had previously been called on this object
 290      * @throws SQLException if there is an error accessing the <code>BLOB</code>
 291      *         value from the database
 292      */
 293     public long position(Blob pattern, long start)
 294        throws SerialException, SQLException {
 295         isValid();
 296         return position(pattern.getBytes(1, (int)(pattern.length())), start);
 297     }
 298 
 299     /**
 300      * Writes the given array of bytes to the <code>BLOB</code> value that
 301      * this <code>Blob</code> object represents, starting at position
 302      * <code>pos</code>, and returns the number of bytes written.
 303      *
 304      * @param pos the position in the SQL <code>BLOB</code> value at which
 305      *     to start writing. The first position is <code>1</code>;
 306      *     must not be less than <code>1</code> nor greater than
 307      *     the length of this <code>SerialBlob</code> object.
 308      * @param bytes the array of bytes to be written to the <code>BLOB</code>
 309      *        value that this <code>Blob</code> object represents
 310      * @return the number of bytes written
 311      * @throws SerialException if there is an error accessing the
 312      *     <code>BLOB</code> value; or if an invalid position is set; if an
 313      *     invalid offset value is set;
 314      * if {@code free} had previously been called on this object
 315      * @throws SQLException if there is an error accessing the <code>BLOB</code>
 316      *         value from the database
 317      * @see #getBytes
 318      */
 319     public int setBytes(long pos, byte[] bytes)
 320         throws SerialException, SQLException {
 321         return (setBytes(pos, bytes, 0, bytes.length));
 322     }
 323 
 324     /**
 325      * Writes all or part of the given <code>byte</code> array to the
 326      * <code>BLOB</code> value that this <code>Blob</code> object represents
 327      * and returns the number of bytes written.
 328      * Writing starts at position <code>pos</code> in the <code>BLOB</code>
 329      * value; <i>len</i> bytes from the given byte array are written.
 330      *
 331      * @param pos the position in the <code>BLOB</code> object at which
 332      *     to start writing. The first position is <code>1</code>;
 333      *     must not be less than <code>1</code> nor greater than
 334      *     the length of this <code>SerialBlob</code> object.
 335      * @param bytes the array of bytes to be written to the <code>BLOB</code>
 336      *     value
 337      * @param offset the offset in the <code>byte</code> array at which
 338      *     to start reading the bytes. The first offset position is
 339      *     <code>0</code>; must not be less than <code>0</code> nor greater
 340      *     than the length of the <code>byte</code> array
 341      * @param length the number of bytes to be written to the
 342      *     <code>BLOB</code> value from the array of bytes <i>bytes</i>.
 343      *
 344      * @return the number of bytes written
 345      * @throws SerialException if there is an error accessing the
 346      *     <code>BLOB</code> value; if an invalid position is set; if an
 347      *     invalid offset value is set; if number of bytes to be written
 348      *     is greater than the <code>SerialBlob</code> length; or the combined
 349      *     values of the length and offset is greater than the Blob buffer;
 350      * if {@code free} had previously been called on this object
 351      * @throws SQLException if there is an error accessing the <code>BLOB</code>
 352      *         value from the database.
 353      * @see #getBytes
 354      */
 355     public int setBytes(long pos, byte[] bytes, int offset, int length)
 356         throws SerialException, SQLException {
 357 
 358         isValid();
 359         if (offset < 0 || offset > bytes.length) {
 360             throw new SerialException("Invalid offset in byte array set");
 361         }
 362 
 363         if (pos < 1 || pos > this.length()) {
 364             throw new SerialException("Invalid position in BLOB object set");
 365         }
 366 
 367         if ((long)(length) > origLen) {
 368             throw new SerialException("Buffer is not sufficient to hold the value");
 369         }
 370 
 371         if ((length + offset) > bytes.length) {
 372             throw new SerialException("Invalid OffSet. Cannot have combined offset " +
 373                 "and length that is greater that the Blob buffer");
 374         }
 375 
 376         int i = 0;
 377         pos--; // correct to array indexing
 378         while ( i < length || (offset + i +1) < (bytes.length-offset) ) {
 379             this.buf[(int)pos + i] = bytes[offset + i ];
 380             i++;
 381         }
 382         return i;
 383     }
 384 
 385     /**
 386      * Retrieves a stream that can be used to write to the <code>BLOB</code>
 387      * value that this <code>Blob</code> object represents.  The stream begins
 388      * at position <code>pos</code>. This method forwards the
 389      * <code>setBinaryStream()</code> call to the underlying <code>Blob</code> in
 390      * the event that this <code>SerialBlob</code> object is instantiated with a
 391      * <code>Blob</code>. If this <code>SerialBlob</code> is instantiated with
 392      * a <code>byte</code> array, a <code>SerialException</code> is thrown.
 393      *
 394      * @param pos the position in the <code>BLOB</code> value at which
 395      *        to start writing
 396      * @return a <code>java.io.OutputStream</code> object to which data can
 397      *         be written
 398      * @throws SQLException if there is an error accessing the
 399      *            <code>BLOB</code> value
 400      * @throws SerialException if the SerialBlob in not instantiated with a
 401      *     <code>Blob</code> object that supports <code>setBinaryStream()</code>;
 402      * if {@code free} had previously been called on this object
 403      * @see #getBinaryStream
 404      */
 405     public java.io.OutputStream setBinaryStream(long pos)
 406         throws SerialException, SQLException {
 407         isValid();
 408         if (this.blob != null) {
 409             return this.blob.setBinaryStream(pos);
 410         } else {
 411             throw new SerialException("Unsupported operation. SerialBlob cannot " +
 412                 "return a writable binary stream, unless instantiated with a Blob object " +
 413                 "that provides a setBinaryStream() implementation");
 414         }
 415     }
 416 
 417     /**
 418      * Truncates the <code>BLOB</code> value that this <code>Blob</code>
 419      * object represents to be <code>len</code> bytes in length.
 420      *
 421      * @param length the length, in bytes, to which the <code>BLOB</code>
 422      *        value that this <code>Blob</code> object represents should be
 423      *        truncated
 424      * @throws SerialException if there is an error accessing the Blob value;
 425      *     or the length to truncate is greater that the SerialBlob length;
 426      * if {@code free} had previously been called on this object
 427      */
 428     public void truncate(long length) throws SerialException {
 429 
 430         isValid();
 431         if (length > len) {
 432            throw new SerialException
 433               ("Length more than what can be truncated");
 434         } else if((int)length == 0) {
 435              buf = new byte[0];
 436              len = length;
 437         } else {
 438              len = length;
 439              buf = this.getBytes(1, (int)len);
 440         }
 441     }
 442 
 443 
 444     /**
 445      * Returns an
 446      * <code>InputStream</code> object that contains a partial
 447      * {@code Blob} value, starting with the byte specified by pos, which is
 448      * length bytes in length.
 449      *
 450      * @param pos the offset to the first byte of the partial value to be
 451      * retrieved. The first byte in the {@code Blob} is at position 1
 452      * @param length the length in bytes of the partial value to be retrieved
 453      * @return
 454      * <code>InputStream</code> through which the partial {@code Blob} value can
 455      * be read.
 456      * @throws SQLException if pos is less than 1 or if pos is greater than the
 457      * number of bytes in the {@code Blob} or if pos + length is greater than
 458      * the number of bytes in the {@code Blob}
 459      * @throws SerialException if the {@code free} method had been previously
 460      * called on this object
 461      *
 462      * @since 1.6
 463      */
 464     public InputStream getBinaryStream(long pos, long length) throws SQLException {
 465         isValid();
 466         if (pos < 1 || pos > this.length()) {
 467             throw new SerialException("Invalid position in BLOB object set");
 468         }
 469         if (length < 1 || length > len - pos + 1) {
 470             throw new SerialException("length is < 1 or pos + length >"
 471                     + "total number of bytes");
 472         }
 473         return new ByteArrayInputStream(buf, (int) pos - 1, (int) length);
 474     }
 475 
 476 
 477     /**
 478      * This method frees the {@code SerialBlob} object and releases the
 479      * resources that it holds. The object is invalid once the {@code free}
 480      * method is called. <p> If {@code free} is called multiple times, the
 481      * subsequent calls to {@code free} are treated as a no-op. </P>
 482      *
 483      * @throws SQLException if an error occurs releasing the Blob's resources
 484      * @since 1.6
 485      */
 486     public void free() throws SQLException {
 487         if (buf != null) {
 488             buf = null;
 489             if (blob != null) {
 490                 blob.free();
 491             }
 492             blob = null;
 493         }
 494     }
 495 
 496     /**
 497      * Compares this SerialBlob to the specified object.  The result is {@code
 498      * true} if and only if the argument is not {@code null} and is a {@code
 499      * SerialBlob} object that represents the same sequence of bytes as this
 500      * object.
 501      *
 502      * @param  obj The object to compare this {@code SerialBlob} against
 503      *
 504      * @return {@code true} if the given object represents a {@code SerialBlob}
 505      *          equivalent to this SerialBlob, {@code false} otherwise
 506      *
 507      */
 508     public boolean equals(Object obj) {
 509         if (this == obj) {
 510             return true;
 511         }
 512         if (obj instanceof SerialBlob) {
 513             SerialBlob sb = (SerialBlob)obj;
 514             if (this.len == sb.len) {
 515                 return Arrays.equals(buf, sb.buf);
 516             }
 517         }
 518         return false;
 519     }
 520 
 521     /**
 522      * Returns a hash code for this {@code SerialBlob}.
 523      * @return  a hash code value for this object.
 524      */
 525     public int hashCode() {
 526        return ((31 + Arrays.hashCode(buf)) * 31 + (int)len) * 31 + (int)origLen;
 527     }
 528 
 529     /**
 530      * Returns a clone of this {@code SerialBlob}. The copy will contain a
 531      * reference to a clone of the internal byte array, not a reference
 532      * to the original internal byte array of this {@code SerialBlob} object.
 533      * The underlying {@code Blob} object will be set to null.
 534      *
 535      * @return  a clone of this SerialBlob
 536      */
 537     public Object clone() {
 538         try {
 539             SerialBlob sb = (SerialBlob) super.clone();
 540             sb.buf =  (buf != null) ? Arrays.copyOf(buf, (int)len) : null;
 541             sb.blob = null;
 542             return sb;
 543         } catch (CloneNotSupportedException ex) {
 544             // this shouldn't happen, since we are Cloneable
 545             throw new InternalError();
 546         }
 547 
 548     }
 549 
 550     /**
 551      * readObject is called to restore the state of the SerialBlob from
 552      * a stream.
 553      */
 554     private void readObject(ObjectInputStream s)
 555             throws IOException, ClassNotFoundException {
 556 
 557         ObjectInputStream.GetField fields = s.readFields();
 558        byte[] tmp = (byte[])fields.get("buf", null);
 559        if (tmp == null)
 560            throw new InvalidObjectException("buf is null and should not be!");
 561        buf = tmp.clone();
 562        len = fields.get("len", 0L);
 563        if (buf.length != len)
 564            throw new InvalidObjectException("buf is not the expected size");
 565        origLen = fields.get("origLen", 0L);
 566        blob = (Blob) fields.get("blob", null);
 567     }
 568 
 569     /**
 570      * writeObject is called to save the state of the SerialBlob
 571      * to a stream.
 572      */
 573     private void writeObject(ObjectOutputStream s)
 574             throws IOException, ClassNotFoundException {
 575 
 576         ObjectOutputStream.PutField fields = s.putFields();
 577         fields.put("buf", buf);
 578         fields.put("len", len);
 579         fields.put("origLen", origLen);
 580         // Note: this check to see if it is an instance of Serializable
 581         // is for backwards compatibility
 582         fields.put("blob", blob instanceof Serializable ? blob : null);
 583         s.writeFields();
 584     }
 585 
 586     /**
 587      * Check to see if this object had previously had its {@code free} method
 588      * called
 589      *
 590      * @throws SerialException
 591      */
 592     private void isValid() throws SerialException {
 593         if (buf == null) {
 594             throw new SerialException("Error: You cannot call a method on a "
 595                     + "SerialBlob instance once free() has been called.");
 596         }
 597     }
 598 
 599     /**
 600      * The identifier that assists in the serialization of this
 601      * {@code SerialBlob} object.
 602      */
 603     static final long serialVersionUID = -8144641928112860441L;
 604 }