1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.ArrayUtil |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import org.apache.derby.iapi.services.sanity.SanityManager; |
24 | import java.io.ObjectOutput; |
25 | import java.io.ObjectInput; |
26 | import java.io.IOException; |
27 | import java.lang.reflect.Array; |
28 | |
29 | /** |
30 | Utility class for constructing and reading and writing arrays from/to |
31 | formatId streams. |
32 | |
33 | @version 0.1 |
34 | @author Rick Hillegas |
35 | */ |
36 | public abstract class ArrayUtil |
37 | { |
38 | /////////////////////////////////////////////////////////////////// |
39 | // |
40 | // Methods for Arrays of OBJECTS. Cannot be used for an |
41 | // array of primitives, see below for something for primitives |
42 | // |
43 | /////////////////////////////////////////////////////////////////// |
44 | /** |
45 | Write the length of an array of objects to an output stream. |
46 | |
47 | The length |
48 | |
49 | @param out ObjectOutput stream |
50 | @param a array of objects whose length should be written. |
51 | |
52 | @exception java.io.IOException The write caused an IOException. |
53 | */ |
54 | public static void writeArrayLength(ObjectOutput out, Object[] a) |
55 | throws IOException |
56 | { |
57 | out.writeInt(a.length); |
58 | } |
59 | |
60 | /** |
61 | Write an array of objects to an output stream. |
62 | |
63 | @param out Object output stream to write to. |
64 | @param a array of objects to write. |
65 | |
66 | @exception java.io.IOException The write caused an IOException. |
67 | */ |
68 | public static void writeArrayItems(ObjectOutput out, Object[] a) |
69 | throws IOException |
70 | { |
71 | if (a == null) |
72 | return; |
73 | |
74 | for(int ix = 0; ix < a.length; ix++) |
75 | { out.writeObject(a[ix]); } |
76 | } |
77 | |
78 | /** |
79 | Write an array of objects and length to an output stream. |
80 | Does equivalent of writeArrayLength() followed by writeArrayItems() |
81 | |
82 | @param out Object output stream to write to. |
83 | @param a array of objects to write. |
84 | |
85 | @exception java.io.IOException The write caused an IOException. |
86 | */ |
87 | public static void writeArray(ObjectOutput out, Object[] a) |
88 | throws IOException |
89 | { |
90 | if (a == null) |
91 | { |
92 | out.writeInt(0); |
93 | return; |
94 | } |
95 | |
96 | out.writeInt(a.length); |
97 | for(int ix = 0; ix < a.length; ix++) |
98 | { out.writeObject(a[ix]); } |
99 | } |
100 | |
101 | /** |
102 | Read an array of objects out of a stream. |
103 | |
104 | @param in Input stream |
105 | @param a array to read into |
106 | |
107 | @exception java.io.IOException The write caused an IOException. |
108 | @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist |
109 | */ |
110 | public static void readArrayItems(ObjectInput in, Object[] a) |
111 | throws IOException, ClassNotFoundException |
112 | { |
113 | for (int ix=0; ix<a.length; ix++) |
114 | { |
115 | a[ix]=in.readObject(); |
116 | } |
117 | } |
118 | |
119 | /** |
120 | Read the length of an array of objects in an object stream. |
121 | |
122 | @param in Input stream. |
123 | |
124 | @return length of the array of objects |
125 | |
126 | @exception java.io.IOException The write caused an IOException. |
127 | */ |
128 | public static int readArrayLength(ObjectInput in) |
129 | throws IOException |
130 | { |
131 | return in.readInt(); |
132 | } |
133 | |
134 | /** |
135 | Reads an array of objects from the stream. |
136 | |
137 | @param in Input stream |
138 | |
139 | @exception java.io.IOException The write caused an IOException. |
140 | @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist |
141 | */ |
142 | public static Object[] readObjectArray(ObjectInput in) |
143 | throws IOException, ClassNotFoundException |
144 | { |
145 | int size = in.readInt(); |
146 | if ( size == 0 ) { return null; } |
147 | |
148 | Object[] result = new Object[ size ]; |
149 | |
150 | readArrayItems( in, result ); |
151 | |
152 | return result; |
153 | } |
154 | |
155 | /////////////////////////////////////////////////////////////////// |
156 | // |
157 | // Methods for Arrays of INTs |
158 | // |
159 | /////////////////////////////////////////////////////////////////// |
160 | |
161 | /** |
162 | Write an array of integers to an ObjectOutput. This writes the array |
163 | in a format readIntArray understands. |
164 | |
165 | @param out the ObjectOutput. |
166 | @param a the array. |
167 | @exception java.io.IOException The write caused an IOException. |
168 | */ |
169 | public static void writeIntArray(ObjectOutput out, int[] a) throws IOException { |
170 | if (a == null) |
171 | out.writeInt(0); |
172 | else { |
173 | out.writeInt(a.length); |
174 | for (int i=0; i<a.length; i++) |
175 | out.writeInt(a[i]); |
176 | } |
177 | } |
178 | |
179 | /** |
180 | Read an array of integers from an ObjectInput. This allocates the |
181 | array. |
182 | |
183 | @param in the ObjectInput. |
184 | @return the array of integers. |
185 | |
186 | @exception java.io.IOException The write caused an IOException. |
187 | */ |
188 | public static int[] readIntArray(ObjectInput in) throws IOException { |
189 | int length = in.readInt(); |
190 | if (length == 0) |
191 | return null; |
192 | int[] a = new int[length]; |
193 | for (int i=0; i<length; i++) |
194 | a[i] = in.readInt(); |
195 | return a; |
196 | } |
197 | |
198 | public static void writeInts( ObjectOutput out, int[][] val ) |
199 | throws IOException |
200 | { |
201 | if (val == null) |
202 | { |
203 | out.writeBoolean(false); |
204 | } |
205 | else |
206 | { |
207 | out.writeBoolean(true); |
208 | |
209 | int count = val.length; |
210 | out.writeInt( count ); |
211 | |
212 | for (int i = 0; i < count; i++) |
213 | { |
214 | ArrayUtil.writeIntArray( out, val[i] ); |
215 | } |
216 | } |
217 | } |
218 | |
219 | public static int[][] readInts( ObjectInput in ) |
220 | throws IOException, ClassNotFoundException |
221 | { |
222 | int[][] retVal = null; |
223 | |
224 | if ( in.readBoolean() ) |
225 | { |
226 | int count = in.readInt(); |
227 | |
228 | retVal = new int[ count ][]; |
229 | |
230 | for (int i = 0; i < count; i++) |
231 | { |
232 | retVal[ i ] = ArrayUtil.readIntArray( in ); |
233 | } |
234 | } |
235 | |
236 | return retVal; |
237 | } |
238 | |
239 | /////////////////////////////////////////////////////////////////// |
240 | // |
241 | // Methods for Arrays of LONGs |
242 | // |
243 | /////////////////////////////////////////////////////////////////// |
244 | |
245 | /** |
246 | Write an array of longs to an ObjectOutput. This writes the array |
247 | in a format readLongArray understands. |
248 | |
249 | @param out the ObjectOutput. |
250 | @param a the array. |
251 | @exception java.io.IOException The write caused an IOException. |
252 | */ |
253 | public static void writeLongArray(ObjectOutput out, long[] a) throws IOException { |
254 | if (a == null) |
255 | out.writeInt(0); |
256 | else { |
257 | out.writeInt(a.length); |
258 | for (int i=0; i<a.length; i++) |
259 | out.writeLong(a[i]); |
260 | } |
261 | } |
262 | |
263 | /** |
264 | Read an array of integers from an ObjectInput. This allocates the |
265 | array. |
266 | |
267 | @param in the ObjectInput. |
268 | @return the array of integers. |
269 | |
270 | @exception java.io.IOException The write caused an IOException. |
271 | */ |
272 | public static long[] readLongArray(ObjectInput in) throws IOException { |
273 | int length = in.readInt(); |
274 | long[] a = new long[length]; |
275 | for (int i=0; i<length; i++) |
276 | a[i] = in.readLong(); |
277 | return a; |
278 | } |
279 | |
280 | /** |
281 | Read an array of strings from an ObjectInput. This allocates the |
282 | array. |
283 | |
284 | @param in the ObjectInput. |
285 | @return the array of integers. |
286 | |
287 | @exception java.io.IOException The write caused an IOException. |
288 | */ |
289 | public static String[] readStringArray(ObjectInput in) |
290 | throws IOException, ClassNotFoundException |
291 | { |
292 | Object[] objArray = readObjectArray(in); |
293 | int size = 0; |
294 | |
295 | if (objArray == null) |
296 | return null; |
297 | |
298 | String[] stringArray = new String[size = objArray.length]; |
299 | |
300 | for (int i = 0; i < size; i++) |
301 | { |
302 | stringArray[i] = (String)objArray[i]; |
303 | } |
304 | |
305 | return stringArray; |
306 | } |
307 | |
308 | /////////////////////////////////////////////////////////////////// |
309 | // |
310 | // Methods for Arrays of BOOLEANS |
311 | // |
312 | /////////////////////////////////////////////////////////////////// |
313 | |
314 | /** |
315 | Write an array of booleans to an ObjectOutput. This writes the array |
316 | in a format readBooleanArray understands. |
317 | |
318 | @param out the ObjectOutput. |
319 | @param a the array. |
320 | @exception java.io.IOException The write caused an IOException. |
321 | */ |
322 | public static void writeBooleanArray(ObjectOutput out, boolean[] a) throws IOException { |
323 | if (a == null) |
324 | out.writeInt(0); |
325 | else { |
326 | out.writeInt(a.length); |
327 | for (int i=0; i<a.length; i++) |
328 | out.writeBoolean(a[i]); |
329 | } |
330 | } |
331 | |
332 | /** |
333 | Read an array of integers from an ObjectInput. This allocates the |
334 | array. |
335 | |
336 | @param in the ObjectInput. |
337 | @return the array of integers. |
338 | |
339 | @exception java.io.IOException The write caused an IOException. |
340 | */ |
341 | public static boolean[] readBooleanArray(ObjectInput in) throws IOException { |
342 | int length = in.readInt(); |
343 | boolean[] a = new boolean[length]; |
344 | for (int i=0; i<length; i++) |
345 | a[i] = in.readBoolean(); |
346 | return a; |
347 | } |
348 | } |