src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialClob.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: 51310
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.*;
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
    30
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A serialized mapping in the Java programming language of an SQL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>CLOB</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The <code>SerialClob</code> class provides a constructor for creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * an instance from a <code>Clob</code> object.  Note that the <code>Clob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * object should have brought the SQL <code>CLOB</code> value's data over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * to the client before a <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * is constructed from it.  The data of an SQL <code>CLOB</code> value can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * be materialized on the client as a stream of Unicode characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>SerialClob</code> methods make it possible to get a substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * from a <code>SerialClob</code> object or to locate the start of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a pattern of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
54106
9a90236ab64c 8220253: Fix Headings in java.sql.rowset
lancea
parents: 51310
diff changeset
    47
 * <h2> Thread safety </h2>
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
    48
 *
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
    49
 * <p> A SerialClob 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
    50
 * SerialClob is to be used by more than one thread then access to the SerialClob
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
    51
 * should be controlled by appropriate synchronization.
24968
3308660aa3f2 8046389: Add missing @since tag under javax.sql.**
henryjen
parents: 18564
diff changeset
    52
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Jonathan Bruce
24968
3308660aa3f2 8046389: Add missing @since tag under javax.sql.**
henryjen
parents: 18564
diff changeset
    54
 * @since 1.5
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class SerialClob implements Clob, Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * A serialized array of characters containing the data of the SQL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <code>CLOB</code> value that this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private char buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
    68
     * Internal Clob representation if SerialClob is initialized with a
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
    69
     * Clob. Null if SerialClob is initialized with a char[].
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
58338
faf791c5a710 8231442: Suppress warnings on non-serializable instance fields in java.sql.* modules
darcy
parents: 54106
diff changeset
    71
    @SuppressWarnings("serial")  // Not statically typed as Serializable; checked in writeObject
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
    72
    private Clob clob;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The length in characters of this <code>SerialClob</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * internal array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private long len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
    83
     * The original length in characters of this <code>SerialClob</code>
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
    84
     * object's internal array of characters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
    88
    private long origLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Constructs a <code>SerialClob</code> object that is a serialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * the given <code>char</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The new <code>SerialClob</code> object is initialized with the data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <code>char</code> array, thus allowing disconnected <code>RowSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * objects to establish a serialized <code>Clob</code> object without touching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * the data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param ch the char array representing the <code>Clob</code> object to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *         serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws SerialException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws SQLException if a SQL error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public SerialClob(char ch[]) throws SerialException, SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // %%% JMB. Agreed. Add code here to throw a SQLException if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // support is available for locatorsUpdateCopy=false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Serializing locators is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        len = ch.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        buf = new char[(int)len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (int i = 0; i < len ; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
           buf[i] = ch[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        origLen = len;
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   116
        clob = null;
2
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>SerialClob</code> object that is a serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * version of the given <code>Clob</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The new <code>SerialClob</code> object is initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * data from the <code>Clob</code> object; therefore, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>Clob</code> object should have previously brought the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * SQL <code>CLOB</code> value's data over to the client from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the database. Otherwise, the new <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * object will contain no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <p>
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   130
     * Note: The <code>Clob</code> object supplied to this constructor must
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   131
     * return non-null for both the <code>Clob.getCharacterStream()</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</code>
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   133
     * constructor cannot serialize a <code>Clob</code> object in this instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * and will throw an <code>SQLException</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param  clob the <code>Clob</code> object from which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *     <code>SerialClob</code> object is to be constructed; cannot be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws SerialException if an error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws SQLException if a SQL error occurs in capturing the CLOB;
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   140
     *     if the <code>Clob</code> object is a null; or if either of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *     <code>Clob.getCharacterStream()</code> and <code>Clob.getAsciiStream()</code>
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   142
     *     methods on the <code>Clob</code> returns a null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see java.sql.Clob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public SerialClob(Clob clob) throws SerialException, SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (clob == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new SQLException("Cannot instantiate a SerialClob " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                "object with a null Clob object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        len = clob.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.clob = clob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        buf = new char[(int)len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   157
        try (Reader charStream = clob.getCharacterStream()) {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   158
            if (charStream == null) {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   159
                throw new SQLException("Invalid Clob object. The call to getCharacterStream " +
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   160
                    "returned null which cannot be serialized.");
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   161
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   163
            // Note: get an ASCII stream in order to null-check it,
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   164
            // even though we don't do anything with it.
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   165
            try (InputStream asciiStream = clob.getAsciiStream()) {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   166
                if (asciiStream == null) {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   167
                    throw new SQLException("Invalid Clob object. The call to getAsciiStream " +
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   168
                        "returned null which cannot be serialized.");
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   169
                }
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   170
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   172
            try (Reader reader = new BufferedReader(charStream)) {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   173
                do {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   174
                    read = reader.read(buf, offset, (int)(len - offset));
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   175
                    offset += read;
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   176
                } while (read > 0);
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   177
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (java.io.IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new SerialException("SerialClob: " + ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        origLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Retrieves the number of characters in this <code>SerialClob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * object's array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return a <code>long</code> indicating the length in characters of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *         <code>SerialClob</code> object's array of character
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   191
     * @throws SerialException if an error occurs;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   192
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public long length() throws SerialException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   195
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns this <code>SerialClob</code> object's data as a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * of Unicode characters. Unlike the related method, <code>getAsciiStream</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * a stream is produced regardless of whether the <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * was created with a <code>Clob</code> object or a <code>char</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return a <code>java.io.Reader</code> object containing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *         <code>SerialClob</code> object's data
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   207
     * @throws SerialException if an error occurs;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   208
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public java.io.Reader getCharacterStream() throws SerialException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   211
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return (java.io.Reader) new CharArrayReader(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Retrieves the <code>CLOB</code> value designated by this <code>SerialClob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * object as an ascii stream. This method forwards the <code>getAsciiStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * call to the underlying <code>Clob</code> object in the event that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <code>SerialClob</code> object is instantiated with a <code>Clob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * object. If this <code>SerialClob</code> object is instantiated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * a <code>char</code> array, a <code>SerialException</code> object is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return a <code>java.io.InputStream</code> object containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *     this <code>SerialClob</code> object's data
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   225
     * @throws SerialException if this {@code SerialClob} object was not
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   226
     * instantiated with a <code>Clob</code> object;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   227
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @throws SQLException if there is an error accessing the
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   229
     *     <code>CLOB</code> value represented by the <code>Clob</code> object
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   230
     * that was used to create this <code>SerialClob</code> object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public java.io.InputStream getAsciiStream() throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   233
        isValid();
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   234
        if (this.clob != null) {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   235
            return this.clob.getAsciiStream();
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   236
        } else {
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   237
            throw new SerialException("Unsupported operation. SerialClob cannot " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                "return a the CLOB value as an ascii stream, unless instantiated " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                "with a fully implemented Clob object.");
8405
bafd7b441d80 7018385: update javax.sql classes to use try-with-resources
smarks
parents: 5506
diff changeset
   240
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Returns a copy of the substring contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <code>SerialClob</code> object, starting at the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * and continuing for the specified number or characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param pos the position of the first character in the substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *            to be copied; the first character of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *            <code>SerialClob</code> object is at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *            <code>1</code>; must not be less than <code>1</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *            and the sum of the starting position and the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *            of the substring must be less than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *            <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param length the number of characters in the substring to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *               returned; must not be greater than the length of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *               this <code>SerialClob</code> object, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *               sum of the starting position and the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *               of the substring must be less than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *               <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return a <code>String</code> object containing a substring of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *         this <code>SerialClob</code> object beginning at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *         given position and containing the specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         consecutive characters
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   265
     * @throws SerialException if either of the arguments is out of bounds;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   266
     * if {@code free} had previously been called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public String getSubString(long pos, int length) throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   270
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (pos < 1 || pos > this.length()) {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   272
            throw new SerialException("Invalid position in SerialClob object set");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if ((pos-1) + length > this.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new SerialException("Invalid position and substring length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return new String(buf, (int)pos - 1, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (StringIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new SerialException("StringIndexOutOfBoundsException: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Returns the position in this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * where the given <code>String</code> object begins, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * the search at the specified position. This method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <code>-1</code> if the pattern is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param searchStr the <code>String</code> object for which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *                  search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param start the position in this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *         at which to start the search; the first position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *         <code>1</code>; must not be less than <code>1</code> nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         greater than the length of this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the position at which the given <code>String</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *         begins, starting the search at the specified position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *         <code>-1</code> if the given <code>String</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *         not found or the starting position is out of bounds; position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *         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
   306
     * @throws SerialException  if the {@code free} method had been
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   307
     * previously called on this object
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   308
     * @throws SQLException if there is an error accessing the Clob value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *         from the database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public long position(String searchStr, long start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   313
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (start < 1 || start > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        char pattern[] = searchStr.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int pos = (int)start-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        long patlen = pattern.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        while (pos < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (pattern[i] == buf[pos]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (i + 1 == patlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    return (pos + 1) - (patlen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                i++; pos++; // increment pos, and i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } else if (pattern[i] != buf[pos]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                pos++; // increment pos only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return -1; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Returns the position in this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * where the given <code>Clob</code> signature begins, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * the search at the specified position. This method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>-1</code> if the pattern is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param searchStr the <code>Clob</code> object for which to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param start the position in this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *        at which to begin the search; the first position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *         <code>1</code>; must not be less than <code>1</code> nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         greater than the length of this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return the position at which the given <code>Clob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *         object begins in this <code>SerialClob</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *         at or after the specified starting position
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   352
     * @throws SerialException if an error occurs locating the Clob signature;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   353
     * if the {@code free} method had been previously called on this object
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   354
     * @throws SQLException if there is an error accessing the Clob value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *         from the database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public long position(Clob searchStr, long start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        throws SerialException, SQLException {
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
        return position(searchStr.getSubString(1,(int)searchStr.length()), start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Writes the given Java <code>String</code> to the <code>CLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * value that this <code>SerialClob</code> object represents, at the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param pos the position at which to start writing to the <code>CLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         value that this <code>SerialClob</code> object represents; the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         position is <code>1</code>; must not be less than <code>1</code> nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *         greater than the length of this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param str the string to be written to the <code>CLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *        value that this <code>SerialClob</code> object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return the number of characters written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws SerialException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *     <code>CLOB</code> value; if an invalid position is set; if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *     invalid offset value is set; if number of bytes to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *     is greater than the <code>SerialClob</code> length; or the combined
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   379
     *     values of the length and offset is greater than the Clob buffer;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   380
     * if the {@code free} method had been previously called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public int setString(long pos, String str) throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return (setString(pos, str, 0, str.length()));
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
     * Writes <code>len</code> characters of <code>str</code>, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * at character <code>offset</code>, to the <code>CLOB</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * that this <code>Clob</code> represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param pos the position at which to start writing to the <code>CLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *         value that this <code>SerialClob</code> object represents; the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *         position is <code>1</code>; must not be less than <code>1</code> nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *         greater than the length of this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param str the string to be written to the <code>CLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *        value that this <code>Clob</code> object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param offset the offset into <code>str</code> to start reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *        the characters to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param length the number of characters to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return the number of characters written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @throws SerialException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *     <code>CLOB</code> value; if an invalid position is set; if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *     invalid offset value is set; if number of bytes to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *     is greater than the <code>SerialClob</code> length; or the combined
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   405
     *     values of the length and offset is greater than the Clob buffer;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   406
     * if the {@code free} method had been previously called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public int setString(long pos, String str, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        throws SerialException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   410
        isValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        String temp = str.substring(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        char cPattern[] = temp.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (offset < 0 || offset > str.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new SerialException("Invalid offset in byte array set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (pos < 1 || pos > this.length()) {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   419
            throw new SerialException("Invalid position in Clob object set");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if ((long)(length) > origLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new SerialException("Buffer is not sufficient to hold the value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if ((length + offset) > str.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            // need check to ensure length + offset !> bytes.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw new SerialException("Invalid OffSet. Cannot have combined offset " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                " and length that is greater that the Blob buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        pos--;  //values in the array are at position one less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        while ( i < length || (offset + i +1) < (str.length() - offset ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            this.buf[(int)pos + i ] = cPattern[offset + i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Retrieves a stream to be used to write Ascii characters to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <code>CLOB</code> value that this <code>SerialClob</code> object represents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * starting at position <code>pos</code>. This method forwards the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <code>setAsciiStream()</code> call to the underlying <code>Clob</code> object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * the event that this <code>SerialClob</code> object is instantiated with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *  with a <code>char</code> array, a <code>SerialException</code> object is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param pos the position at which to start writing to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *        <code>CLOB</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @return the stream to which ASCII encoded characters can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @throws SerialException if SerialClob is not instantiated with a
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   454
     *     Clob object;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   455
     * if the {@code free} method had been previously called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @throws SQLException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *     <code>CLOB</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @see #getAsciiStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public java.io.OutputStream setAsciiStream(long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   462
        isValid();
10323
48d695d1e966 7077451: SerialLob, SerialClob have the wrong checks for setStream methods
lancea
parents: 9035
diff changeset
   463
         if (this.clob != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
             return this.clob.setAsciiStream(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
             throw new SerialException("Unsupported operation. SerialClob cannot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                "return a writable ascii stream\n unless instantiated with a Clob object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                "that has a setAsciiStream() implementation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Retrieves a stream to be used to write a stream of Unicode characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * to the <code>CLOB</code> value that this <code>SerialClob</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * represents, at position <code>pos</code>. This method forwards the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <code>setCharacterStream()</code> call to the underlying <code>Clob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * object in the event that this <code>SerialClob</code> object is instantiated with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * a <code>char</code> array, a <code>SerialException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param  pos the position at which to start writing to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *        <code>CLOB</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @return a stream to which Unicode encoded characters can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @throws SerialException if the SerialClob is not instantiated with
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   486
     *     a Clob object;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   487
     * if the {@code free} method had been previously called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @throws SQLException if there is an error accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *            <code>CLOB</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @see #getCharacterStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public java.io.Writer setCharacterStream(long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        throws SerialException, SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   494
        isValid();
10323
48d695d1e966 7077451: SerialLob, SerialClob have the wrong checks for setStream methods
lancea
parents: 9035
diff changeset
   495
        if (this.clob != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return this.clob.setCharacterStream(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new SerialException("Unsupported operation. SerialClob cannot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                "return a writable character stream\n unless instantiated with a Clob object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                "that has a setCharacterStream implementation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Truncates the <code>CLOB</code> value that this <code>SerialClob</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * object represents so that it has a length of <code>len</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Truncating a <code>SerialClob</code> object to length 0 has the effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * clearing its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param length the length, in bytes, to which the <code>CLOB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *        value should be truncated
16732
c96397eeca72 8011393: Typo in javadoc for SerialClob.truncate
lancea
parents: 14781
diff changeset
   514
     * @throws SerialException if there is an error accessing the
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   515
     *        <code>CLOB</code> value;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   516
     * if the {@code free} method had been previously called on this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    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
   519
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   520
        if (length > len) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   521
           throw new SerialException
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   522
              ("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
   523
        } else {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   524
             len = length;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   525
             // re-size the buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   527
             if (len == 0) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   528
                buf = new char[] {};
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   529
             } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                buf = (this.getSubString(1, (int)len)).toCharArray();
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   531
             }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   532
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   536
    /**
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   537
     * Returns a {@code Reader} object that contains a partial
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   538
     * {@code SerialClob} value, starting
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   539
     * with the character specified by pos, which is length characters in length.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   540
     *
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   541
     * @param pos the offset to the first character of the partial value to
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   542
     * be retrieved.  The first character in the {@code SerialClob} is at position 1.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   543
     * @param length the length in characters of the partial value to be retrieved.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   544
     * @return {@code Reader} through which the partial {@code SerialClob}
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   545
     * value can be read.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   546
     * @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
   547
     * number of characters in the {@code SerialClob} or if pos + length
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   548
     * is greater than the number of characters in the {@code SerialClob};
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   549
     * @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
   550
     * called on this object
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   551
     * @since 1.6
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   552
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public Reader getCharacterStream(long pos, long length) throws SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   554
        isValid();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   555
        if (pos < 1 || pos > len) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   556
            throw new SerialException("Invalid position in Clob object set");
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   557
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   558
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   559
        if ((pos-1) + length > len) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   560
            throw new SerialException("Invalid position and substring length");
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   561
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   562
        if (length <= 0) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   563
            throw new SerialException("Invalid length specified");
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   564
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   565
        return new CharArrayReader(buf, (int)pos, (int)length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   568
    /**
25976
4de01a56e3ee 8054555: javadoc cleanup for java.sql and javax.sql
lancea
parents: 24968
diff changeset
   569
     * This method frees the {@code SerialClob} object and releases the
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   570
     * resources that it holds.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   571
     * The object is invalid once the {@code free} method is called.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   572
     * <p>
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   573
     * If {@code free} is called multiple times, the subsequent
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   574
     * calls to {@code free} are treated as a no-op.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   575
     * </P>
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   576
     * @throws SQLException if an error occurs releasing
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   577
     * the Clob's resources
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   578
     * @since 1.6
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   579
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public void free() throws SQLException {
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   581
        if (buf != null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   582
            buf = null;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   583
            if (clob != null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   584
                clob.free();
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   585
            }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   586
            clob = null;
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   587
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   591
     * Compares this SerialClob to the specified object.  The result is {@code
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   592
     * 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: 10323
diff changeset
   593
     * SerialClob} object that represents the same sequence of characters as this
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   594
     * object.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   595
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   596
     * @param  obj The object to compare this {@code SerialClob} against
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   597
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   598
     * @return  {@code true} if the given object represents a {@code SerialClob}
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   599
     *          equivalent to this SerialClob, {@code false} otherwise
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   600
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   601
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   602
    public boolean equals(Object obj) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   603
        if (this == obj) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   604
            return true;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   605
        }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   606
        if (obj instanceof SerialClob) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   607
            SerialClob sc = (SerialClob)obj;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   608
            if (this.len == sc.len) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   609
                return Arrays.equals(buf, sc.buf);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   610
            }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   611
        }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   612
        return false;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   613
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   614
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   615
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   616
     * Returns a hash code for this {@code SerialClob}.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   617
     * @return  a hash code value for this object.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   618
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   619
    public int hashCode() {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   620
       return ((31 + Arrays.hashCode(buf)) * 31 + (int)len) * 31 + (int)origLen;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   621
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   622
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   623
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   624
     * Returns a clone of this {@code SerialClob}. The copy will contain a
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   625
     * reference to a clone of the internal character array, not a reference
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   626
     * to the original internal character array of this {@code SerialClob} object.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   627
     * The underlying {@code Clob} object will be set to null.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   628
     *
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   629
     * @return  a clone of this SerialClob
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   630
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   631
    public Object clone() {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   632
        try {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   633
            SerialClob sc = (SerialClob) super.clone();
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   634
            sc.buf = (buf != null) ? Arrays.copyOf(buf, (int)len) : null;
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   635
            sc.clob = null;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   636
            return sc;
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   637
        } catch (CloneNotSupportedException ex) {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   638
            // this shouldn't happen, since we are Cloneable
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   639
            throw new InternalError();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   640
        }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   641
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   642
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   643
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   644
     * readObject is called to restore the state of the SerialClob from
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   645
     * a stream.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   646
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   647
    private void readObject(ObjectInputStream s)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   648
            throws IOException, ClassNotFoundException {
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   649
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   650
        ObjectInputStream.GetField fields = s.readFields();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   651
       char[] tmp = (char[])fields.get("buf", null);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   652
       if (tmp == null)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   653
           throw new InvalidObjectException("buf is null and should not be!");
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   654
       buf = tmp.clone();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   655
       len = fields.get("len", 0L);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   656
       if (buf.length != len)
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   657
           throw new InvalidObjectException("buf is not the expected size");
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   658
       origLen = fields.get("origLen", 0L);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   659
       clob = (Clob) fields.get("clob", null);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   660
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   661
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   662
    /**
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   663
     * writeObject is called to save the state of the SerialClob
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   664
     * to a stream.
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   665
     */
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   666
    private void writeObject(ObjectOutputStream s)
51310
a694574b2def 8208782: Remove extra type in throws clause of SerialClob.writeObject
darcy
parents: 47216
diff changeset
   667
            throws IOException {
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   668
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   669
        ObjectOutputStream.PutField fields = s.putFields();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   670
        fields.put("buf", buf);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   671
        fields.put("len", len);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   672
        fields.put("origLen", origLen);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   673
        // 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
   674
        // is for backwards compatibility
14337
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   675
        fields.put("clob", clob instanceof Serializable ? clob : null);
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   676
        s.writeFields();
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   677
    }
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   678
a003c6a54cb6 8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents: 10323
diff changeset
   679
    /**
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   680
     * 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
   681
     * called
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   682
     *
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   683
     * @throws SerialException
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   684
     */
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   685
    private void isValid() throws SerialException {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   686
        if (buf == null) {
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   687
            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
   688
                    + "SerialClob instance once free() has been called.");
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   689
        }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   690
    }
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   691
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   692
    /**
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14337
diff changeset
   693
     * The identifier that assists in the serialization of this {@code SerialClob}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    static final long serialVersionUID = -1662519690087375313L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
}