KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > ArrayHelper


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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