001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.ArrayUtil
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.sanity.SanityManager;
025: import java.io.ObjectOutput;
026: import java.io.ObjectInput;
027: import java.io.IOException;
028: import java.lang.reflect.Array;
029:
030: /**
031: Utility class for constructing and reading and writing arrays from/to
032: formatId streams.
033:
034: @version 0.1
035: @author Rick Hillegas
036: */
037: public abstract class ArrayUtil {
038: ///////////////////////////////////////////////////////////////////
039: //
040: // Methods for Arrays of OBJECTS. Cannot be used for an
041: // array of primitives, see below for something for primitives
042: //
043: ///////////////////////////////////////////////////////////////////
044: /**
045: Write the length of an array of objects to an output stream.
046:
047: The length
048:
049: @param out ObjectOutput stream
050: @param a array of objects whose length should be written.
051:
052: @exception java.io.IOException The write caused an IOException.
053: */
054: public static void writeArrayLength(ObjectOutput out, Object[] a)
055: throws IOException {
056: out.writeInt(a.length);
057: }
058:
059: /**
060: Write an array of objects to an output stream.
061:
062: @param out Object output stream to write to.
063: @param a array of objects to write.
064:
065: @exception java.io.IOException The write caused an IOException.
066: */
067: public static void writeArrayItems(ObjectOutput out, Object[] a)
068: throws IOException {
069: if (a == null)
070: return;
071:
072: for (int ix = 0; ix < a.length; ix++) {
073: out.writeObject(a[ix]);
074: }
075: }
076:
077: /**
078: Write an array of objects and length to an output stream.
079: Does equivalent of writeArrayLength() followed by writeArrayItems()
080:
081: @param out Object output stream to write to.
082: @param a array of objects to write.
083:
084: @exception java.io.IOException The write caused an IOException.
085: */
086: public static void writeArray(ObjectOutput out, Object[] a)
087: throws IOException {
088: if (a == null) {
089: out.writeInt(0);
090: return;
091: }
092:
093: out.writeInt(a.length);
094: for (int ix = 0; ix < a.length; ix++) {
095: out.writeObject(a[ix]);
096: }
097: }
098:
099: /**
100: Read an array of objects out of a stream.
101:
102: @param in Input stream
103: @param a array to read into
104:
105: @exception java.io.IOException The write caused an IOException.
106: @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist
107: */
108: public static void readArrayItems(ObjectInput in, Object[] a)
109: throws IOException, ClassNotFoundException {
110: for (int ix = 0; ix < a.length; ix++) {
111: a[ix] = in.readObject();
112: }
113: }
114:
115: /**
116: Read the length of an array of objects in an object stream.
117:
118: @param in Input stream.
119:
120: @return length of the array of objects
121:
122: @exception java.io.IOException The write caused an IOException.
123: */
124: public static int readArrayLength(ObjectInput in)
125: throws IOException {
126: return in.readInt();
127: }
128:
129: /**
130: Reads an array of objects from the stream.
131:
132: @param in Input stream
133:
134: @exception java.io.IOException The write caused an IOException.
135: @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist
136: */
137: public static Object[] readObjectArray(ObjectInput in)
138: throws IOException, ClassNotFoundException {
139: int size = in.readInt();
140: if (size == 0) {
141: return null;
142: }
143:
144: Object[] result = new Object[size];
145:
146: readArrayItems(in, result);
147:
148: return result;
149: }
150:
151: ///////////////////////////////////////////////////////////////////
152: //
153: // Methods for Arrays of INTs
154: //
155: ///////////////////////////////////////////////////////////////////
156:
157: /**
158: Write an array of integers to an ObjectOutput. This writes the array
159: in a format readIntArray understands.
160:
161: @param out the ObjectOutput.
162: @param a the array.
163: @exception java.io.IOException The write caused an IOException.
164: */
165: public static void writeIntArray(ObjectOutput out, int[] a)
166: throws IOException {
167: if (a == null)
168: out.writeInt(0);
169: else {
170: out.writeInt(a.length);
171: for (int i = 0; i < a.length; i++)
172: out.writeInt(a[i]);
173: }
174: }
175:
176: /**
177: Read an array of integers from an ObjectInput. This allocates the
178: array.
179:
180: @param in the ObjectInput.
181: @return the array of integers.
182:
183: @exception java.io.IOException The write caused an IOException.
184: */
185: public static int[] readIntArray(ObjectInput in) throws IOException {
186: int length = in.readInt();
187: if (length == 0)
188: return null;
189: int[] a = new int[length];
190: for (int i = 0; i < length; i++)
191: a[i] = in.readInt();
192: return a;
193: }
194:
195: public static void writeInts(ObjectOutput out, int[][] val)
196: throws IOException {
197: if (val == null) {
198: out.writeBoolean(false);
199: } else {
200: out.writeBoolean(true);
201:
202: int count = val.length;
203: out.writeInt(count);
204:
205: for (int i = 0; i < count; i++) {
206: ArrayUtil.writeIntArray(out, val[i]);
207: }
208: }
209: }
210:
211: public static int[][] readInts(ObjectInput in) throws IOException,
212: ClassNotFoundException {
213: int[][] retVal = null;
214:
215: if (in.readBoolean()) {
216: int count = in.readInt();
217:
218: retVal = new int[count][];
219:
220: for (int i = 0; i < count; i++) {
221: retVal[i] = ArrayUtil.readIntArray(in);
222: }
223: }
224:
225: return retVal;
226: }
227:
228: ///////////////////////////////////////////////////////////////////
229: //
230: // Methods for Arrays of LONGs
231: //
232: ///////////////////////////////////////////////////////////////////
233:
234: /**
235: Write an array of longs to an ObjectOutput. This writes the array
236: in a format readLongArray understands.
237:
238: @param out the ObjectOutput.
239: @param a the array.
240: @exception java.io.IOException The write caused an IOException.
241: */
242: public static void writeLongArray(ObjectOutput out, long[] a)
243: throws IOException {
244: if (a == null)
245: out.writeInt(0);
246: else {
247: out.writeInt(a.length);
248: for (int i = 0; i < a.length; i++)
249: out.writeLong(a[i]);
250: }
251: }
252:
253: /**
254: Read an array of integers from an ObjectInput. This allocates the
255: array.
256:
257: @param in the ObjectInput.
258: @return the array of integers.
259:
260: @exception java.io.IOException The write caused an IOException.
261: */
262: public static long[] readLongArray(ObjectInput in)
263: throws IOException {
264: int length = in.readInt();
265: long[] a = new long[length];
266: for (int i = 0; i < length; i++)
267: a[i] = in.readLong();
268: return a;
269: }
270:
271: /**
272: Read an array of strings from an ObjectInput. This allocates the
273: array.
274:
275: @param in the ObjectInput.
276: @return the array of integers.
277:
278: @exception java.io.IOException The write caused an IOException.
279: */
280: public static String[] readStringArray(ObjectInput in)
281: throws IOException, ClassNotFoundException {
282: Object[] objArray = readObjectArray(in);
283: int size = 0;
284:
285: if (objArray == null)
286: return null;
287:
288: String[] stringArray = new String[size = objArray.length];
289:
290: for (int i = 0; i < size; i++) {
291: stringArray[i] = (String) objArray[i];
292: }
293:
294: return stringArray;
295: }
296:
297: ///////////////////////////////////////////////////////////////////
298: //
299: // Methods for Arrays of BOOLEANS
300: //
301: ///////////////////////////////////////////////////////////////////
302:
303: /**
304: Write an array of booleans to an ObjectOutput. This writes the array
305: in a format readBooleanArray understands.
306:
307: @param out the ObjectOutput.
308: @param a the array.
309: @exception java.io.IOException The write caused an IOException.
310: */
311: public static void writeBooleanArray(ObjectOutput out, boolean[] a)
312: throws IOException {
313: if (a == null)
314: out.writeInt(0);
315: else {
316: out.writeInt(a.length);
317: for (int i = 0; i < a.length; i++)
318: out.writeBoolean(a[i]);
319: }
320: }
321:
322: /**
323: Read an array of integers from an ObjectInput. This allocates the
324: array.
325:
326: @param in the ObjectInput.
327: @return the array of integers.
328:
329: @exception java.io.IOException The write caused an IOException.
330: */
331: public static boolean[] readBooleanArray(ObjectInput in)
332: throws IOException {
333: int length = in.readInt();
334: boolean[] a = new boolean[length];
335: for (int i = 0; i < length; i++)
336: a[i] = in.readBoolean();
337: return a;
338: }
339: }
|