jdk/src/share/classes/javax/sql/rowset/serial/SerialBlob.java
author henryjen
Mon, 16 Jun 2014 08:09:30 -0700
changeset 24968 3308660aa3f2
parent 18564 f9db68ff2cbb
child 25976 4de01a56e3ee
permissions -rw-r--r--
8046389: Add missing @since tag under javax.sql.** Reviewed-by: lancea
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
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private byte buf[];
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Blob blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The number of bytes in this <code>SerialBlob</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private long len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
    86
     * The original number of bytes in this <code>SerialBlob</code> object's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * array of bytes when it was first established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private long origLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Constructs a <code>SerialBlob</code> object that is a serialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * the given <code>byte</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The new <code>SerialBlob</code> object is initialized with the data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>byte</code> array, thus allowing disconnected <code>RowSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * objects to establish serialized <code>Blob</code> objects without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * touching the data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param b the <code>byte</code> array containing the data for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *        <code>Blob</code> object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws SerialException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws SQLException if a SQL errors occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public SerialBlob(byte[] b) throws SerialException, SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        len = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        buf = new byte[(int)len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for(int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
           buf[i] = b[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        origLen = len;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Constructs a <code>SerialBlob</code> object that is a serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * version of the given <code>Blob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The new <code>SerialBlob</code> object is initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * data from the <code>Blob</code> object; therefore, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>Blob</code> object should have previously brought the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * SQL <code>BLOB</code> value's data over to the client from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the database. Otherwise, the new <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * will contain no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param blob the <code>Blob</code> object from which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *     <code>SerialBlob</code> object is to be constructed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *     cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws SerialException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws SQLException if the <code>Blob</code> passed to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *     to this constructor is a <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see java.sql.Blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public SerialBlob (Blob blob) throws SerialException, SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (blob == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new SQLException("Cannot instantiate a SerialBlob " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                 "object with a null Blob object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        len = blob.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        buf = blob.getBytes(1, (int)len );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.blob = blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         //if ( len < 10240000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         // len = 10240000;
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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   250
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (start < 1 || start > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int pos = (int)start-1; // internally Blobs are stored as arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        long patlen = pattern.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        while (pos < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (pattern[i] == buf[pos]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (i + 1 == patlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    return (pos + 1) - (patlen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                i++; pos++; // increment pos, and i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else if (pattern[i] != buf[pos]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                pos++; // increment pos only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return -1; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns the position in this <code>SerialBlob</code> object where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * the given <code>Blob</code> object begins, starting the search at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param pattern the <code>Blob</code> object for which to search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param start the position of the byte in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *              <code>SerialBlob</code> object from which to begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *              the search; the first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *              must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *              the length of this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return the position in this <code>SerialBlob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         where the given <code>Blob</code> object begins, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         at the specified position; <code>-1</code> if the pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *         not found or the given starting position is out of bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         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
   288
     * @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
   289
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *         value from the database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public long position(Blob pattern, long start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
       throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   295
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return position(pattern.getBytes(1, (int)(pattern.length())), start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Writes the given array of bytes to the <code>BLOB</code> value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * this <code>Blob</code> object represents, starting at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <code>pos</code>, and returns the number of bytes written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param pos the position in the SQL <code>BLOB</code> value at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *     to start writing. The first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *     must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *     the length of this <code>SerialBlob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param bytes the array of bytes to be written to the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *        value that this <code>Blob</code> object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @return the number of bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @throws SerialException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *     <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
   313
     *     invalid offset value is set;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   314
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *         value from the database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @see #getBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public int setBytes(long pos, byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        throws SerialException, SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return (setBytes(pos, bytes, 0, bytes.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Writes all or part of the given <code>byte</code> array to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <code>BLOB</code> value that this <code>Blob</code> object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * and returns the number of bytes written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Writing starts at position <code>pos</code> in the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * value; <i>len</i> bytes from the given byte array are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param pos the position in the <code>BLOB</code> object at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *     to start writing. The first position is <code>1</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *     must not be less than <code>1</code> nor greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *     the length of this <code>SerialBlob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param bytes the array of bytes to be written to the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *     value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param offset the offset in the <code>byte</code> array at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *     to start reading the bytes. The first offset position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *     <code>0</code>; must not be less than <code>0</code> nor greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *     than the length of the <code>byte</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param length the number of bytes to be written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *     <code>BLOB</code> value from the array of bytes <i>bytes</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return the number of bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws SerialException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *     <code>BLOB</code> value; if an invalid position is set; if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *     invalid offset value is set; if number of bytes to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *     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
   349
     *     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
   350
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @throws SQLException if there is an error accessing the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *         value from the database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #getBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public int setBytes(long pos, byte[] bytes, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        throws SerialException, SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   358
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (offset < 0 || offset > bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new SerialException("Invalid offset in byte array set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (pos < 1 || pos > this.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new SerialException("Invalid position in BLOB object set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if ((long)(length) > origLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new SerialException("Buffer is not sufficient to hold the value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if ((length + offset) > bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throw new SerialException("Invalid OffSet. Cannot have combined offset " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                "and length that is greater that the Blob buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        pos--; // correct to array indexing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        while ( i < length || (offset + i +1) < (bytes.length-offset) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            this.buf[(int)pos + i] = bytes[offset + i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Retrieves a stream that can be used to write to the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * value that this <code>Blob</code> object represents.  The stream begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * at position <code>pos</code>. This method forwards the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <code>setBinaryStream()</code> call to the underlying <code>Blob</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * the event that this <code>SerialBlob</code> object is instantiated with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <code>Blob</code>. If this <code>SerialBlob</code> is instantiated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * a <code>byte</code> array, a <code>SerialException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param pos the position in the <code>BLOB</code> value at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *        to start writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return a <code>java.io.OutputStream</code> object to which data can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *         be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @throws SQLException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *            <code>BLOB</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @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
   401
     *     <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
   402
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see #getBinaryStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public java.io.OutputStream setBinaryStream(long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   407
        isValid();
10323
48d695d1e966 7077451: SerialLob, SerialClob have the wrong checks for setStream methods
lancea
parents: 7544
diff changeset
   408
        if (this.blob != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return this.blob.setBinaryStream(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw new SerialException("Unsupported operation. SerialBlob cannot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                "return a writable binary stream, unless instantiated with a Blob object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                "that provides a setBinaryStream() implementation");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Truncates the <code>BLOB</code> value that this <code>Blob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * object represents to be <code>len</code> bytes in length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param length the length, in bytes, to which the <code>BLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *        value that this <code>Blob</code> object represents should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *        truncated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @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
   425
     *     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
   426
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void truncate(long length) throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   430
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   431
        if (length > len) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   432
           throw new SerialException
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   433
              ("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
   434
        } else if((int)length == 0) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   435
             buf = new byte[0];
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   436
             len = length;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   437
        } else {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   438
             len = length;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   439
             buf = this.getBytes(1, (int)len);
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   440
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   445
     * Returns an
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   446
     * <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
   447
     * {@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
   448
     * length bytes in length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   450
     * @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
   451
     * retrieved. The first byte in the {@code Blob} is at position 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @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
   453
     * @return
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   454
     * <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
   455
     * be read.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   456
     * @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
   457
     * 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
   458
     * 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
   459
     * @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
   460
     * called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   464
    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
   465
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   466
        if (pos < 1 || pos > this.length()) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   467
            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
   468
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   469
        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
   470
            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
   471
                    + "total number of bytes");
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   472
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   473
        return new ByteArrayInputStream(buf, (int) pos - 1, (int) length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   478
     * 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
   479
     * 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
   480
     * 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
   481
     * subsequent calls to {@code free} are treated as a no-op. </P>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   483
     * @throws SQLException if an error occurs releasing the Blob's resources
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public void free() throws SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   487
        if (buf != null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   488
            buf = null;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   489
            if (blob != null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   490
                blob.free();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   491
            }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   492
            blob = null;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   493
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
14337
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
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   497
     * 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
   498
     * 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
   499
     * 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
   500
     * object.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   501
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   502
     * @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
   503
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   504
     * @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
   505
     *          equivalent to this SerialBlob, {@code false} otherwise
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
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   508
    public boolean equals(Object obj) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   509
        if (this == obj) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   510
            return true;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   511
        }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   512
        if (obj instanceof SerialBlob) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   513
            SerialBlob sb = (SerialBlob)obj;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   514
            if (this.len == sb.len) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   515
                return Arrays.equals(buf, sb.buf);
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
        }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   518
        return false;
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
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   522
     * Returns a hash code for this {@code SerialBlob}.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   523
     * @return  a hash code value for this object.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   524
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   525
    public int hashCode() {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   526
       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
   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
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   530
     * 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
   531
     * 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
   532
     * 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
   533
     * 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
   534
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   535
     * @return  a clone of this SerialBlob
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   536
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   537
    public Object clone() {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   538
        try {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   539
            SerialBlob sb = (SerialBlob) super.clone();
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   540
            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
   541
            sb.blob = null;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   542
            return sb;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   543
        } catch (CloneNotSupportedException ex) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   544
            // this shouldn't happen, since we are Cloneable
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   545
            throw new InternalError();
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
    /**
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();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   558
       byte[] tmp = (byte[])fields.get("buf", null);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   559
       if (tmp == null)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   560
           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
   561
       buf = tmp.clone();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   562
       len = fields.get("len", 0L);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   563
       if (buf.length != len)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   564
           throw new InvalidObjectException("buf is not the expected size");
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   565
       origLen = fields.get("origLen", 0L);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   566
       blob = (Blob) fields.get("blob", null);
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)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   574
            throws IOException, ClassNotFoundException {
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
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 11129
diff changeset
   581
        // is for backwards compatibiity
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) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   594
            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
   595
                    + "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
   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
}