src/share/classes/javax/sql/rowset/serial/SerialClob.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sql.rowset.serial;
  27 
  28 import java.sql.*;
  29 import java.io.*;


  30 
  31 /**
  32  * A serialized mapping in the Java programming language of an SQL
  33  * <code>CLOB</code> value.
  34  * <P>
  35  * The <code>SerialClob</code> class provides a constructor for creating
  36  * an instance from a <code>Clob</code> object.  Note that the <code>Clob</code>
  37  * object should have brought the SQL <code>CLOB</code> value's data over
  38  * to the client before a <code>SerialClob</code> object
  39  * is constructed from it.  The data of an SQL <code>CLOB</code> value can
  40  * be materialized on the client as a stream of Unicode characters.
  41  * <P>
  42  * <code>SerialClob</code> methods make it possible to get a substring
  43  * from a <code>SerialClob</code> object or to locate the start of
  44  * a pattern of characters.
  45  *
  46  * @author Jonathan Bruce
  47  */
  48 public class SerialClob implements Clob, Serializable, Cloneable {
  49 
  50     /**
  51      * A serialized array of characters containing the data of the SQL
  52      * <code>CLOB</code> value that this <code>SerialClob</code> object
  53      * represents.
  54      *
  55      * @serial
  56      */
  57     private char buf[];
  58 
  59     /**
  60      * Internal Clob representation if SerialClob is initialized with a
  61      * Clob. Null if SerialClob is initialized with a char[].
  62      */
  63     private final Clob clob;
  64 
  65     /**
  66      * The length in characters of this <code>SerialClob</code> object's
  67      * internal array of characters.
  68      *
  69      * @serial
  70      */
  71     private long len;
  72 
  73     /**
  74      * The original length in characters of this <code>SerialClob</code>
  75      * object's internal array of characters.
  76      *
  77      * @serial
  78      */
  79     private final long origLen;
  80 
  81     /**
  82      * Constructs a <code>SerialClob</code> object that is a serialized version of
  83      * the given <code>char</code> array.
  84      * <p>
  85      * The new <code>SerialClob</code> object is initialized with the data from the
  86      * <code>char</code> array, thus allowing disconnected <code>RowSet</code>
  87      * objects to establish a serialized <code>Clob</code> object without touching
  88      * the data source.
  89      *
  90      * @param ch the char array representing the <code>Clob</code> object to be
  91      *         serialized
  92      * @throws SerialException if an error occurs during serialization
  93      * @throws SQLException if a SQL error occurs
  94      */
  95     public SerialClob(char ch[]) throws SerialException, SQLException {
  96 
  97         // %%% JMB. Agreed. Add code here to throw a SQLException if no
  98         // support is available for locatorsUpdateCopy=false
  99         // Serializing locators is not supported.


 496               len = length;
 497               // re-size the buffer
 498 
 499               if (len == 0) {
 500                   buf = new char[] {};
 501               } else {
 502                 buf = (this.getSubString(1, (int)len)).toCharArray();
 503               }
 504 
 505          }
 506     }
 507 
 508 
 509     public Reader getCharacterStream(long pos, long length) throws SQLException {
 510         throw new java.lang.UnsupportedOperationException("Not supported");
 511     }
 512 
 513     public void free() throws SQLException {
 514         throw new java.lang.UnsupportedOperationException("Not supported");
 515     }


























































































 516 
 517     /**
 518          * The identifier that assists in the serialization of this <code>SerialClob</code>
 519      * object.
 520      */
 521     static final long serialVersionUID = -1662519690087375313L;
 522 }
   1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sql.rowset.serial;
  27 
  28 import java.sql.*;
  29 import java.io.*;
  30 import java.util.Arrays;
  31 import java.util.Objects;
  32 
  33 /**
  34  * A serialized mapping in the Java programming language of an SQL
  35  * <code>CLOB</code> value.
  36  * <P>
  37  * The <code>SerialClob</code> class provides a constructor for creating
  38  * an instance from a <code>Clob</code> object.  Note that the <code>Clob</code>
  39  * object should have brought the SQL <code>CLOB</code> value's data over
  40  * to the client before a <code>SerialClob</code> object
  41  * is constructed from it.  The data of an SQL <code>CLOB</code> value can
  42  * be materialized on the client as a stream of Unicode characters.
  43  * <P>
  44  * <code>SerialClob</code> methods make it possible to get a substring
  45  * from a <code>SerialClob</code> object or to locate the start of
  46  * a pattern of characters.
  47  *
  48  * @author Jonathan Bruce
  49  */
  50 public class SerialClob implements Clob, Serializable, Cloneable {
  51 
  52     /**
  53      * A serialized array of characters containing the data of the SQL
  54      * <code>CLOB</code> value that this <code>SerialClob</code> object
  55      * represents.
  56      *
  57      * @serial
  58      */
  59     private char buf[];
  60 
  61     /**
  62      * Internal Clob representation if SerialClob is initialized with a
  63      * Clob. Null if SerialClob is initialized with a char[].
  64      */
  65     private Clob clob;
  66 
  67     /**
  68      * The length in characters of this <code>SerialClob</code> object's
  69      * internal array of characters.
  70      *
  71      * @serial
  72      */
  73     private long len;
  74 
  75     /**
  76      * The original length in characters of this <code>SerialClob</code>
  77      * object's internal array of characters.
  78      *
  79      * @serial
  80      */
  81     private long origLen;
  82 
  83     /**
  84      * Constructs a <code>SerialClob</code> object that is a serialized version of
  85      * the given <code>char</code> array.
  86      * <p>
  87      * The new <code>SerialClob</code> object is initialized with the data from the
  88      * <code>char</code> array, thus allowing disconnected <code>RowSet</code>
  89      * objects to establish a serialized <code>Clob</code> object without touching
  90      * the data source.
  91      *
  92      * @param ch the char array representing the <code>Clob</code> object to be
  93      *         serialized
  94      * @throws SerialException if an error occurs during serialization
  95      * @throws SQLException if a SQL error occurs
  96      */
  97     public SerialClob(char ch[]) throws SerialException, SQLException {
  98 
  99         // %%% JMB. Agreed. Add code here to throw a SQLException if no
 100         // support is available for locatorsUpdateCopy=false
 101         // Serializing locators is not supported.


 498               len = length;
 499               // re-size the buffer
 500 
 501               if (len == 0) {
 502                   buf = new char[] {};
 503               } else {
 504                 buf = (this.getSubString(1, (int)len)).toCharArray();
 505               }
 506 
 507          }
 508     }
 509 
 510 
 511     public Reader getCharacterStream(long pos, long length) throws SQLException {
 512         throw new java.lang.UnsupportedOperationException("Not supported");
 513     }
 514 
 515     public void free() throws SQLException {
 516         throw new java.lang.UnsupportedOperationException("Not supported");
 517     }
 518    /**
 519      * Compares this SerialClob to the specified object.  The result is {@code
 520      * true} if and only if the argument is not {@code null} and is a {@code
 521      * SerialClob} object that represents the same sequence of characters as this
 522      * object.
 523      *
 524      * @param  obj
 525      *         The object to compare this {@code SerialClob} against
 526      *
 527      * @return  {@code true} if the given object represents a {@code SerialClob}
 528      *          equivalent to this SerialClob, {@code false} otherwise
 529      *
 530      */
 531     public boolean equals(Object obj) {
 532         if (this == obj) {
 533             return true;
 534         }
 535         if(obj == null || !(obj instanceof SerialClob)) {
 536             return false;
 537         }
 538         
 539         SerialClob sc = (SerialClob)obj;
 540         if(this.len == sc.len) {
 541             return Arrays.equals(buf, sc.buf);
 542         }
 543         return false;
 544     }
 545 
 546     /**
 547      * Returns a hash code for this {@code SerialClob}.
 548      * @return  a hash code value for this object.
 549      */
 550     public int hashCode() {
 551        return Objects.hash(buf, len, origLen);
 552     }
 553 
 554     /**
 555      * Returns a clone of this {@code SerialClob}. The copy will contain a
 556      * reference to a clone of the internal character array, not a reference
 557      * to the original internal character array of this {@code SerialClob} object.
 558      * The internal {@code Clob} field will be set to null.
 559      *
 560      * @return  a clone of this SerialClob
 561      */
 562     public Object clone() {
 563         SerialClob sc = null;
 564         try {
 565             sc = (SerialClob) super.clone();
 566             sc.buf = Arrays.copyOf(buf, (int)len);
 567             sc.clob = null;
 568 
 569         } catch (CloneNotSupportedException ex) {
 570             // this shouldn't happen, since we are Cloneable
 571         }
 572        return sc;
 573     }
 574 
 575     /**
 576      * readObject is called to restore the state of the SerialClob from
 577      * a stream.
 578      */
 579     private void readObject(ObjectInputStream s)
 580             throws IOException, ClassNotFoundException {
 581 
 582         ObjectInputStream.GetField fields = s.readFields();
 583        char[] tmp = (char[])fields.get("buf", null);
 584        if (tmp == null)
 585            throw new InvalidObjectException("buf is null and should not be!");
 586        buf = tmp.clone();
 587        len = fields.get("len", 0L);
 588        origLen = fields.get("origLen", 0L);
 589        clob = (Clob) fields.get("clob", null);
 590        if(buf.length != len)
 591            throw new InvalidObjectException("buf is not the expected size");
 592     }
 593 
 594     /**
 595      * writeObject is called to save the state of the SerialClob
 596      * to a stream.
 597      */
 598     private void writeObject(ObjectOutputStream s)
 599             throws IOException, ClassNotFoundException {
 600 
 601         ObjectOutputStream.PutField fields = s.putFields();
 602         fields.put("buf", buf);
 603         fields.put("len", len);
 604         fields.put("origLen", origLen);
 605         fields.put("clob", clob instanceof Serializable ? clob : null);
 606         s.writeFields();
 607     }
 608 
 609     /**
 610          * The identifier that assists in the serialization of this <code>SerialClob</code>
 611      * object.
 612      */
 613     static final long serialVersionUID = -1662519690087375313L;
 614 }