0N/A/*
3909N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.sql.rowset.serial;
0N/A
0N/Aimport java.sql.*;
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * A serialized mapping in the Java programming language of an SQL
0N/A * <code>CLOB</code> value.
0N/A * <P>
0N/A * The <code>SerialClob</code> class provides a constructor for creating
0N/A * an instance from a <code>Clob</code> object. Note that the <code>Clob</code>
0N/A * object should have brought the SQL <code>CLOB</code> value's data over
0N/A * to the client before a <code>SerialClob</code> object
0N/A * is constructed from it. The data of an SQL <code>CLOB</code> value can
0N/A * be materialized on the client as a stream of Unicode characters.
0N/A * <P>
0N/A * <code>SerialClob</code> methods make it possible to get a substring
0N/A * from a <code>SerialClob</code> object or to locate the start of
0N/A * a pattern of characters.
0N/A *
0N/A * @author Jonathan Bruce
0N/A */
0N/Apublic class SerialClob implements Clob, Serializable, Cloneable {
0N/A
0N/A /**
0N/A * A serialized array of characters containing the data of the SQL
0N/A * <code>CLOB</code> value that this <code>SerialClob</code> object
0N/A * represents.
0N/A *
0N/A * @serial
0N/A */
0N/A private char buf[];
0N/A
0N/A /**
3588N/A * Internal Clob representation if SerialClob is initialized with a
3588N/A * Clob. Null if SerialClob is initialized with a char[].
0N/A */
3588N/A private final Clob clob;
0N/A
0N/A /**
0N/A * The length in characters of this <code>SerialClob</code> object's
0N/A * internal array of characters.
0N/A *
0N/A * @serial
0N/A */
0N/A private long len;
0N/A
0N/A /**
3588N/A * The original length in characters of this <code>SerialClob</code>
3588N/A * object's internal array of characters.
0N/A *
0N/A * @serial
0N/A */
3588N/A private final long origLen;
0N/A
0N/A /**
0N/A * Constructs a <code>SerialClob</code> object that is a serialized version of
0N/A * the given <code>char</code> array.
0N/A * <p>
0N/A * The new <code>SerialClob</code> object is initialized with the data from the
0N/A * <code>char</code> array, thus allowing disconnected <code>RowSet</code>
0N/A * objects to establish a serialized <code>Clob</code> object without touching
0N/A * the data source.
0N/A *
0N/A * @param ch the char array representing the <code>Clob</code> object to be
0N/A * serialized
0N/A * @throws SerialException if an error occurs during serialization
0N/A * @throws SQLException if a SQL error occurs
0N/A */
0N/A public SerialClob(char ch[]) throws SerialException, SQLException {
0N/A
0N/A // %%% JMB. Agreed. Add code here to throw a SQLException if no
0N/A // support is available for locatorsUpdateCopy=false
0N/A // Serializing locators is not supported.
0N/A
0N/A len = ch.length;
0N/A buf = new char[(int)len];
0N/A for (int i = 0; i < len ; i++){
0N/A buf[i] = ch[i];
0N/A }
0N/A origLen = len;
3588N/A clob = null;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>SerialClob</code> object that is a serialized
0N/A * version of the given <code>Clob</code> object.
0N/A * <P>
0N/A * The new <code>SerialClob</code> object is initialized with the
0N/A * data from the <code>Clob</code> object; therefore, the
0N/A * <code>Clob</code> object should have previously brought the
0N/A * SQL <code>CLOB</code> value's data over to the client from
0N/A * the database. Otherwise, the new <code>SerialClob</code> object
0N/A * object will contain no data.
0N/A * <p>
3588N/A * Note: The <code>Clob</code> object supplied to this constructor must
3588N/A * return non-null for both the <code>Clob.getCharacterStream()</code>
0N/A * and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</code>
3588N/A * constructor cannot serialize a <code>Clob</code> object in this instance
0N/A * and will throw an <code>SQLException</code> object.
0N/A *
0N/A * @param clob the <code>Clob</code> object from which this
0N/A * <code>SerialClob</code> object is to be constructed; cannot be null
0N/A * @throws SerialException if an error occurs during serialization
0N/A * @throws SQLException if a SQL error occurs in capturing the CLOB;
3588N/A * if the <code>Clob</code> object is a null; or if either of the
0N/A * <code>Clob.getCharacterStream()</code> and <code>Clob.getAsciiStream()</code>
3588N/A * methods on the <code>Clob</code> returns a null
0N/A * @see java.sql.Clob
0N/A */
0N/A public SerialClob(Clob clob) throws SerialException, SQLException {
0N/A
0N/A if (clob == null) {
0N/A throw new SQLException("Cannot instantiate a SerialClob " +
0N/A "object with a null Clob object");
0N/A }
0N/A len = clob.length();
0N/A this.clob = clob;
0N/A buf = new char[(int)len];
0N/A int read = 0;
0N/A int offset = 0;
0N/A
3588N/A try (Reader charStream = clob.getCharacterStream()) {
3588N/A if (charStream == null) {
3588N/A throw new SQLException("Invalid Clob object. The call to getCharacterStream " +
3588N/A "returned null which cannot be serialized.");
3588N/A }
0N/A
3588N/A // Note: get an ASCII stream in order to null-check it,
3588N/A // even though we don't do anything with it.
3588N/A try (InputStream asciiStream = clob.getAsciiStream()) {
3588N/A if (asciiStream == null) {
3588N/A throw new SQLException("Invalid Clob object. The call to getAsciiStream " +
3588N/A "returned null which cannot be serialized.");
3588N/A }
3588N/A }
0N/A
3588N/A try (Reader reader = new BufferedReader(charStream)) {
3588N/A do {
3588N/A read = reader.read(buf, offset, (int)(len - offset));
3588N/A offset += read;
3588N/A } while (read > 0);
3588N/A }
0N/A } catch (java.io.IOException ex) {
0N/A throw new SerialException("SerialClob: " + ex.getMessage());
0N/A }
0N/A
0N/A origLen = len;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the number of characters in this <code>SerialClob</code>
0N/A * object's array of characters.
0N/A *
0N/A * @return a <code>long</code> indicating the length in characters of this
0N/A * <code>SerialClob</code> object's array of character
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A public long length() throws SerialException {
0N/A return len;
0N/A }
0N/A
0N/A /**
0N/A * Returns this <code>SerialClob</code> object's data as a stream
0N/A * of Unicode characters. Unlike the related method, <code>getAsciiStream</code>,
0N/A * a stream is produced regardless of whether the <code>SerialClob</code> object
0N/A * was created with a <code>Clob</code> object or a <code>char</code> array.
0N/A *
0N/A * @return a <code>java.io.Reader</code> object containing this
0N/A * <code>SerialClob</code> object's data
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A public java.io.Reader getCharacterStream() throws SerialException {
0N/A return (java.io.Reader) new CharArrayReader(buf);
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the <code>CLOB</code> value designated by this <code>SerialClob</code>
0N/A * object as an ascii stream. This method forwards the <code>getAsciiStream</code>
0N/A * call to the underlying <code>Clob</code> object in the event that this
0N/A * <code>SerialClob</code> object is instantiated with a <code>Clob</code>
0N/A * object. If this <code>SerialClob</code> object is instantiated with
0N/A * a <code>char</code> array, a <code>SerialException</code> object is thrown.
0N/A *
0N/A * @return a <code>java.io.InputStream</code> object containing
0N/A * this <code>SerialClob</code> object's data
0N/A * @throws SerialException if this <code>SerialClob</code> object was not instantiated
0N/A * with a <code>Clob</code> object
0N/A * @throws SQLException if there is an error accessing the
0N/A * <code>CLOB</code> value represented by the <code>Clob</code> object that was
0N/A * used to create this <code>SerialClob</code> object
0N/A */
0N/A public java.io.InputStream getAsciiStream() throws SerialException, SQLException {
3588N/A if (this.clob != null) {
3588N/A return this.clob.getAsciiStream();
3588N/A } else {
3588N/A throw new SerialException("Unsupported operation. SerialClob cannot " +
0N/A "return a the CLOB value as an ascii stream, unless instantiated " +
0N/A "with a fully implemented Clob object.");
3588N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the substring contained in this
0N/A * <code>SerialClob</code> object, starting at the given position
0N/A * and continuing for the specified number or characters.
0N/A *
0N/A * @param pos the position of the first character in the substring
0N/A * to be copied; the first character of the
0N/A * <code>SerialClob</code> object is at position
0N/A * <code>1</code>; must not be less than <code>1</code>,
0N/A * and the sum of the starting position and the length
0N/A * of the substring must be less than the length of this
0N/A * <code>SerialClob</code> object
0N/A * @param length the number of characters in the substring to be
0N/A * returned; must not be greater than the length of
0N/A * this <code>SerialClob</code> object, and the
0N/A * sum of the starting position and the length
0N/A * of the substring must be less than the length of this
0N/A * <code>SerialClob</code> object
0N/A * @return a <code>String</code> object containing a substring of
0N/A * this <code>SerialClob</code> object beginning at the
0N/A * given position and containing the specified number of
0N/A * consecutive characters
0N/A * @throws SerialException if either of the arguments is out of bounds
0N/A */
0N/A public String getSubString(long pos, int length) throws SerialException {
0N/A
0N/A if (pos < 1 || pos > this.length()) {
0N/A throw new SerialException("Invalid position in BLOB object set");
0N/A }
0N/A
0N/A if ((pos-1) + length > this.length()) {
0N/A throw new SerialException("Invalid position and substring length");
0N/A }
0N/A
0N/A try {
0N/A return new String(buf, (int)pos - 1, length);
0N/A
0N/A } catch (StringIndexOutOfBoundsException e) {
0N/A throw new SerialException("StringIndexOutOfBoundsException: " +
0N/A e.getMessage());
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns the position in this <code>SerialClob</code> object
0N/A * where the given <code>String</code> object begins, starting
0N/A * the search at the specified position. This method returns
0N/A * <code>-1</code> if the pattern is not found.
0N/A *
0N/A * @param searchStr the <code>String</code> object for which to
0N/A * search
0N/A * @param start the position in this <code>SerialClob</code> object
0N/A * at which to start the search; the first position is
0N/A * <code>1</code>; must not be less than <code>1</code> nor
0N/A * greater than the length of this <code>SerialClob</code> object
0N/A * @return the position at which the given <code>String</code> object
0N/A * begins, starting the search at the specified position;
0N/A * <code>-1</code> if the given <code>String</code> object is
0N/A * not found or the starting position is out of bounds; position
0N/A * numbering for the return value starts at <code>1</code>
0N/A * @throws SerialException if an error occurs locating the String signature
0N/A * @throws SQLException if there is an error accessing the Blob value
0N/A * from the database.
0N/A */
0N/A public long position(String searchStr, long start)
0N/A throws SerialException, SQLException {
0N/A
0N/A if (start < 1 || start > len) {
0N/A return -1;
0N/A }
0N/A
0N/A char pattern[] = searchStr.toCharArray();
0N/A
0N/A int pos = (int)start-1;
0N/A int i = 0;
0N/A long patlen = pattern.length;
0N/A
0N/A while (pos < len) {
0N/A if (pattern[i] == buf[pos]) {
0N/A if (i + 1 == patlen) {
0N/A return (pos + 1) - (patlen - 1);
0N/A }
0N/A i++; pos++; // increment pos, and i
0N/A
0N/A } else if (pattern[i] != buf[pos]) {
0N/A pos++; // increment pos only
0N/A }
0N/A }
0N/A return -1; // not found
0N/A }
0N/A
0N/A /**
0N/A * Returns the position in this <code>SerialClob</code> object
0N/A * where the given <code>Clob</code> signature begins, starting
0N/A * the search at the specified position. This method returns
0N/A * <code>-1</code> if the pattern is not found.
0N/A *
0N/A * @param searchStr the <code>Clob</code> object for which to search
0N/A * @param start the position in this <code>SerialClob</code> object
0N/A * at which to begin the search; the first position is
0N/A * <code>1</code>; must not be less than <code>1</code> nor
0N/A * greater than the length of this <code>SerialClob</code> object
0N/A * @return the position at which the given <code>Clob</code>
0N/A * object begins in this <code>SerialClob</code> object,
0N/A * at or after the specified starting position
0N/A * @throws SerialException if an error occurs locating the Clob signature
0N/A * @throws SQLException if there is an error accessing the Blob value
0N/A * from the database
0N/A */
0N/A public long position(Clob searchStr, long start)
0N/A throws SerialException, SQLException {
0N/A
0N/A return position(searchStr.getSubString(1,(int)searchStr.length()), start);
0N/A }
0N/A
0N/A /**
0N/A * Writes the given Java <code>String</code> to the <code>CLOB</code>
0N/A * value that this <code>SerialClob</code> object represents, at the position
0N/A * <code>pos</code>.
0N/A *
0N/A * @param pos the position at which to start writing to the <code>CLOB</code>
0N/A * value that this <code>SerialClob</code> object represents; the first
0N/A * position is <code>1</code>; must not be less than <code>1</code> nor
0N/A * greater than the length of this <code>SerialClob</code> object
0N/A * @param str the string to be written to the <code>CLOB</code>
0N/A * value that this <code>SerialClob</code> object represents
0N/A * @return the number of characters written
0N/A * @throws SerialException if there is an error accessing the
0N/A * <code>CLOB</code> value; if an invalid position is set; if an
0N/A * invalid offset value is set; if number of bytes to be written
0N/A * is greater than the <code>SerialClob</code> length; or the combined
0N/A * values of the length and offset is greater than the Clob buffer
0N/A */
0N/A public int setString(long pos, String str) throws SerialException {
0N/A return (setString(pos, str, 0, str.length()));
0N/A }
0N/A
0N/A /**
0N/A * Writes <code>len</code> characters of <code>str</code>, starting
0N/A * at character <code>offset</code>, to the <code>CLOB</code> value
0N/A * that this <code>Clob</code> represents.
0N/A *
0N/A * @param pos the position at which to start writing to the <code>CLOB</code>
0N/A * value that this <code>SerialClob</code> object represents; the first
0N/A * position is <code>1</code>; must not be less than <code>1</code> nor
0N/A * greater than the length of this <code>SerialClob</code> object
0N/A * @param str the string to be written to the <code>CLOB</code>
0N/A * value that this <code>Clob</code> object represents
0N/A * @param offset the offset into <code>str</code> to start reading
0N/A * the characters to be written
0N/A * @param length the number of characters to be written
0N/A * @return the number of characters written
0N/A * @throws SerialException if there is an error accessing the
0N/A * <code>CLOB</code> value; if an invalid position is set; if an
0N/A * invalid offset value is set; if number of bytes to be written
0N/A * is greater than the <code>SerialClob</code> length; or the combined
0N/A * values of the length and offset is greater than the Clob buffer
0N/A */
0N/A public int setString(long pos, String str, int offset, int length)
0N/A throws SerialException {
0N/A String temp = str.substring(offset);
0N/A char cPattern[] = temp.toCharArray();
0N/A
0N/A if (offset < 0 || offset > str.length()) {
0N/A throw new SerialException("Invalid offset in byte array set");
0N/A }
0N/A
0N/A if (pos < 1 || pos > this.length()) {
0N/A throw new SerialException("Invalid position in BLOB object set");
0N/A }
0N/A
0N/A if ((long)(length) > origLen) {
0N/A throw new SerialException("Buffer is not sufficient to hold the value");
0N/A }
0N/A
0N/A if ((length + offset) > str.length()) {
0N/A // need check to ensure length + offset !> bytes.length
0N/A throw new SerialException("Invalid OffSet. Cannot have combined offset " +
0N/A " and length that is greater that the Blob buffer");
0N/A }
0N/A
0N/A int i = 0;
0N/A pos--; //values in the array are at position one less
0N/A while ( i < length || (offset + i +1) < (str.length() - offset ) ) {
0N/A this.buf[(int)pos + i ] = cPattern[offset + i ];
0N/A i++;
0N/A }
0N/A return i;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves a stream to be used to write Ascii characters to the
0N/A * <code>CLOB</code> value that this <code>SerialClob</code> object represents,
0N/A * starting at position <code>pos</code>. This method forwards the
0N/A * <code>setAsciiStream()</code> call to the underlying <code>Clob</code> object in
0N/A * the event that this <code>SerialClob</code> object is instantiated with a
0N/A * <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated
0N/A * with a <code>char</code> array, a <code>SerialException</code> object is thrown.
0N/A *
0N/A * @param pos the position at which to start writing to the
0N/A * <code>CLOB</code> object
0N/A * @return the stream to which ASCII encoded characters can be written
0N/A * @throws SerialException if SerialClob is not instantiated with a
0N/A * Clob object that supports <code>setAsciiStream</code>
0N/A * @throws SQLException if there is an error accessing the
0N/A * <code>CLOB</code> value
0N/A * @see #getAsciiStream
0N/A */
0N/A public java.io.OutputStream setAsciiStream(long pos)
0N/A throws SerialException, SQLException {
0N/A if (this.clob.setAsciiStream(pos) != null) {
0N/A return this.clob.setAsciiStream(pos);
0N/A } else {
0N/A throw new SerialException("Unsupported operation. SerialClob cannot " +
0N/A "return a writable ascii stream\n unless instantiated with a Clob object " +
0N/A "that has a setAsciiStream() implementation");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Retrieves a stream to be used to write a stream of Unicode characters
0N/A * to the <code>CLOB</code> value that this <code>SerialClob</code> object
0N/A * represents, at position <code>pos</code>. This method forwards the
0N/A * <code>setCharacterStream()</code> call to the underlying <code>Clob</code>
0N/A * object in the event that this <code>SerialClob</code> object is instantiated with a
0N/A * <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated with
0N/A * a <code>char</code> array, a <code>SerialException</code> is thrown.
0N/A *
0N/A * @param pos the position at which to start writing to the
0N/A * <code>CLOB</code> value
0N/A *
0N/A * @return a stream to which Unicode encoded characters can be written
0N/A * @throws SerialException if the SerialClob is not instantiated with
0N/A * a Clob object that supports <code>setCharacterStream</code>
0N/A * @throws SQLException if there is an error accessing the
0N/A * <code>CLOB</code> value
0N/A * @see #getCharacterStream
0N/A */
0N/A public java.io.Writer setCharacterStream(long pos)
0N/A throws SerialException, SQLException {
0N/A if (this.clob.setCharacterStream(pos) != null) {
0N/A return this.clob.setCharacterStream(pos);
0N/A } else {
0N/A throw new SerialException("Unsupported operation. SerialClob cannot " +
0N/A "return a writable character stream\n unless instantiated with a Clob object " +
0N/A "that has a setCharacterStream implementation");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Truncates the <code>CLOB</code> value that this <code>SerialClob</code>
0N/A * object represents so that it has a length of <code>len</code>
0N/A * characters.
0N/A * <p>
0N/A * Truncating a <code>SerialClob</code> object to length 0 has the effect of
0N/A * clearing its contents.
0N/A *
0N/A * @param length the length, in bytes, to which the <code>CLOB</code>
0N/A * value should be truncated
0N/A * @throws SQLException if there is an error accessing the
0N/A * <code>CLOB</code> value
0N/A */
0N/A public void truncate(long length) throws SerialException {
0N/A if (length > len) {
0N/A throw new SerialException
0N/A ("Length more than what can be truncated");
0N/A } else {
0N/A len = length;
0N/A // re-size the buffer
0N/A
0N/A if (len == 0) {
0N/A buf = new char[] {};
0N/A } else {
0N/A buf = (this.getSubString(1, (int)len)).toCharArray();
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A
0N/A public Reader getCharacterStream(long pos, long length) throws SQLException {
0N/A throw new java.lang.UnsupportedOperationException("Not supported");
0N/A }
0N/A
0N/A public void free() throws SQLException {
0N/A throw new java.lang.UnsupportedOperationException("Not supported");
0N/A }
0N/A
0N/A /**
0N/A * The identifier that assists in the serialization of this <code>SerialClob</code>
0N/A * object.
0N/A */
0N/A static final long serialVersionUID = -1662519690087375313L;
0N/A}