001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.FormatableArrayHolder
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.io;
023:
024: import org.apache.derby.iapi.services.io.ArrayInputStream;
025:
026: import org.apache.derby.iapi.services.io.StoredFormatIds;
027: import org.apache.derby.iapi.services.io.FormatIdUtil;
028: import org.apache.derby.iapi.services.io.ArrayUtil;
029: import org.apache.derby.iapi.services.io.Formatable;
030:
031: import org.apache.derby.iapi.services.sanity.SanityManager;
032:
033: import java.io.ObjectOutput;
034: import java.io.ObjectInput;
035: import java.io.IOException;
036:
037: import java.lang.reflect.Array;
038:
039: /**
040: * A formatable holder for an array of formatables.
041: * Used to avoid serializing arrays.
042: */
043: public class FormatableArrayHolder implements Formatable {
044: /********************************************************
045: **
046: ** This class implements Formatable. That means that it
047: ** can write itself to and from a formatted stream. If
048: ** you add more fields to this class, make sure that you
049: ** also write/read them with the writeExternal()/readExternal()
050: ** methods.
051: **
052: ** If, inbetween releases, you add more fields to this class,
053: ** then you should bump the version number emitted by the getTypeFormatId()
054: ** method.
055: **
056: ********************************************************/
057:
058: // the array
059: private Object[] array;
060:
061: /**
062: * Niladic constructor for formatable
063: */
064: public FormatableArrayHolder() {
065: }
066:
067: /**
068: * Construct a FormatableArrayHolder using the input
069: * array.
070: *
071: * @param array the array to hold
072: */
073: public FormatableArrayHolder(Object[] array) {
074: if (SanityManager.DEBUG) {
075: SanityManager
076: .ASSERT(array != null,
077: "array input to constructor is null, code can't handle this.");
078: }
079:
080: this .array = array;
081: }
082:
083: /**
084: * Set the held array to the input array.
085: *
086: * @param array the array to hold
087: */
088: public void setArray(Object[] array) {
089: if (SanityManager.DEBUG) {
090: SanityManager
091: .ASSERT(array != null,
092: "array input to setArray() is null, code can't handle this.");
093: }
094:
095: this .array = array;
096: }
097:
098: /**
099: * Get the held array of formatables, and return
100: * it in an array of type inputClass.
101: *
102: * @param inputClass the class to use for the returned array
103: *
104: * @return an array of formatables
105: */
106: public Object[] getArray(Class inputClass) {
107: Object[] outArray = (Object[]) Array.newInstance(inputClass,
108: array.length);
109:
110: /*
111: ** HACK: on as400 the following arraycopy() throws an
112: ** ArrayStoreException because the output array isn't
113: ** assignment compatible with the input array. This
114: ** is a bug on as400, but to get around it we are
115: ** going to do an element by element copy.
116: */
117: //System.arraycopy(array, 0, outArray, 0, outArray.length);
118: for (int i = 0; i < outArray.length; i++) {
119: outArray[i] = array[i];
120: }
121:
122: return outArray;
123: }
124:
125: //////////////////////////////////////////////
126: //
127: // FORMATABLE
128: //
129: //////////////////////////////////////////////
130: /**
131: * Write this array out
132: *
133: * @param out write bytes here
134: *
135: * @exception IOException thrown on error
136: */
137: public void writeExternal(ObjectOutput out) throws IOException {
138: if (SanityManager.DEBUG) {
139: SanityManager.ASSERT(array != null,
140: "Array is null, which isn't expected");
141: }
142:
143: ArrayUtil.writeArrayLength(out, array);
144: ArrayUtil.writeArrayItems(out, array);
145: }
146:
147: /**
148: * Read this array from a stream of stored objects.
149: *
150: * @param in read this.
151: *
152: * @exception IOException thrown on error
153: * @exception ClassNotFoundException thrown on error
154: */
155: public void readExternal(ObjectInput in) throws IOException,
156: ClassNotFoundException {
157: array = new Object[ArrayUtil.readArrayLength(in)];
158: ArrayUtil.readArrayItems(in, array);
159: }
160:
161: public void readExternal(ArrayInputStream in) throws IOException,
162: ClassNotFoundException {
163: array = new Formatable[ArrayUtil.readArrayLength(in)];
164: ArrayUtil.readArrayItems(in, array);
165: }
166:
167: /**
168: * Get the formatID which corresponds to this class.
169: *
170: * @return the formatID of this class
171: */
172: public int getTypeFormatId() {
173: return StoredFormatIds.FORMATABLE_ARRAY_HOLDER_V01_ID;
174: }
175: }
|