KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > ArrayHelper


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 import org.apache.geronimo.interop.util.ArrayUtil;
21
22 import java.lang.reflect.*;
23
24 public class ArrayHelper implements ObjectHelper
25 {
26     private ValueType _element;
27
28     private ObjectHelper _primitive;
29
30     public ArrayHelper(Class JavaDoc elementClass)
31     {
32         if (elementClass.isPrimitive())
33         {
34             _primitive = PrimitiveType.getArrayHelper(elementClass);
35         }
36         else
37         {
38             _element = ValueType.getInstance(elementClass);
39         }
40     }
41
42     public Object JavaDoc read(ObjectInputStream input)
43     {
44         if (_primitive != null)
45         {
46             return _primitive.read(input);
47         }
48         CdrInputStream cdrInput = input._cdrInput;
49         int n = cdrInput.read_long();
50         Object JavaDoc[] array = (Object JavaDoc[])Array.newInstance(_element._class, n);
51         for (int i = 0; i < n; i++)
52         {
53             array[i] = input.readObject(_element);
54         }
55         return array;
56     }
57
58     public void write(ObjectOutputStream output, Object JavaDoc value)
59     {
60         if (_primitive != null)
61         {
62             _primitive.write(output, value);
63             return;
64         }
65         CdrOutputStream cdrOutput = output._cdrOutput;
66         Object JavaDoc[] array = (Object JavaDoc[])value;
67         if (array == null)
68         {
69             array = ArrayUtil.EMPTY_OBJECT_ARRAY;
70         }
71         int n = array.length;
72         cdrOutput.write_long(n);
73         for (int i = 0; i < n; i++)
74         {
75             output.writeObject(_element, array[i]);
76         }
77     }
78 }
79
Popular Tags