src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialBlob.java
author darcy
Wed, 25 Sep 2019 09:37:18 -0700
changeset 58338 faf791c5a710
parent 54106 9a90236ab64c
permissions -rw-r--r--
8231442: Suppress warnings on non-serializable instance fields in java.sql.* modules Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54106
9a90236ab64c 8220253: Fix Headings in java.sql.rowset
lancea
parents: 51181
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
 *
54106
9a90236ab64c 8220253: Fix Headings in java.sql.rowset
lancea
parents: 51181
diff changeset
    54
 * <h2> Thread safety </h2>
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
24968
3308660aa3f2 8046389: Add missing @since tag under javax.sql.**
henryjen
parents: 18564
diff changeset
    61
 * @since 1.5
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class SerialBlob implements Blob, Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * A serialized array of uninterpreted bytes representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * value of this <code>SerialBlob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
    70
    private byte[] buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The internal representation of the <code>Blob</code> object on which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>SerialBlob</code> object is based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
58338
faf791c5a710 8231442: Suppress warnings on non-serializable instance fields in java.sql.* modules
darcy
parents: 54106
diff changeset
    76
    @SuppressWarnings("serial") // Not statically typed as Serializable; checked in writeObject
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private Blob blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The number of bytes in this <code>SerialBlob</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private long len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
    87
     * The original number of bytes in this <code>SerialBlob</code> object's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * array of bytes when it was first established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private long origLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs a <code>SerialBlob</code> object that is a serialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * the given <code>byte</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The new <code>SerialBlob</code> object is initialized with the data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <code>byte</code> array, thus allowing disconnected <code>RowSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * objects to establish serialized <code>Blob</code> objects without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * touching the data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param b the <code>byte</code> array containing the data for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *        <code>Blob</code> object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws SerialException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws SQLException if a SQL errors occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   107
    public SerialBlob(byte[] b)
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   108
            throws SerialException, SQLException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        len = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        buf = new byte[(int)len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for(int i = 0; i < len; i++) {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   113
            buf[i] = b[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        origLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Constructs a <code>SerialBlob</code> object that is a serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * version of the given <code>Blob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The new <code>SerialBlob</code> object is initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * data from the <code>Blob</code> object; therefore, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>Blob</code> object should have previously brought the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * SQL <code>BLOB</code> value's data over to the client from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the database. Otherwise, the new <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * will contain no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param blob the <code>Blob</code> object from which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *     <code>SerialBlob</code> object is to be constructed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *     cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws SerialException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws SQLException if the <code>Blob</code> passed to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *     to this constructor is a <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see java.sql.Blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   138
    public SerialBlob (Blob blob)
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   139
            throws SerialException, SQLException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (blob == null) {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   142
            throw new SQLException(
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   143
                    "Cannot instantiate a SerialBlob object with a null Blob object");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        len = blob.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        buf = blob.getBytes(1, (int)len );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.blob = blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        origLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Copies the specified number of bytes, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * position, from this <code>SerialBlob</code> object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * another array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Note that if the given number of bytes to be copied is larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the length of this <code>SerialBlob</code> object's array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * bytes, the given number will be shortened to the array's length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param pos the ordinal position of the first byte in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *            <code>SerialBlob</code> object to be copied;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *            numbering starts at <code>1</code>; must not be less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *            than <code>1</code> and must be less than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *            to the length of this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param length the number of bytes to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return an array of bytes that is a copy of a region of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *         <code>SerialBlob</code> object, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *         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
   170
     * @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
   171
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    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
   174
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (length > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            length = (int)len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
7544
85a0e84bb9a0 6659234: Incorrect check in SerialBlob.getBytes
lancea
parents: 5506
diff changeset
   179
        if (pos < 1 || len - pos < 0 ) {
85a0e84bb9a0 6659234: Incorrect check in SerialBlob.getBytes
lancea
parents: 5506
diff changeset
   180
            throw new SerialException("Invalid arguments: position cannot be "
85a0e84bb9a0 6659234: Incorrect check in SerialBlob.getBytes
lancea
parents: 5506
diff changeset
   181
                    + "less than 1 or greater than the length of the SerialBlob");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        pos--; // correct pos to array index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        byte[] b = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            b[i] = this.buf[(int)pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Retrieves the number of bytes in this <code>SerialBlob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * object's array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return a <code>long</code> indicating the length in bytes of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *         <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
   201
     * @throws SerialException if an error occurs;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   202
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public long length() throws SerialException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   205
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns this <code>SerialBlob</code> object as an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Unlike the related method, <code>setBinaryStream</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * a stream is produced regardless of whether the <code>SerialBlob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * was created with a <code>Blob</code> object or a <code>byte</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return a <code>java.io.InputStream</code> object that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *         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
   217
     * @throws SerialException if an error occurs;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   218
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see #setBinaryStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    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
   222
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   223
        InputStream stream = new ByteArrayInputStream(buf);
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   224
        return stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns the position in this <code>SerialBlob</code> object where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * the given pattern of bytes begins, starting the search at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param pattern the pattern of bytes for which to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param start the position of the byte in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *              <code>SerialBlob</code> object from which to begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *              the search; the first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *              must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *              the length of this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return the position in this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *         where the given pattern begins, starting at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *         position; <code>-1</code> if the pattern is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         or the given starting position is out of bounds; position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *         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
   243
     * @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
   244
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *         value from the database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public long position(byte[] pattern, long start)
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   249
            throws SerialException, SQLException {
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   250
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   251
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (start < 1 || start > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int pos = (int)start-1; // internally Blobs are stored as arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        long patlen = pattern.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        while (pos < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (pattern[i] == buf[pos]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (i + 1 == patlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    return (pos + 1) - (patlen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                i++; pos++; // increment pos, and i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } else if (pattern[i] != buf[pos]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                pos++; // increment pos only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return -1; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Returns the position in this <code>SerialBlob</code> object where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * the given <code>Blob</code> object begins, starting the search at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param pattern the <code>Blob</code> object for which to search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param start the position of the byte in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *              <code>SerialBlob</code> object from which to begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *              the search; the first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *              must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *              the length of this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return the position in this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         where the given <code>Blob</code> object begins, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *         at the specified position; <code>-1</code> if the pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         not found or the given starting position is out of bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *         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
   289
     * @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
   290
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *         value from the database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public long position(Blob pattern, long start)
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   295
            throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   296
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return position(pattern.getBytes(1, (int)(pattern.length())), start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Writes the given array of bytes to the <code>BLOB</code> value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * this <code>Blob</code> object represents, starting at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>pos</code>, and returns the number of bytes written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param pos the position in the SQL <code>BLOB</code> value at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *     to start writing. The first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *     must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *     the length of this <code>SerialBlob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param bytes the array of bytes to be written to the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *        value that this <code>Blob</code> object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return the number of bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @throws SerialException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *     <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
   314
     *     invalid offset value is set;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   315
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         value from the database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @see #getBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int setBytes(long pos, byte[] bytes)
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   321
            throws SerialException, SQLException {
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   322
        return setBytes(pos, bytes, 0, bytes.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Writes all or part of the given <code>byte</code> array to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <code>BLOB</code> value that this <code>Blob</code> object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * and returns the number of bytes written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Writing starts at position <code>pos</code> in the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * value; <i>len</i> bytes from the given byte array are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param pos the position in the <code>BLOB</code> object at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *     to start writing. The first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *     must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *     the length of this <code>SerialBlob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param bytes the array of bytes to be written to the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *     value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param offset the offset in the <code>byte</code> array at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *     to start reading the bytes. The first offset position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *     <code>0</code>; must not be less than <code>0</code> nor greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *     than the length of the <code>byte</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param length the number of bytes to be written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *     <code>BLOB</code> value from the array of bytes <i>bytes</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @return the number of bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws SerialException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *     <code>BLOB</code> value; if an invalid position is set; if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *     invalid offset value is set; if number of bytes to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *     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
   350
     *     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
   351
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         value from the database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see #getBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public int setBytes(long pos, byte[] bytes, int offset, int length)
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   357
            throws SerialException, SQLException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   359
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (offset < 0 || offset > bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new SerialException("Invalid offset in byte array set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (pos < 1 || pos > this.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new SerialException("Invalid position in BLOB object set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if ((long)(length) > origLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new SerialException("Buffer is not sufficient to hold the value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if ((length + offset) > bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throw new SerialException("Invalid OffSet. Cannot have combined offset " +
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   374
                    "and length that is greater that the Blob buffer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        pos--; // correct to array indexing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        while ( i < length || (offset + i +1) < (bytes.length-offset) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            this.buf[(int)pos + i] = bytes[offset + i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Retrieves a stream that can be used to write to the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * value that this <code>Blob</code> object represents.  The stream begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * at position <code>pos</code>. This method forwards the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <code>setBinaryStream()</code> call to the underlying <code>Blob</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * the event that this <code>SerialBlob</code> object is instantiated with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <code>Blob</code>. If this <code>SerialBlob</code> is instantiated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * a <code>byte</code> array, a <code>SerialException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param pos the position in the <code>BLOB</code> value at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *        to start writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @return a <code>java.io.OutputStream</code> object to which data can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *         be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @throws SQLException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *            <code>BLOB</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @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
   402
     *     <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
   403
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @see #getBinaryStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public java.io.OutputStream setBinaryStream(long pos)
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   407
            throws SerialException, SQLException {
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   408
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   409
        isValid();
10323
48d695d1e966 7077451: SerialLob, SerialClob have the wrong checks for setStream methods
lancea
parents: 7544
diff changeset
   410
        if (this.blob != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return this.blob.setBinaryStream(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new SerialException("Unsupported operation. SerialBlob cannot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                "return a writable binary stream, unless instantiated with a Blob object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                "that provides a setBinaryStream() implementation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Truncates the <code>BLOB</code> value that this <code>Blob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * object represents to be <code>len</code> bytes in length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param length the length, in bytes, to which the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *        value that this <code>Blob</code> object represents should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *        truncated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @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
   427
     *     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
   428
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void truncate(long length) throws SerialException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   431
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   432
        if (length > len) {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   433
            throw new SerialException(
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   434
                    "Length more than what can be truncated");
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   435
        } else if((int)length == 0) {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   436
            buf = new byte[0];
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   437
            len = length;
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   438
        } else {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   439
            len = length;
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   440
            buf = this.getBytes(1, (int)len);
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   441
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   446
     * Returns an
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   447
     * <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
   448
     * {@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
   449
     * length bytes in length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   451
     * @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
   452
     * retrieved. The first byte in the {@code Blob} is at position 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @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
   454
     * @return
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   455
     * <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
   456
     * be read.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   457
     * @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
   458
     * 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
   459
     * 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
   460
     * @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
   461
     * called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   465
    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
   466
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   467
        if (pos < 1 || pos > this.length()) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   468
            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
   469
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   470
        if (length < 1 || length > len - pos + 1) {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   471
            throw new SerialException(
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   472
                    "length is < 1 or pos + length > total number of bytes");
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   473
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   474
        return new ByteArrayInputStream(buf, (int) pos - 1, (int) length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
25976
4de01a56e3ee 8054555: javadoc cleanup for java.sql and javax.sql
lancea
parents: 24968
diff changeset
   479
     * This method frees the {@code SerialBlob} object and releases the
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   480
     * 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
   481
     * 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
   482
     * subsequent calls to {@code free} are treated as a no-op. </P>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   484
     * @throws SQLException if an error occurs releasing the Blob's resources
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public void free() throws SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   488
        if (buf != null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   489
            buf = null;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   490
            if (blob != null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   491
                blob.free();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   492
            }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   493
            blob = null;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   494
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   496
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   497
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   498
     * 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
   499
     * 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
   500
     * 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
   501
     * object.
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
     * @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
   504
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   505
     * @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
   506
     *          equivalent to this SerialBlob, {@code false} otherwise
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   507
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   508
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   509
    public boolean equals(Object obj) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   510
        if (this == obj) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   511
            return true;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   512
        }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   513
        if (obj instanceof SerialBlob) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   514
            SerialBlob sb = (SerialBlob)obj;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   515
            if (this.len == sb.len) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   516
                return Arrays.equals(buf, sb.buf);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   517
            }
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
        return false;
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
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   522
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   523
     * Returns a hash code for this {@code SerialBlob}.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   524
     * @return  a hash code value for this object.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   525
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   526
    public int hashCode() {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   527
       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
   528
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   529
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   530
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   531
     * 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
   532
     * 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
   533
     * 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
   534
     * 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
   535
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   536
     * @return  a clone of this SerialBlob
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   537
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   538
    public Object clone() {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   539
        try {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   540
            SerialBlob sb = (SerialBlob) super.clone();
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   541
            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
   542
            sb.blob = null;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   543
            return sb;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   544
        } catch (CloneNotSupportedException ex) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   545
            // this shouldn't happen, since we are Cloneable
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   546
            throw new InternalError();
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
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   551
     * 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
   552
     * a stream.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   553
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   554
    private void readObject(ObjectInputStream s)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   555
            throws IOException, ClassNotFoundException {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   556
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   557
        ObjectInputStream.GetField fields = s.readFields();
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   558
        byte[] tmp = (byte[])fields.get("buf", null);
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   559
        if (tmp == null)
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   560
            throw new InvalidObjectException("buf is null and should not be!");
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   561
        buf = tmp.clone();
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   562
        len = fields.get("len", 0L);
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   563
        if (buf.length != len)
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   564
            throw new InvalidObjectException("buf is not the expected size");
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   565
        origLen = fields.get("origLen", 0L);
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   566
        blob = (Blob) fields.get("blob", null);
14337
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
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   570
     * 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
   571
     * to a stream.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   572
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   573
    private void writeObject(ObjectOutputStream s)
51181
01b8120f867a 8208060: Additional corrections of serial-related declarations
darcy
parents: 47216
diff changeset
   574
            throws IOException {
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   575
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   576
        ObjectOutputStream.PutField fields = s.putFields();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   577
        fields.put("buf", buf);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   578
        fields.put("len", len);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   579
        fields.put("origLen", origLen);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   580
        // Note: this check to see if it is an instance of Serializable
25976
4de01a56e3ee 8054555: javadoc cleanup for java.sql and javax.sql
lancea
parents: 24968
diff changeset
   581
        // is for backwards compatibility
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   582
        fields.put("blob", blob instanceof Serializable ? blob : null);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   583
        s.writeFields();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   584
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   585
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   587
     * 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
   588
     * called
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   589
     *
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   590
     * @throws SerialException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   592
    private void isValid() throws SerialException {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   593
        if (buf == null) {
30641
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   594
            throw new SerialException("Error: You cannot call a method on a " +
701f6f90dc0b 8074657: Missing space on a boundary of concatenated strings
igerasim
parents: 25991
diff changeset
   595
                    "SerialBlob instance once free() has been called.");
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   596
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   597
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   599
    /**
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   600
     * 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
   601
     * {@code SerialBlob} object.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   602
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    static final long serialVersionUID = -8144641928112860441L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
}