Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialArray.java
40948 views
/*1* Copyright (c) 2003, 2020, 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* <h2> Thread safety </h2>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* @since 1.559*/60public class SerialArray implements Array, Serializable, Cloneable {6162/**63* A serialized array in which each element is an <code>Object</code>64* in the Java programming language that represents an element65* in the SQL <code>ARRAY</code> value.66* @serial67*/68@SuppressWarnings("serial") // Not statically typed as Serializable69private Object[] elements;7071/**72* The SQL type of the elements in this <code>SerialArray</code> object. The73* type is expressed as one of the constants from the class74* <code>java.sql.Types</code>.75* @serial76*/77private int baseType;7879/**80* The type name used by the DBMS for the elements in the SQL <code>ARRAY</code>81* value that this <code>SerialArray</code> object represents.82* @serial83*/84private String baseTypeName;8586/**87* The number of elements in this <code>SerialArray</code> object, which88* is also the number of elements in the SQL <code>ARRAY</code> value89* that this <code>SerialArray</code> object represents.90* @serial91*/92private int len;9394/**95* Constructs a new <code>SerialArray</code> object from the given96* <code>Array</code> object, using the given type map for the custom97* mapping of each element when the elements are SQL UDTs.98* <P>99* This method does custom mapping if the array elements are a UDT100* and the given type map has an entry for that UDT.101* Custom mapping is recursive,102* meaning that if, for instance, an element of an SQL structured type103* is an SQL structured type that itself has an element that is an SQL104* structured type, each structured type that has a custom mapping will be105* mapped according to the given type map.106* <P>107* The new <code>SerialArray</code>108* object contains the same elements as the <code>Array</code> object109* from which it is built, except when the base type is the SQL type110* <code>STRUCT</code>, <code>ARRAY</code>, <code>BLOB</code>,111* <code>CLOB</code>, <code>DATALINK</code> or <code>JAVA_OBJECT</code>.112* In this case, each element in the new113* <code>SerialArray</code> object is the appropriate serialized form,114* that is, a <code>SerialStruct</code>, <code>SerialArray</code>,115* <code>SerialBlob</code>, <code>SerialClob</code>,116* <code>SerialDatalink</code>, or <code>SerialJavaObject</code> object.117* <P>118* Note: (1) The <code>Array</code> object from which a <code>SerialArray</code>119* object is created must have materialized the SQL <code>ARRAY</code> value's120* data on the client before it is passed to the constructor. Otherwise,121* the new <code>SerialArray</code> object will contain no data.122* <p>123* Note: (2) If the <code>Array</code> contains <code>java.sql.Types.JAVA_OBJECT</code>124* types, the <code>SerialJavaObject</code> constructor is called where checks125* are made to ensure this object is serializable.126* <p>127* Note: (3) The <code>Array</code> object supplied to this constructor cannot128* return <code>null</code> for any <code>Array.getArray()</code> methods.129* <code>SerialArray</code> cannot serialize null array values.130*131*132* @param array the <code>Array</code> object to be serialized133* @param map a <code>java.util.Map</code> object in which134* each entry consists of 1) a <code>String</code> object135* giving the fully qualified name of a UDT (an SQL structured type or136* distinct type) and 2) the137* <code>Class</code> object for the <code>SQLData</code> implementation138* that defines how the UDT is to be mapped. The <i>map</i>139* parameter does not have any effect for <code>Blob</code>,140* <code>Clob</code>, <code>DATALINK</code>, or141* <code>JAVA_OBJECT</code> types.142* @throws SerialException if an error occurs serializing the143* <code>Array</code> object144* @throws SQLException if a database access error occurs or if the145* <i>array</i> or the <i>map</i> values are <code>null</code>146*/147public SerialArray(Array array, Map<String,Class<?>> map)148throws SerialException, SQLException149{150151if ((array == null) || (map == null)) {152throw new SQLException("Cannot instantiate a SerialArray " +153"object with null parameters");154}155156if ((elements = (Object[])array.getArray()) == null) {157throw new SQLException("Invalid Array object. Calls to Array.getArray() " +158"return null value which cannot be serialized");159}160161elements = (Object[])array.getArray(map);162baseType = array.getBaseType();163baseTypeName = array.getBaseTypeName();164len = elements.length;165166switch (baseType) {167case java.sql.Types.STRUCT:168for (int i = 0; i < len; i++) {169elements[i] = new SerialStruct((Struct)elements[i], map);170}171break;172173case java.sql.Types.ARRAY:174for (int i = 0; i < len; i++) {175elements[i] = new SerialArray((Array)elements[i], map);176}177break;178179case java.sql.Types.BLOB:180for (int i = 0; i < len; i++) {181elements[i] = new SerialBlob((Blob)elements[i]);182}183break;184185case java.sql.Types.CLOB:186for (int i = 0; i < len; i++) {187elements[i] = new SerialClob((Clob)elements[i]);188}189break;190191case java.sql.Types.DATALINK:192for (int i = 0; i < len; i++) {193elements[i] = new SerialDatalink((URL)elements[i]);194}195break;196197case java.sql.Types.JAVA_OBJECT:198for (int i = 0; i < len; i++) {199elements[i] = new SerialJavaObject(elements[i]);200}201}202}203204/**205* This method frees the {@code SerialArray} object and releases the206* resources that it holds. The object is invalid once the {@code free}207* method is called. <p> If {@code free} is called multiple times, the208* subsequent calls to {@code free} are treated as a no-op. </P>209*210* @throws SQLException if an error occurs releasing the SerialArray's resources211* @since 1.6212*/213public void free() throws SQLException {214if (elements != null) {215elements = null;216baseTypeName= null;217}218}219220/**221* Constructs a new <code>SerialArray</code> object from the given222* <code>Array</code> object.223* <P>224* This constructor does not do custom mapping. If the base type of the array225* is an SQL structured type and custom mapping is desired, the constructor226* <code>SerialArray(Array array, Map map)</code> should be used.227* <P>228* The new <code>SerialArray</code>229* object contains the same elements as the <code>Array</code> object230* from which it is built, except when the base type is the SQL type231* <code>BLOB</code>,232* <code>CLOB</code>, <code>DATALINK</code> or <code>JAVA_OBJECT</code>.233* In this case, each element in the new234* <code>SerialArray</code> object is the appropriate serialized form,235* that is, a <code>SerialBlob</code>, <code>SerialClob</code>,236* <code>SerialDatalink</code>, or <code>SerialJavaObject</code> object.237* <P>238* Note: (1) The <code>Array</code> object from which a <code>SerialArray</code>239* object is created must have materialized the SQL <code>ARRAY</code> value's240* data on the client before it is passed to the constructor. Otherwise,241* the new <code>SerialArray</code> object will contain no data.242* <p>243* Note: (2) The <code>Array</code> object supplied to this constructor cannot244* return <code>null</code> for any <code>Array.getArray()</code> methods.245* <code>SerialArray</code> cannot serialize <code>null</code> array values.246*247* @param array the <code>Array</code> object to be serialized248* @throws SerialException if an error occurs serializing the249* <code>Array</code> object250* @throws SQLException if a database access error occurs or the251* <i>array</i> parameter is <code>null</code>.252*/253public SerialArray(Array array) throws SerialException, SQLException {254if (array == null) {255throw new SQLException("Cannot instantiate a SerialArray " +256"object with a null Array object");257}258259if ((elements = (Object[])array.getArray()) == null) {260throw new SQLException("Invalid Array object. Calls to Array.getArray() " +261"return null value which cannot be serialized");262}263264//elements = (Object[])array.getArray();265baseType = array.getBaseType();266baseTypeName = array.getBaseTypeName();267len = elements.length;268269switch (baseType) {270271case java.sql.Types.BLOB:272for (int i = 0; i < len; i++) {273elements[i] = new SerialBlob((Blob)elements[i]);274}275break;276277case java.sql.Types.CLOB:278for (int i = 0; i < len; i++) {279elements[i] = new SerialClob((Clob)elements[i]);280}281break;282283case java.sql.Types.DATALINK:284for (int i = 0; i < len; i++) {285elements[i] = new SerialDatalink((URL)elements[i]);286}287break;288289case java.sql.Types.JAVA_OBJECT:290for (int i = 0; i < len; i++) {291elements[i] = new SerialJavaObject(elements[i]);292}293break;294295}296297298}299300/**301* Returns a new array that is a copy of this <code>SerialArray</code>302* object.303*304* @return a copy of this <code>SerialArray</code> object as an305* <code>Object</code> in the Java programming language306* @throws SerialException if an error occurs;307* if {@code free} had previously been called on this object308*/309public Object getArray() throws SerialException {310isValid();311Object dst = new Object[len];312System.arraycopy((Object)elements, 0, dst, 0, len);313return dst;314}315316/**317* Returns a new array that is a copy of this <code>SerialArray</code>318* object, using the given type map for the custom319* mapping of each element when the elements are SQL UDTs.320* <P>321* This method does custom mapping if the array elements are a UDT322* and the given type map has an entry for that UDT.323* Custom mapping is recursive,324* meaning that if, for instance, an element of an SQL structured type325* is an SQL structured type that itself has an element that is an SQL326* structured type, each structured type that has a custom mapping will be327* mapped according to the given type map.328*329* @param map a <code>java.util.Map</code> object in which330* each entry consists of 1) a <code>String</code> object331* giving the fully qualified name of a UDT and 2) the332* <code>Class</code> object for the <code>SQLData</code> implementation333* that defines how the UDT is to be mapped334* @return a copy of this <code>SerialArray</code> object as an335* <code>Object</code> in the Java programming language336* @throws SerialException if an error occurs;337* if {@code free} had previously been called on this object338*/339public Object getArray(Map<String, Class<?>> map) throws SerialException {340isValid();341Object dst[] = new Object[len];342System.arraycopy((Object)elements, 0, dst, 0, len);343return dst;344}345346/**347* Returns a new array that is a copy of a slice348* of this <code>SerialArray</code> object, starting with the349* element at the given index and containing the given number350* of consecutive elements.351*352* @param index the index into this <code>SerialArray</code> object353* of the first element to be copied;354* the index of the first element is <code>0</code>355* @param count the number of consecutive elements to be copied, starting356* at the given index357* @return a copy of the designated elements in this <code>SerialArray</code>358* object as an <code>Object</code> in the Java programming language359* @throws SerialException if an error occurs;360* if {@code free} had previously been called on this object361*/362public Object getArray(long index, int count) throws SerialException {363isValid();364Object dst = new Object[count];365System.arraycopy((Object)elements, (int)index, dst, 0, count);366return dst;367}368369/**370* Returns a new array that is a copy of a slice371* of this <code>SerialArray</code> object, starting with the372* element at the given index and containing the given number373* of consecutive elements.374* <P>375* This method does custom mapping if the array elements are a UDT376* and the given type map has an entry for that UDT.377* Custom mapping is recursive,378* meaning that if, for instance, an element of an SQL structured type379* is an SQL structured type that itself has an element that is an SQL380* structured type, each structured type that has a custom mapping will be381* mapped according to the given type map.382*383* @param index the index into this <code>SerialArray</code> object384* of the first element to be copied; the index of the385* first element in the array is <code>0</code>386* @param count the number of consecutive elements to be copied, starting387* at the given index388* @param map a <code>java.util.Map</code> object in which389* each entry consists of 1) a <code>String</code> object390* giving the fully qualified name of a UDT and 2) the391* <code>Class</code> object for the <code>SQLData</code> implementation392* that defines how the UDT is to be mapped393* @return a copy of the designated elements in this <code>SerialArray</code>394* object as an <code>Object</code> in the Java programming language395* @throws SerialException if an error occurs;396* if {@code free} had previously been called on this object397*/398public Object getArray(long index, int count, Map<String,Class<?>> map)399throws SerialException400{401isValid();402Object dst = new Object[count];403System.arraycopy((Object)elements, (int)index, dst, 0, count);404return dst;405}406407/**408* Retrieves the SQL type of the elements in this <code>SerialArray</code>409* object. The <code>int</code> returned is one of the constants in the class410* <code>java.sql.Types</code>.411*412* @return one of the constants in <code>java.sql.Types</code>, indicating413* the SQL type of the elements in this <code>SerialArray</code> object414* @throws SerialException if an error occurs;415* if {@code free} had previously been called on this object416*/417public int getBaseType() throws SerialException {418isValid();419return baseType;420}421422/**423* Retrieves the DBMS-specific type name for the elements in this424* <code>SerialArray</code> object.425*426* @return the SQL type name used by the DBMS for the base type of this427* <code>SerialArray</code> object428* @throws SerialException if an error occurs;429* if {@code free} had previously been called on this object430*/431public String getBaseTypeName() throws SerialException {432isValid();433return baseTypeName;434}435436/**437* Retrieves a <code>ResultSet</code> object holding the elements of438* the subarray that starts at439* index <i>index</i> and contains up to <i>count</i> successive elements.440* This method uses the connection's type map to map the elements of441* the array if the map contains442* an entry for the base type. Otherwise, the standard mapping is used.443*444* @param index the index into this <code>SerialArray</code> object445* of the first element to be copied; the index of the446* first element in the array is <code>0</code>447* @param count the number of consecutive elements to be copied, starting448* at the given index449* @return a <code>ResultSet</code> object containing the designated450* elements in this <code>SerialArray</code> object, with a451* separate row for each element452* @throws SerialException if called with the cause set to453* {@code UnsupportedOperationException}454*/455public ResultSet getResultSet(long index, int count) throws SerialException {456SerialException se = new SerialException();457se.initCause(new UnsupportedOperationException());458throw se;459}460461/**462*463* Retrieves a <code>ResultSet</code> object that contains all of464* the elements of the SQL <code>ARRAY</code>465* value represented by this <code>SerialArray</code> object. This method uses466* the specified map for type map customizations unless the base type of the467* array does not match a user-defined type (UDT) in <i>map</i>, in468* which case it uses the469* standard mapping. This version of the method <code>getResultSet</code>470* uses either the given type map or the standard mapping; it never uses the471* type map associated with the connection.472*473* @param map a <code>java.util.Map</code> object in which474* each entry consists of 1) a <code>String</code> object475* giving the fully qualified name of a UDT and 2) the476* <code>Class</code> object for the <code>SQLData</code> implementation477* that defines how the UDT is to be mapped478* @return a <code>ResultSet</code> object containing all of the479* elements in this <code>SerialArray</code> object, with a480* separate row for each element481* @throws SerialException if called with the cause set to482* {@code UnsupportedOperationException}483*/484public ResultSet getResultSet(Map<String, Class<?>> map)485throws SerialException486{487SerialException se = new SerialException();488se.initCause(new UnsupportedOperationException());489throw se;490}491492/**493* Retrieves a <code>ResultSet</code> object that contains all of494* the elements in the <code>ARRAY</code> value that this495* <code>SerialArray</code> object represents.496* If appropriate, the elements of the array are mapped using the connection's497* type map; otherwise, the standard mapping is used.498*499* @return a <code>ResultSet</code> object containing all of the500* elements in this <code>SerialArray</code> object, with a501* separate row for each element502* @throws SerialException if called with the cause set to503* {@code UnsupportedOperationException}504*/505public ResultSet getResultSet() throws SerialException {506SerialException se = new SerialException();507se.initCause(new UnsupportedOperationException());508throw se;509}510511512/**513* Retrieves a result set holding the elements of the subarray that starts at514* Retrieves a <code>ResultSet</code> object that contains a subarray of the515* elements in this <code>SerialArray</code> object, starting at516* index <i>index</i> and containing up to <i>count</i> successive517* elements. This method uses518* the specified map for type map customizations unless the base type of the519* array does not match a user-defined type (UDT) in <i>map</i>, in520* which case it uses the521* standard mapping. This version of the method <code>getResultSet</code> uses522* either the given type map or the standard mapping; it never uses the type523* map associated with the connection.524*525* @param index the index into this <code>SerialArray</code> object526* of the first element to be copied; the index of the527* first element in the array is <code>0</code>528* @param count the number of consecutive elements to be copied, starting529* at the given index530* @param map a <code>java.util.Map</code> object in which531* each entry consists of 1) a <code>String</code> object532* giving the fully qualified name of a UDT and 2) the533* <code>Class</code> object for the <code>SQLData</code> implementation534* that defines how the UDT is to be mapped535* @return a <code>ResultSet</code> object containing the designated536* elements in this <code>SerialArray</code> object, with a537* separate row for each element538* @throws SerialException if called with the cause set to539* {@code UnsupportedOperationException}540*/541public ResultSet getResultSet(long index, int count,542Map<String,Class<?>> map)543throws SerialException544{545SerialException se = new SerialException();546se.initCause(new UnsupportedOperationException());547throw se;548}549550551/**552* Compares this SerialArray to the specified object. The result is {@code553* true} if and only if the argument is not {@code null} and is a {@code554* SerialArray} object whose elements are identical to this object's elements555*556* @param obj The object to compare this {@code SerialArray} against557*558* @return {@code true} if the given object represents a {@code SerialArray}559* equivalent to this SerialArray, {@code false} otherwise560*561*/562public boolean equals(Object obj) {563if (this == obj) {564return true;565}566567if (obj instanceof SerialArray) {568SerialArray sa = (SerialArray)obj;569return baseType == sa.baseType &&570baseTypeName.equals(sa.baseTypeName) &&571Arrays.equals(elements, sa.elements);572}573return false;574}575576/**577* Returns a hash code for this SerialArray. The hash code for a578* {@code SerialArray} object is computed using the hash codes579* of the elements of the {@code SerialArray} object580*581* @return a hash code value for this object.582*/583public int hashCode() {584return (((31 + Arrays.hashCode(elements)) * 31 + len) * 31 +585baseType) * 31 + baseTypeName.hashCode();586}587588/**589* Returns a clone of this {@code SerialArray}. The copy will contain a590* reference to a clone of the underlying objects array, not a reference591* to the original underlying object array of this {@code SerialArray} object.592*593* @return a clone of this SerialArray594*/595public Object clone() {596try {597SerialArray sa = (SerialArray) super.clone();598sa.elements = (elements != null) ? Arrays.copyOf(elements, len) : null;599return sa;600} catch (CloneNotSupportedException ex) {601// this shouldn't happen, since we are Cloneable602throw new InternalError();603}604605}606607/**608* readObject is called to restore the state of the {@code SerialArray} from609* a stream.610* @param s the {@code ObjectInputStream} to read from.611*612* @throws ClassNotFoundException if the class of a serialized object613* could not be found.614* @throws IOException if an I/O error occurs.615*/616private void readObject(ObjectInputStream s)617throws IOException, ClassNotFoundException {618619ObjectInputStream.GetField fields = s.readFields();620Object[] tmp = (Object[])fields.get("elements", null);621if (tmp == null)622throw new InvalidObjectException("elements is null and should not be!");623elements = tmp.clone();624len = fields.get("len", 0);625if(elements.length != len)626throw new InvalidObjectException("elements is not the expected size");627628baseType = fields.get("baseType", 0);629baseTypeName = (String)fields.get("baseTypeName", null);630}631632/**633* writeObject is called to save the state of the {@code SerialArray}634* to a stream.635* @param s the {@code ObjectOutputStream} to write to.636* @throws IOException if an I/O error occurs.637*/638private void writeObject(ObjectOutputStream s)639throws IOException {640641ObjectOutputStream.PutField fields = s.putFields();642fields.put("elements", elements);643fields.put("len", len);644fields.put("baseType", baseType);645fields.put("baseTypeName", baseTypeName);646s.writeFields();647}648649/**650* Check to see if this object had previously had its {@code free} method651* called652*653* @throws SerialException654*/655private void isValid() throws SerialException {656if (elements == null) {657throw new SerialException("Error: You cannot call a method on a "658+ "SerialArray instance once free() has been called.");659}660}661662/**663* The identifier that assists in the serialization of this <code>SerialArray</code>664* object.665*/666static final long serialVersionUID = -8466174297270688520L;667}668669670