KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
22
23 public class PrimitiveType
24 {
25     public static final int BOOLEAN = 1;
26     public static final int BYTE = 2;
27     public static final int CHAR = 3;
28     public static final int DOUBLE = 4;
29     public static final int FLOAT = 5;
30     public static final int INT = 6;
31     public static final int LONG = 7;
32     public static final int SHORT = 8;
33
34     public static final ObjectHelper BOOLEAN_ARRAY_HELPER = new ObjectHelper()
35     {
36         public Object read(ObjectInputStream input)
37         {
38             int n = input._cdrInput.read_long();
39             if (n == 0)
40             {
41                 return ArrayUtil.EMPTY_BOOLEAN_ARRAY;
42             }
43             boolean[] array = new boolean[n];
44             input._cdrInput.read_boolean_array(array, 0, n);
45             return array;
46         }
47
48         public void write(ObjectOutputStream output, Object value)
49         {
50             boolean[] array = (boolean[])value;
51             int n = array.length;
52             output._cdrOutput.write_long(n);
53             output._cdrOutput.write_boolean_array(array, 0, n);
54         }
55     }
56     ;
57
58     public static final ObjectHelper BYTE_ARRAY_HELPER = new ObjectHelper()
59     {
60         public Object read(ObjectInputStream input)
61         {
62             int n = input._cdrInput.read_long();
63             if (n == 0)
64             {
65                 return ArrayUtil.EMPTY_BYTE_ARRAY;
66             }
67             byte[] array = new byte[n];
68             input._cdrInput.read_octet_array(array, 0, n);
69             return array;
70         }
71
72         public void write(ObjectOutputStream output, Object value)
73         {
74             byte[] array = (byte[])value;
75             int n = array.length;
76             output._cdrOutput.write_long(n);
77             output._cdrOutput.write_octet_array(array, 0, n);
78         }
79     }
80     ;
81
82     public static final ObjectHelper CHAR_ARRAY_HELPER = new ObjectHelper()
83     {
84         public Object read(ObjectInputStream input)
85         {
86             int n = input._cdrInput.read_long();
87             if (n == 0)
88             {
89                 return ArrayUtil.EMPTY_CHAR_ARRAY;
90             }
91             char[] array = new char[n];
92             input._cdrInput.read_char_array(array, 0, n);
93             return array;
94         }
95
96         public void write(ObjectOutputStream output, Object value)
97         {
98             char[] array = (char[])value;
99             int n = array.length;
100             output._cdrOutput.write_long(n);
101             output._cdrOutput.write_char_array(array, 0, n);
102         }
103     }
104     ;
105
106     public static final ObjectHelper DOUBLE_ARRAY_HELPER = new ObjectHelper()
107     {
108         public Object read(ObjectInputStream input)
109         {
110             int n = input._cdrInput.read_long();
111             if (n == 0)
112             {
113                 return ArrayUtil.EMPTY_DOUBLE_ARRAY;
114             }
115             double[] array = new double[n];
116             input._cdrInput.read_double_array(array, 0, n);
117             return array;
118         }
119
120         public void write(ObjectOutputStream output, Object value)
121         {
122             double[] array = (double[])value;
123             int n = array.length;
124             output._cdrOutput.write_long(n);
125             output._cdrOutput.write_double_array(array, 0, n);
126         }
127     }
128     ;
129
130     public static final ObjectHelper FLOAT_ARRAY_HELPER = new ObjectHelper()
131     {
132         public Object read(ObjectInputStream input)
133         {
134             int n = input._cdrInput.read_long();
135             if (n == 0)
136             {
137                 return ArrayUtil.EMPTY_FLOAT_ARRAY;
138             }
139             float[] array = new float[n];
140             input._cdrInput.read_float_array(array, 0, n);
141             return array;
142         }
143
144         public void write(ObjectOutputStream output, Object value)
145         {
146             float[] array = (float[])value;
147             int n = array.length;
148             output._cdrOutput.write_long(n);
149             output._cdrOutput.write_float_array(array, 0, n);
150         }
151     }
152     ;
153
154     public static final ObjectHelper INT_ARRAY_HELPER = new ObjectHelper()
155     {
156         public Object read(ObjectInputStream input)
157         {
158             int n = input._cdrInput.read_long();
159             if (n == 0)
160             {
161                 return ArrayUtil.EMPTY_INT_ARRAY;
162             }
163             int[] array = new int[n];
164             input._cdrInput.read_long_array(array, 0, n);
165             return array;
166         }
167
168         public void write(ObjectOutputStream output, Object value)
169         {
170             int[] array = (int[])value;
171             int n = array.length;
172             output._cdrOutput.write_long(n);
173             output._cdrOutput.write_long_array(array, 0, n);
174         }
175     }
176     ;
177
178     public static final ObjectHelper LONG_ARRAY_HELPER = new ObjectHelper()
179     {
180         public Object read(ObjectInputStream input)
181         {
182             int n = input._cdrInput.read_long();
183             if (n == 0)
184             {
185                 return ArrayUtil.EMPTY_LONG_ARRAY;
186             }
187             long[] array = new long[n];
188             input._cdrInput.read_longlong_array(array, 0, n);
189             return array;
190         }
191
192         public void write(ObjectOutputStream output, Object value)
193         {
194             long[] array = (long[])value;
195             int n = array.length;
196             output._cdrOutput.write_long(n);
197             output._cdrOutput.write_longlong_array(array, 0, n);
198         }
199     }
200     ;
201
202     public static final ObjectHelper SHORT_ARRAY_HELPER = new ObjectHelper()
203     {
204         public Object read(ObjectInputStream input)
205         {
206             int n = input._cdrInput.read_long();
207             if (n == 0)
208             {
209                 return ArrayUtil.EMPTY_SHORT_ARRAY;
210             }
211             short[] array = new short[n];
212             input._cdrInput.read_short_array(array, 0, n);
213             return array;
214         }
215
216         public void write(ObjectOutputStream output, Object value)
217         {
218             short[] array = (short[])value;
219             int n = array.length;
220             output._cdrOutput.write_long(n);
221             output._cdrOutput.write_short_array(array, 0, n);
222         }
223     }
224     ;
225
226     public static int get(Class _class)
227     {
228         if (_class == boolean.class)
229         {
230             return BOOLEAN;
231         }
232         else if (_class == byte.class)
233         {
234             return BYTE;
235         }
236         else if (_class == char.class)
237         {
238             return CHAR;
239         }
240         else if (_class == double.class)
241         {
242             return DOUBLE;
243         }
244         else if (_class == float.class)
245         {
246             return FLOAT;
247         }
248         else if (_class == int.class)
249         {
250             return INT;
251         }
252         else if (_class == long.class)
253         {
254             return LONG;
255         }
256         else if (_class == short.class)
257         {
258             return SHORT;
259         }
260         else
261         {
262             throw new IllegalArgumentException("class = " +_class.getName());
263         }
264     }
265
266     public static ObjectHelper getArrayHelper(Class _class)
267     {
268         if (_class == boolean.class)
269         {
270             return BOOLEAN_ARRAY_HELPER;
271         }
272         else if (_class == byte.class)
273         {
274             return BYTE_ARRAY_HELPER;
275         }
276         else if (_class == char.class)
277         {
278             return CHAR_ARRAY_HELPER;
279         }
280         else if (_class == double.class)
281         {
282             return DOUBLE_ARRAY_HELPER;
283         }
284         else if (_class == float.class)
285         {
286             return FLOAT_ARRAY_HELPER;
287         }
288         else if (_class == int.class)
289         {
290             return INT_ARRAY_HELPER;
291         }
292         else if (_class == long.class)
293         {
294             return LONG_ARRAY_HELPER;
295         }
296         else if (_class == short.class)
297         {
298             return SHORT_ARRAY_HELPER;
299         }
300         else
301         {
302             throw new IllegalArgumentException("class = " +_class.getName());
303         }
304     }
305
306     public static org.omg.CORBA.TypeCode getTypeCode(int p)
307     {
308         switch (p)
309         {
310             case BOOLEAN:
311                 return TypeCode.BOOLEAN;
312             case BYTE: // java byte is IDL octet
313
return TypeCode.OCTET;
314             case CHAR:
315                 return TypeCode.CHAR;
316             case DOUBLE:
317                 return TypeCode.DOUBLE;
318             case FLOAT:
319                 return TypeCode.FLOAT;
320             case INT: // java int is IDL long
321
return TypeCode.LONG;
322             case LONG: // java long is IDL long long
323
return TypeCode.LONGLONG;
324             case SHORT:
325                 return TypeCode.SHORT;
326             default:
327                 throw new IllegalArgumentException("primitive type = " + p);
328         }
329     }
330 }
331
Popular Tags