Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sql/rowset/serial/SerialArray.java
38918 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset.serial;2627import java.sql.*;28import java.io.*;29import java.util.Map;30import java.net.URL;31import java.util.Arrays;323334/**35* A serialized version of an <code>Array</code>36* object, which is the mapping in the Java programming language of an SQL37* <code>ARRAY</code> value.38* <P>39* The <code>SerialArray</code> class provides a constructor for creating40* a <code>SerialArray</code> instance from an <code>Array</code> object,41* methods for getting the base type and the SQL name for the base type, and42* methods for copying all or part of a <code>SerialArray</code> object.43* <P>44*45* Note: In order for this class to function correctly, a connection to the46* data source47* must be available in order for the SQL <code>Array</code> object to be48* materialized (have all of its elements brought to the client server)49* if necessary. At this time, logical pointers to the data in the data source,50* such as locators, are not currently supported.51*52* <h3> Thread safety </h3>53*54* A SerialArray is not safe for use by multiple concurrent threads. If a55* SerialArray is to be used by more than one thread then access to the56* SerialArray should be controlled by appropriate synchronization.57*58*/59public class SerialArray implements Array, Serializable, Cloneable {6061/**62* A serialized array in which each element is an <code>Object</code>63* in the Java programming language that represents an element64* in the SQL <code>ARRAY</code> value.65* @serial66*/67private Object[] elements;6869/**70* The SQL type of the elements in this <code>SerialArray</code> object. The71* type is expressed as one of the constants from the class72* <code>java.sql.Types</code>.73* @serial74*/75private int baseType;7677/**78* The type name used by the DBMS for the elements in the SQL <code>ARRAY</code>79* value that this <code>SerialArray</code> object represents.80* @serial81*/82private String baseTypeName;8384/**85* The number of elements in this <code>SerialArray</code> object, which86* is also the number of elements in the SQL <code>ARRAY</code> value87* that this <code>SerialArray</code> object represents.88* @serial89*/90private int len;9192/**93* Constructs a new <code>SerialArray</code> object from the given94* <code>Array</code> object, using the given type map for the custom95* mapping of each element when the elements are SQL UDTs.96* <P>97* This method does custom mapping if the array elements are a UDT98* and the given type map has an entry for that UDT.99* Custom mapping is recursive,100* meaning that if, for instance, an element of an SQL structured type101* is an SQL structured type that itself has an element that is an SQL102* structured type, each structured type that has a custom mapping will be103* mapped according to the given type map.104* <P>105* The new <code>SerialArray</code>106* object contains the same elements as the <code>Array</code> object107* from which it is built, except when the base type is the SQL type108* <code>STRUCT</code>, <code>ARRAY</code>, <code>BLOB</code>,109* <code>CLOB</code>, <code>DATALINK</code> or <code>JAVA_OBJECT</code>.110* In this case, each element in the new111* <code>SerialArray</code> object is the appropriate serialized form,112* that is, a <code>SerialStruct</code>, <code>SerialArray</code>,113* <code>SerialBlob</code>, <code>SerialClob</code>,114* <code>SerialDatalink</code>, or <code>SerialJavaObject</code> object.115* <P>116* Note: (1) The <code>Array</code> object from which a <code>SerialArray</code>117* object is created must have materialized the SQL <code>ARRAY</code> value's118* data on the client before it is passed to the constructor. Otherwise,119* the new <code>SerialArray</code> object will contain no data.120* <p>121* Note: (2) If the <code>Array</code> contains <code>java.sql.Types.JAVA_OBJECT</code>122* types, the <code>SerialJavaObject</code> constructor is called where checks123* are made to ensure this object is serializable.124* <p>125* Note: (3) The <code>Array</code> object supplied to this constructor cannot126* return <code>null</code> for any <code>Array.getArray()</code> methods.127* <code>SerialArray</code> cannot serialize null array values.128*129*130* @param array the <code>Array</code> object to be serialized131* @param map a <code>java.util.Map</code> object in which132* each entry consists of 1) a <code>String</code> object133* giving the fully qualified name of a UDT (an SQL structured type or134* distinct type) and 2) the135* <code>Class</code> object for the <code>SQLData</code> implementation136* that defines how the UDT is to be mapped. The <i>map</i>137* parameter does not have any effect for <code>Blob</code>,138* <code>Clob</code>, <code>DATALINK</code>, or139* <code>JAVA_OBJECT</code> types.140* @throws SerialException if an error occurs serializing the141* <code>Array</code> object142* @throws SQLException if a database access error occurs or if the143* <i>array</i> or the <i>map</i> values are <code>null</code>144*/145public SerialArray(Array array, Map<String,Class<?>> map)146throws SerialException, SQLException147{148149if ((array == null) || (map == null)) {150throw new SQLException("Cannot instantiate a SerialArray " +151"object with null parameters");152}153154if ((elements = (Object[])array.getArray()) == null) {155throw new SQLException("Invalid Array object. Calls to Array.getArray() " +156"return null value which cannot be serialized");157}158159elements = (Object[])array.getArray(map);160baseType = array.getBaseType();161baseTypeName = array.getBaseTypeName();162len = elements.length;163164switch (baseType) {165case java.sql.Types.STRUCT:166for (int i = 0; i < len; i++) {167elements[i] = new SerialStruct((Struct)elements[i], map);168}169break;170171case java.sql.Types.ARRAY:172for (int i = 0; i < len; i++) {173elements[i] = new SerialArray((Array)elements[i], map);174}175break;176177case java.sql.Types.BLOB:178for (int i = 0; i < len; i++) {179elements[i] = new SerialBlob((Blob)elements[i]);180}181break;182183case java.sql.Types.CLOB:184for (int i = 0; i < len; i++) {185elements[i] = new SerialClob((Clob)elements[i]);186}187break;188189case java.sql.Types.DATALINK:190for (int i = 0; i < len; i++) {191elements[i] = new SerialDatalink((URL)elements[i]);192}193break;194195case java.sql.Types.JAVA_OBJECT:196for (int i = 0; i < len; i++) {197elements[i] = new SerialJavaObject(elements[i]);198}199}200}201202/**203* This method frees the {@code SeriableArray} object and releases the204* resources that it holds. The object is invalid once the {@code free}205* method is called. <p> If {@code free} is called multiple times, the206* subsequent calls to {@code free} are treated as a no-op. </P>207*208* @throws SQLException if an error occurs releasing the SerialArray's resources209* @since 1.6210*/211public void free() throws SQLException {212if (elements != null) {213elements = null;214baseTypeName= null;215}216}217218/**219* Constructs a new <code>SerialArray</code> object from the given220* <code>Array</code> object.221* <P>222* This constructor does not do custom mapping. If the base type of the array223* is an SQL structured type and custom mapping is desired, the constructor224* <code>SerialArray(Array array, Map map)</code> should be used.225* <P>226* The new <code>SerialArray</code>227* object contains the same elements as the <code>Array</code> object228* from which it is built, except when the base type is the SQL type229* <code>BLOB</code>,230* <code>CLOB</code>, <code>DATALINK</code> or <code>JAVA_OBJECT</code>.231* In this case, each element in the new232* <code>SerialArray</code> object is the appropriate serialized form,233* that is, a <code>SerialBlob</code>, <code>SerialClob</code>,234* <code>SerialDatalink</code>, or <code>SerialJavaObject</code> object.235* <P>236* Note: (1) The <code>Array</code> object from which a <code>SerialArray</code>237* object is created must have materialized the SQL <code>ARRAY</code> value's238* data on the client before it is passed to the constructor. Otherwise,239* the new <code>SerialArray</code> object will contain no data.240* <p>241* Note: (2) The <code>Array</code> object supplied to this constructor cannot242* return <code>null</code> for any <code>Array.getArray()</code> methods.243* <code>SerialArray</code> cannot serialize <code>null</code> array values.244*245* @param array the <code>Array</code> object to be serialized246* @throws SerialException if an error occurs serializing the247* <code>Array</code> object248* @throws SQLException if a database access error occurs or the249* <i>array</i> parameter is <code>null</code>.250*/251public SerialArray(Array array) throws SerialException, SQLException {252if (array == null) {253throw new SQLException("Cannot instantiate a SerialArray " +254"object with a null Array object");255}256257if ((elements = (Object[])array.getArray()) == null) {258throw new SQLException("Invalid Array object. Calls to Array.getArray() " +259"return null value which cannot be serialized");260}261262//elements = (Object[])array.getArray();263baseType = array.getBaseType();264baseTypeName = array.getBaseTypeName();265len = elements.length;266267switch (baseType) {268269case java.sql.Types.BLOB:270for (int i = 0; i < len; i++) {271elements[i] = new SerialBlob((Blob)elements[i]);272}273break;274275case java.sql.Types.CLOB:276for (int i = 0; i < len; i++) {277elements[i] = new SerialClob((Clob)elements[i]);278}279break;280281case java.sql.Types.DATALINK:282for (int i = 0; i < len; i++) {283elements[i] = new SerialDatalink((URL)elements[i]);284}285break;286287case java.sql.Types.JAVA_OBJECT:288for (int i = 0; i < len; i++) {289elements[i] = new SerialJavaObject(elements[i]);290}291break;292293}294295296}297298/**299* Returns a new array that is a copy of this <code>SerialArray</code>300* object.301*302* @return a copy of this <code>SerialArray</code> object as an303* <code>Object</code> in the Java programming language304* @throws SerialException if an error occurs;305* if {@code free} had previously been called on this object306*/307public Object getArray() throws SerialException {308isValid();309Object dst = new Object[len];310System.arraycopy((Object)elements, 0, dst, 0, len);311return dst;312}313314//[if an error occurstype map used??]315/**316* Returns a new array that is a copy of this <code>SerialArray</code>317* object, using the given type map for the custom318* mapping of each element when the elements are SQL UDTs.319* <P>320* This method does custom mapping if the array elements are a UDT321* and the given type map has an entry for that UDT.322* Custom mapping is recursive,323* meaning that if, for instance, an element of an SQL structured type324* is an SQL structured type that itself has an element that is an SQL325* structured type, each structured type that has a custom mapping will be326* mapped according to the given type map.327*328* @param map a <code>java.util.Map</code> object in which329* each entry consists of 1) a <code>String</code> object330* giving the fully qualified name of a UDT and 2) the331* <code>Class</code> object for the <code>SQLData</code> implementation332* that defines how the UDT is to be mapped333* @return a copy of this <code>SerialArray</code> object as an334* <code>Object</code> in the Java programming language335* @throws SerialException if an error occurs;336* if {@code free} had previously been called on this object337*/338public Object getArray(Map<String, Class<?>> map) throws SerialException {339isValid();340Object dst[] = new Object[len];341System.arraycopy((Object)elements, 0, dst, 0, len);342return dst;343}344345/**346* Returns a new array that is a copy of a slice347* of this <code>SerialArray</code> object, starting with the348* element at the given index and containing the given number349* of consecutive elements.350*351* @param index the index into this <code>SerialArray</code> object352* of the first element to be copied;353* the index of the first element is <code>0</code>354* @param count the number of consecutive elements to be copied, starting355* at the given index356* @return a copy of the designated elements in this <code>SerialArray</code>357* object as an <code>Object</code> in the Java programming language358* @throws SerialException if an error occurs;359* if {@code free} had previously been called on this object360*/361public Object getArray(long index, int count) throws SerialException {362isValid();363Object dst = new Object[count];364System.arraycopy((Object)elements, (int)index, dst, 0, count);365return dst;366}367368/**369* Returns a new array that is a copy of a slice370* of this <code>SerialArray</code> object, starting with the371* element at the given index and containing the given number372* of consecutive elements.373* <P>374* This method does custom mapping if the array elements are a UDT375* and the given type map has an entry for that UDT.376* Custom mapping is recursive,377* meaning that if, for instance, an element of an SQL structured type378* is an SQL structured type that itself has an element that is an SQL379* structured type, each structured type that has a custom mapping will be380* mapped according to the given type map.381*382* @param index the index into this <code>SerialArray</code> object383* of the first element to be copied; the index of the384* first element in the array is <code>0</code>385* @param count the number of consecutive elements to be copied, starting386* at the given index387* @param map a <code>java.util.Map</code> object in which388* each entry consists of 1) a <code>String</code> object389* giving the fully qualified name of a UDT and 2) the390* <code>Class</code> object for the <code>SQLData</code> implementation391* that defines how the UDT is to be mapped392* @return a copy of the designated elements in this <code>SerialArray</code>393* object as an <code>Object</code> in the Java programming language394* @throws SerialException if an error occurs;395* if {@code free} had previously been called on this object396*/397public Object getArray(long index, int count, Map<String,Class<?>> map)398throws SerialException399{400isValid();401Object dst = new Object[count];402System.arraycopy((Object)elements, (int)index, dst, 0, count);403return dst;404}405406/**407* Retrieves the SQL type of the elements in this <code>SerialArray</code>408* object. The <code>int</code> returned is one of the constants in the class409* <code>java.sql.Types</code>.410*411* @return one of the constants in <code>java.sql.Types</code>, indicating412* the SQL type of the elements in this <code>SerialArray</code> object413* @throws SerialException if an error occurs;414* if {@code free} had previously been called on this object415*/416public int getBaseType() throws SerialException {417isValid();418return baseType;419}420421/**422* Retrieves the DBMS-specific type name for the elements in this423* <code>SerialArray</code> object.424*425* @return the SQL type name used by the DBMS for the base type of this426* <code>SerialArray</code> object427* @throws SerialException if an error occurs;428* if {@code free} had previously been called on this object429*/430public String getBaseTypeName() throws SerialException {431isValid();432return baseTypeName;433}434435/**436* Retrieves a <code>ResultSet</code> object holding the elements of437* the subarray that starts at438* index <i>index</i> and contains up to <i>count</i> successive elements.439* This method uses the connection's type map to map the elements of440* the array if the map contains441* an entry for the base type. Otherwise, the standard mapping is used.442*443* @param index the index into this <code>SerialArray</code> object444* of the first element to be copied; the index of the445* first element in the array is <code>0</code>446* @param count the number of consecutive elements to be copied, starting447* at the given index448* @return a <code>ResultSet</code> object containing the designated449* elements in this <code>SerialArray</code> object, with a450* separate row for each element451* @throws SerialException if called with the cause set to452* {@code UnsupportedOperationException}453*/454public ResultSet getResultSet(long index, int count) throws SerialException {455SerialException se = new SerialException();456se.initCause(new UnsupportedOperationException());457throw se;458}459460/**461*462* Retrieves a <code>ResultSet</code> object that contains all of463* the elements of the SQL <code>ARRAY</code>464* value represented by this <code>SerialArray</code> object. This method uses465* the specified map for type map customizations unless the base type of the466* array does not match a user-defined type (UDT) in <i>map</i>, in467* which case it uses the468* standard mapping. This version of the method <code>getResultSet</code>469* uses either the given type map or the standard mapping; it never uses the470* type map associated with the connection.471*472* @param map a <code>java.util.Map</code> object in which473* each entry consists of 1) a <code>String</code> object474* giving the fully qualified name of a UDT and 2) the475* <code>Class</code> object for the <code>SQLData</code> implementation476* that defines how the UDT is to be mapped477* @return a <code>ResultSet</code> object containing all of the478* elements in this <code>SerialArray</code> object, with a479* separate row for each element480* @throws SerialException if called with the cause set to481* {@code UnsupportedOperationException}482*/483public ResultSet getResultSet(Map<String, Class<?>> map)484throws SerialException485{486SerialException se = new SerialException();487se.initCause(new UnsupportedOperationException());488throw se;489}490491/**492* Retrieves a <code>ResultSet</code> object that contains all of493* the elements in the <code>ARRAY</code> value that this494* <code>SerialArray</code> object represents.495* If appropriate, the elements of the array are mapped using the connection's496* type map; otherwise, the standard mapping is used.497*498* @return a <code>ResultSet</code> object containing all of the499* elements in this <code>SerialArray</code> object, with a500* separate row for each element501* @throws SerialException if called with the cause set to502* {@code UnsupportedOperationException}503*/504public ResultSet getResultSet() throws SerialException {505SerialException se = new SerialException();506se.initCause(new UnsupportedOperationException());507throw se;508}509510511/**512* Retrieves a result set holding the elements of the subarray that starts at513* Retrieves a <code>ResultSet</code> object that contains a subarray of the514* elements in this <code>SerialArray</code> object, starting at515* index <i>index</i> and containing up to <i>count</i> successive516* elements. This method uses517* the specified map for type map customizations unless the base type of the518* array does not match a user-defined type (UDT) in <i>map</i>, in519* which case it uses the520* standard mapping. This version of the method <code>getResultSet</code> uses521* either the given type map or the standard mapping; it never uses the type522* map associated with the connection.523*524* @param index the index into this <code>SerialArray</code> object525* of the first element to be copied; the index of the526* first element in the array is <code>0</code>527* @param count the number of consecutive elements to be copied, starting528* at the given index529* @param map a <code>java.util.Map</code> object in which530* each entry consists of 1) a <code>String</code> object531* giving the fully qualified name of a UDT and 2) the532* <code>Class</code> object for the <code>SQLData</code> implementation533* that defines how the UDT is to be mapped534* @return a <code>ResultSet</code> object containing the designated535* elements in this <code>SerialArray</code> object, with a536* separate row for each element537* @throws SerialException if called with the cause set to538* {@code UnsupportedOperationException}539*/540public ResultSet getResultSet(long index, int count,541Map<String,Class<?>> map)542throws SerialException543{544SerialException se = new SerialException();545se.initCause(new UnsupportedOperationException());546throw se;547}548549550/**551* Compares this SerialArray to the specified object. The result is {@code552* true} if and only if the argument is not {@code null} and is a {@code553* SerialArray} object whose elements are identical to this object's elements554*555* @param obj The object to compare this {@code SerialArray} against556*557* @return {@code true} if the given object represents a {@code SerialArray}558* equivalent to this SerialArray, {@code false} otherwise559*560*/561public boolean equals(Object obj) {562if (this == obj) {563return true;564}565566if (obj instanceof SerialArray) {567SerialArray sa = (SerialArray)obj;568return baseType == sa.baseType &&569baseTypeName.equals(sa.baseTypeName) &&570Arrays.equals(elements, sa.elements);571}572return false;573}574575/**576* Returns a hash code for this SerialArray. The hash code for a577* {@code SerialArray} object is computed using the hash codes578* of the elements of the {@code SerialArray} object579*580* @return a hash code value for this object.581*/582public int hashCode() {583return (((31 + Arrays.hashCode(elements)) * 31 + len) * 31 +584baseType) * 31 + baseTypeName.hashCode();585}586587/**588* Returns a clone of this {@code SerialArray}. The copy will contain a589* reference to a clone of the underlying objects array, not a reference590* to the original underlying object array of this {@code SerialArray} object.591*592* @return a clone of this SerialArray593*/594public Object clone() {595try {596SerialArray sa = (SerialArray) super.clone();597sa.elements = (elements != null) ? Arrays.copyOf(elements, len) : null;598return sa;599} catch (CloneNotSupportedException ex) {600// this shouldn't happen, since we are Cloneable601throw new InternalError();602}603604}605606/**607* readObject is called to restore the state of the {@code SerialArray} from608* a stream.609*/610private void readObject(ObjectInputStream s)611throws IOException, ClassNotFoundException {612613ObjectInputStream.GetField fields = s.readFields();614Object[] tmp = (Object[])fields.get("elements", null);615if (tmp == null)616throw new InvalidObjectException("elements is null and should not be!");617elements = tmp.clone();618len = fields.get("len", 0);619if(elements.length != len)620throw new InvalidObjectException("elements is not the expected size");621622baseType = fields.get("baseType", 0);623baseTypeName = (String)fields.get("baseTypeName", null);624}625626/**627* writeObject is called to save the state of the {@code SerialArray}628* to a stream.629*/630private void writeObject(ObjectOutputStream s)631throws IOException, ClassNotFoundException {632633ObjectOutputStream.PutField fields = s.putFields();634fields.put("elements", elements);635fields.put("len", len);636fields.put("baseType", baseType);637fields.put("baseTypeName", baseTypeName);638s.writeFields();639}640641/**642* Check to see if this object had previously had its {@code free} method643* called644*645* @throws SerialException646*/647private void isValid() throws SerialException {648if (elements == null) {649throw new SerialException("Error: You cannot call a method on a "650+ "SerialArray instance once free() has been called.");651}652}653654/**655* The identifier that assists in the serialization of this <code>SerialArray</code>656* object.657*/658static final long serialVersionUID = -8466174297270688520L;659}660661662