001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sql.rowset.serial;
019:
020: import java.io.Serializable;
021: import java.sql.Array;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.Types;
025: import java.util.Map;
026:
027: import org.apache.harmony.sql.internal.nls.Messages;
028:
029: /**
030: *
031: * A array that can be serialized
032: *
033: */
034: public class SerialArray implements Array, Serializable, Cloneable {
035:
036: private static final long serialVersionUID = -8466174297270688520L;
037:
038: Object[] elements;
039:
040: int baseType;
041:
042: String baseTypeName;
043:
044: int len;
045:
046: /**
047: * The constructor
048: *
049: * @param array
050: * array to be serializated
051: * @param map
052: * the UDT map
053: * @throws SerialException
054: * when any error occurs during serializing
055: * @throws SQLException
056: * if array is null
057: */
058: public SerialArray(Array array, Map<String, Class<?>> map)
059: throws SerialException, SQLException {
060: if (null == array || null == array.getArray() || null == map) {
061: throw new SQLException(Messages.getString("sql.39")); //$NON-NLS-1$
062: }
063: baseType = array.getBaseType();
064: baseTypeName = array.getBaseTypeName();
065: Object[] element = (Object[]) array.getArray(map);
066: if (element.length == 0) {
067: elements = new Object[0];
068: } else {
069: transferElements(baseType, element);
070: }
071: }
072:
073: /**
074: * Transfers primitive objects to SerialXXX objects according to the given
075: * type.
076: *
077: * @throws SQLException
078: */
079: private void transferElements(int type, Object[] element)
080: throws SQLException {
081: switch (type) {
082: case Types.STRUCT:
083: elements = DefaultUDTMap.processStruct(element);
084: break;
085: case Types.ARRAY:
086: elements = DefaultUDTMap.processArray(element);
087: break;
088: case Types.CLOB:
089: elements = DefaultUDTMap.processClob(element);
090: break;
091: case Types.BLOB:
092: elements = DefaultUDTMap.processBlob(element);
093: break;
094: case Types.DATALINK:
095: elements = DefaultUDTMap.processDatalink(element);
096: break;
097: case Types.JAVA_OBJECT:
098: elements = DefaultUDTMap.processObject(element);
099: break;
100: default:
101: elements = new Object[element.length];
102: for (int i = 0; i < element.length; i++) {
103: elements[i] = element[i];
104: }
105: }
106: }
107:
108: /**
109: * The constructor
110: *
111: * @param array
112: * array to be serializated
113: * @throws SerialException
114: * when any error occurs during serializing
115: * @throws SQLException
116: * if array is null
117: */
118: public SerialArray(Array array) throws SerialException,
119: SQLException {
120: if (null == array || null == array.getArray()) {
121: throw new SQLException(Messages.getString("sql.39")); //$NON-NLS-1$
122: }
123: baseType = array.getBaseType();
124: baseTypeName = array.getBaseTypeName();
125:
126: Object[] element = (Object[]) array.getArray();
127: if (element.length == 0) {
128: elements = new Object[0];
129: } else {
130: transferElements(baseType, element);
131: }
132: }
133:
134: /**
135: * Answers the array that copies the SerialArray object.
136: *
137: * @return the array that copies the SerialArray object.
138: * @throws SerialException
139: * if any error occurs when copy the array
140: */
141: public Object getArray() throws SerialException {
142: Object[] ret = new Object[elements.length];
143: System.arraycopy(elements, 0, ret, 0, elements.length);
144: return elements;
145: }
146:
147: /**
148: * Answers the array that copies the certain elements of the SerialArray
149: * object
150: *
151: * @param index
152: * the start offset
153: * @param count
154: * the length of element to be copied
155: * @return the array that copies the SerialArray object.
156: * @throws SerialException
157: * if any error occurs when copy the array
158: */
159: public Object getArray(long index, int count)
160: throws SerialException {
161: if (index < 0 || count + index > elements.length) {
162: throw new SerialException(Messages.getString("sql.42")); //$NON-NLS-1$
163: }
164: Object[] ret = new Object[count];
165: System.arraycopy(elements, (int) index, ret, 0, count);
166: return ret;
167: }
168:
169: /**
170: * Answers the array that copies the certain elements of the SerialArray
171: * object according to UDT map
172: *
173: * @param index
174: * the start offset
175: * @param count
176: * the length of element to be copied
177: * @param map
178: * the UDT map
179: * @return the array that copies the SerialArray object.
180: * @throws SerialException
181: * if any error occurs when copy the array
182: */
183: public Object getArray(long index, int count,
184: Map<String, Class<?>> map) throws SerialException {
185: if (index < 0 || count + index > elements.length) {
186: throw new SerialException(Messages.getString("sql.40")); //$NON-NLS-1$
187: }
188: Object[] ret = new Object[count];
189: System.arraycopy(elements, (int) index, ret, 0, count);
190: return ret;
191: }
192:
193: /**
194: * Answers the array that copies the SerialArray object according to UDT map
195: *
196: * @param map
197: * the UDT map
198: * @return the array that copies the SerialArray object.
199: * @throws SerialException
200: * if any error occurs when copy the array
201: */
202: public Object getArray(Map<String, Class<?>> map)
203: throws SerialException {
204: return getArray(0, elements.length,
205: null == map ? DefaultUDTMap.DEFAULTMAP : map);
206: }
207:
208: /**
209: * Answers the base type of this array
210: *
211: * @return the base type of this array
212: * @throws SerialException
213: * if any errors occur
214: */
215: public int getBaseType() throws SerialException {
216: return baseType;
217: }
218:
219: /**
220: * Answers the base type name of this array
221: *
222: * @return the base type name of this array
223: * @throws SerialException
224: * if any errors occur
225: */
226: public String getBaseTypeName() throws SerialException {
227: return baseTypeName;
228: }
229:
230: /**
231: * Answers the base type of this array, but throw always
232: * UnsupportedOperationException here
233: *
234: * @return the base type of this array
235: * @throws SerialException
236: * if any errors occur
237: */
238: public ResultSet getResultSet() throws SerialException {
239: throw new UnsupportedOperationException();
240: }
241:
242: /**
243: * Answers the base type of this array, but throw always
244: * UnsupportedOperationException here
245: *
246: * @param index
247: * the start offset
248: * @param count
249: * the length of element to be copied
250: * @return the base type of this array
251: * @throws SerialException
252: * if any errors occur
253: */
254: public ResultSet getResultSet(long index, int count)
255: throws SerialException {
256: throw new UnsupportedOperationException();
257: }
258:
259: /**
260: * Answers the base type of this array, but throw always
261: * UnsupportedOperationException here
262: *
263: * @param index
264: * the start offset
265: * @param count
266: * the length of element to be copied
267: * @param map
268: * the UDT map
269: * @return the base type of this array
270: * @throws SerialException
271: * if any errors occur
272: */
273: public ResultSet getResultSet(long index, int count,
274: Map<String, Class<?>> map) throws SerialException {
275: throw new UnsupportedOperationException();
276: }
277:
278: /**
279: * Answers the base type of this array, but throw always
280: * UnsupportedOperationException here
281: *
282: * @param map
283: * the UDT map
284: * @return the base type of this array
285: * @throws SerialException
286: * if any errors occur
287: */
288: public ResultSet getResultSet(Map<String, Class<?>> map)
289: throws SerialException {
290: throw new UnsupportedOperationException();
291: }
292: }
|