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