KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > ArrayUtil


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.ArrayUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import java.io.ObjectOutput JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Array JavaDoc;
29
30 /**
31   Utility class for constructing and reading and writing arrays from/to
32   formatId streams.
33  
34   @version 0.1
35   @author Rick Hillegas
36  */

37 public abstract class ArrayUtil
38 {
39     ///////////////////////////////////////////////////////////////////
40
//
41
// Methods for Arrays of OBJECTS. Cannot be used for an
42
// array of primitives, see below for something for primitives
43
//
44
///////////////////////////////////////////////////////////////////
45
/**
46       Write the length of an array of objects to an output stream.
47
48       The length
49
50       @param out ObjectOutput stream
51       @param a array of objects whose length should be written.
52
53       @exception java.io.IOException The write caused an IOException.
54       */

55     public static void writeArrayLength(ObjectOutput JavaDoc out, Object JavaDoc[] a)
56          throws IOException JavaDoc
57     {
58         out.writeInt(a.length);
59     }
60
61     /**
62       Write an array of objects to an output stream.
63
64       @param out Object output stream to write to.
65       @param a array of objects to write.
66
67       @exception java.io.IOException The write caused an IOException.
68       */

69     public static void writeArrayItems(ObjectOutput JavaDoc out, Object JavaDoc[] a)
70          throws IOException JavaDoc
71     {
72         if (a == null)
73             return;
74
75         for(int ix = 0; ix < a.length; ix++)
76         { out.writeObject(a[ix]); }
77     }
78
79     /**
80       Write an array of objects and length to an output stream.
81       Does equivalent of writeArrayLength() followed by writeArrayItems()
82
83       @param out Object output stream to write to.
84       @param a array of objects to write.
85
86       @exception java.io.IOException The write caused an IOException.
87       */

88     public static void writeArray(ObjectOutput JavaDoc out, Object JavaDoc[] a)
89          throws IOException JavaDoc
90     {
91         if (a == null)
92         {
93             out.writeInt(0);
94             return;
95         }
96
97         out.writeInt(a.length);
98         for(int ix = 0; ix < a.length; ix++)
99         { out.writeObject(a[ix]); }
100     }
101
102     /**
103       Read an array of objects out of a stream.
104
105       @param in Input stream
106       @param a array to read into
107
108       @exception java.io.IOException The write caused an IOException.
109       @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist
110       */

111     public static void readArrayItems(ObjectInput JavaDoc in, Object JavaDoc[] a)
112          throws IOException JavaDoc, ClassNotFoundException JavaDoc
113     {
114         for (int ix=0; ix<a.length; ix++)
115         {
116             a[ix]=in.readObject();
117         }
118     }
119
120     /**
121       Read the length of an array of objects in an object stream.
122
123       @param in Input stream.
124
125       @return length of the array of objects
126       
127       @exception java.io.IOException The write caused an IOException.
128       */

129     public static int readArrayLength(ObjectInput JavaDoc in)
130          throws IOException JavaDoc
131     {
132         return in.readInt();
133     }
134
135     /**
136       Reads an array of objects from the stream.
137
138       @param in Input stream
139
140       @exception java.io.IOException The write caused an IOException.
141       @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist
142       */

143     public static Object JavaDoc[] readObjectArray(ObjectInput JavaDoc in)
144          throws IOException JavaDoc, ClassNotFoundException JavaDoc
145     {
146         int size = in.readInt();
147         if ( size == 0 ) { return null; }
148
149         Object JavaDoc[] result = new Object JavaDoc[ size ];
150
151         readArrayItems( in, result );
152
153         return result;
154     }
155
156     ///////////////////////////////////////////////////////////////////
157
//
158
// Methods for Arrays of INTs
159
//
160
///////////////////////////////////////////////////////////////////
161

162     /**
163       Write an array of integers to an ObjectOutput. This writes the array
164       in a format readIntArray understands.
165
166       @param out the ObjectOutput.
167       @param a the array.
168       @exception java.io.IOException The write caused an IOException.
169       */

170     public static void writeIntArray(ObjectOutput JavaDoc out, int[] a) throws IOException JavaDoc {
171         if (a == null)
172             out.writeInt(0);
173         else {
174             out.writeInt(a.length);
175             for (int i=0; i<a.length; i++)
176                 out.writeInt(a[i]);
177         }
178     }
179
180     /**
181       Read an array of integers from an ObjectInput. This allocates the
182       array.
183
184       @param in the ObjectInput.
185       @return the array of integers.
186
187       @exception java.io.IOException The write caused an IOException.
188       */

189     public static int[] readIntArray(ObjectInput JavaDoc in) throws IOException JavaDoc {
190         int length = in.readInt();
191         if (length == 0)
192             return null;
193         int[] a = new int[length];
194         for (int i=0; i<length; i++)
195             a[i] = in.readInt();
196         return a;
197     }
198
199     public static void writeInts( ObjectOutput JavaDoc out, int[][] val )
200         throws IOException JavaDoc
201     {
202         if (val == null)
203         {
204             out.writeBoolean(false);
205         }
206         else
207         {
208             out.writeBoolean(true);
209
210             int count = val.length;
211             out.writeInt( count );
212
213             for (int i = 0; i < count; i++)
214             {
215                 ArrayUtil.writeIntArray( out, val[i] );
216             }
217         }
218     }
219
220     public static int[][] readInts( ObjectInput JavaDoc in )
221          throws IOException JavaDoc, ClassNotFoundException JavaDoc
222     {
223         int[][] retVal = null;
224
225         if ( in.readBoolean() )
226         {
227             int count = in.readInt();
228
229             retVal = new int[ count ][];
230
231             for (int i = 0; i < count; i++)
232             {
233                 retVal[ i ] = ArrayUtil.readIntArray( in );
234             }
235         }
236
237         return retVal;
238     }
239
240     ///////////////////////////////////////////////////////////////////
241
//
242
// Methods for Arrays of LONGs
243
//
244
///////////////////////////////////////////////////////////////////
245

246     /**
247       Write an array of longs to an ObjectOutput. This writes the array
248       in a format readLongArray understands.
249
250       @param out the ObjectOutput.
251       @param a the array.
252       @exception java.io.IOException The write caused an IOException.
253       */

254     public static void writeLongArray(ObjectOutput JavaDoc out, long[] a) throws IOException JavaDoc {
255         if (a == null)
256             out.writeInt(0);
257         else {
258             out.writeInt(a.length);
259             for (int i=0; i<a.length; i++)
260                 out.writeLong(a[i]);
261         }
262     }
263
264     /**
265       Read an array of integers from an ObjectInput. This allocates the
266       array.
267
268       @param in the ObjectInput.
269       @return the array of integers.
270
271       @exception java.io.IOException The write caused an IOException.
272       */

273     public static long[] readLongArray(ObjectInput JavaDoc in) throws IOException JavaDoc {
274         int length = in.readInt();
275         long[] a = new long[length];
276         for (int i=0; i<length; i++)
277             a[i] = in.readLong();
278         return a;
279     }
280
281     /**
282       Read an array of strings from an ObjectInput. This allocates the
283       array.
284
285       @param in the ObjectInput.
286       @return the array of integers.
287
288       @exception java.io.IOException The write caused an IOException.
289       */

290     public static String JavaDoc[] readStringArray(ObjectInput JavaDoc in)
291         throws IOException JavaDoc, ClassNotFoundException JavaDoc
292     {
293         Object JavaDoc[] objArray = readObjectArray(in);
294         int size = 0;
295
296         if (objArray == null)
297             return null;
298
299         String JavaDoc[] stringArray = new String JavaDoc[size = objArray.length];
300
301         for (int i = 0; i < size; i++)
302         {
303             stringArray[i] = (String JavaDoc)objArray[i];
304         }
305
306         return stringArray;
307     }
308     
309     ///////////////////////////////////////////////////////////////////
310
//
311
// Methods for Arrays of BOOLEANS
312
//
313
///////////////////////////////////////////////////////////////////
314

315     /**
316       Write an array of booleans to an ObjectOutput. This writes the array
317       in a format readBooleanArray understands.
318
319       @param out the ObjectOutput.
320       @param a the array.
321       @exception java.io.IOException The write caused an IOException.
322       */

323     public static void writeBooleanArray(ObjectOutput JavaDoc out, boolean[] a) throws IOException JavaDoc {
324         if (a == null)
325             out.writeInt(0);
326         else {
327             out.writeInt(a.length);
328             for (int i=0; i<a.length; i++)
329                 out.writeBoolean(a[i]);
330         }
331     }
332
333     /**
334       Read an array of integers from an ObjectInput. This allocates the
335       array.
336
337       @param in the ObjectInput.
338       @return the array of integers.
339
340       @exception java.io.IOException The write caused an IOException.
341       */

342     public static boolean[] readBooleanArray(ObjectInput JavaDoc in) throws IOException JavaDoc {
343         int length = in.readInt();
344         boolean[] a = new boolean[length];
345         for (int i=0; i<length; i++)
346             a[i] = in.readBoolean();
347         return a;
348     }
349 }
350
Popular Tags