1 /* TypeCodeHelper.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import org.omg.CORBA.TCKind;
42 import org.omg.CORBA.TypeCode;
43 import org.omg.CORBA.TypeCodePackage.BadKind;
44 import org.omg.CORBA.TypeCodePackage.Bounds;
47 * Reads and writes the TypeCodes usind common data representation.
49 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
51 public class TypeCodeHelper
54 * Read the CORBA {@link TypeCode}. First, the TypeCode kind
55 * is read as four byte long. Then, if needed, the additional
56 * parameters are loaded following CORBA specification.
58 * @param in a stream to read from.
60 public static TypeCode read(org.omg.CORBA.portable.InputStream in)
61 throws BadKind, Bounds
63 TCKind kind = TCKind.from_int(in.read_long());
67 recordTypeCode.Field f;
73 case TCKind._tk_sequence :
74 case TCKind._tk_array :
76 primitiveArrayTypeCode p = new primitiveArrayTypeCode(kind);
77 p.setLength(in.read_long());
81 case TCKind._tk_string :
82 case TCKind._tk_wstring :
83 s = new stringTypeCode(kind);
84 s.setLength(in.read_long());
88 case TCKind._tk_fixed :
90 fixedTypeCode fx = new fixedTypeCode();
91 fx.setDigits(in.read_short());
92 fx.setScale(in.read_short());
96 case TCKind._tk_objref :
97 case TCKind._tk_native :
98 case TCKind._tk_abstract_interface :
99 g = new generalTypeCode(kind);
100 g.setId(in.read_string());
101 g.setName(in.read_string());
105 case TCKind._tk_alias :
106 case TCKind._tk_value_box :
107 g = new generalTypeCode(kind);
108 g.setId(in.read_string());
109 g.setName(in.read_string());
110 g.setContentType(in.read_TypeCode());
114 case TCKind._tk_struct :
115 case TCKind._tk_except :
116 r = new recordTypeCode(kind);
117 r.setId(in.read_string());
118 r.setName(in.read_string());
122 for (int i = 0; i < n; i++)
125 f.name = in.read_string();
126 f.type = in.read_TypeCode();
131 case TCKind._tk_enum :
132 r = new recordTypeCode(kind);
133 r.setId(in.read_string());
134 r.setName(in.read_string());
138 for (int i = 0; i < n; i++)
141 f.name = in.read_string();
146 case TCKind._tk_union :
147 r = new recordTypeCode(kind);
148 r.setId(in.read_string());
149 r.setName(in.read_string());
150 r.setDiscriminator_type(in.read_TypeCode());
151 r.setDefaultIndex(in.read_long());
155 for (int i = 0; i < n; i++)
158 f.label = in.read_any();
159 f.name = in.read_string();
160 f.type = in.read_TypeCode();
166 case TCKind._tk_value :
167 r = new recordTypeCode(kind);
168 r.setId(in.read_string());
169 r.setName(in.read_string());
170 r.setTypeModifier(in.read_short());
171 r.setConcreteBase_type(in.read_TypeCode());
175 for (int i = 0; i < n; i++)
178 f.name = in.read_string();
179 f.type = in.read_TypeCode();
180 f.visibility = in.read_short();
186 rt = new primitiveTypeCode(kind);
192 * Write the CORBA {@link TypeCode}. First, the TypeCode kind
193 * is written as four byte long. Then, if needed, the additional
194 * parameters are stored following CORBA specification.
196 * @param out a stream to write into.
197 * @param x a {@link TypeCode} to write.
199 public static void write(org.omg.CORBA.portable.OutputStream out, TypeCode x)
200 throws BadKind, Bounds
202 out.write_long(x.kind().value());
204 switch (x.kind().value())
206 case TCKind._tk_string :
207 case TCKind._tk_wstring :
208 out.write_long(x.length());
211 case TCKind._tk_sequence :
212 case TCKind._tk_array :
213 write(out, x.content_type());
214 out.write_long(x.length());
217 case TCKind._tk_fixed :
218 out.write_short(x.fixed_digits());
219 out.write_short(x.fixed_scale());
222 case TCKind._tk_objref :
223 case TCKind._tk_native :
224 case TCKind._tk_abstract_interface :
225 out.write_string(x.id());
226 out.write_string(x.name());
229 case TCKind._tk_alias :
230 case TCKind._tk_value_box :
231 out.write_string(x.id());
232 out.write_string(x.name());
233 write(out, x.content_type());
236 case TCKind._tk_struct :
237 case TCKind._tk_except :
238 out.write_string(x.id());
239 out.write_string(x.name());
241 out.write_long(x.member_count());
243 for (int i = 0; i < x.member_count(); i++)
245 out.write_string(x.member_name(i));
246 write(out, x.member_type(i));
250 case TCKind._tk_enum :
251 out.write_string(x.id());
252 out.write_string(x.name());
254 out.write_long(x.member_count());
256 for (int i = 0; i < x.member_count(); i++)
258 out.write_string(x.member_name(i));
262 case TCKind._tk_union :
263 out.write_string(x.id());
264 out.write_string(x.name());
266 write(out, x.discriminator_type());
267 out.write_long(x.default_index());
269 out.write_long(x.member_count());
271 for (int i = 0; i < x.member_count(); i++)
273 out.write_any(x.member_label(i));
274 out.write_string(x.member_name(i));
275 write(out, x.member_type(i));
279 case TCKind._tk_value :
280 out.write_string(x.id());
281 out.write_string(x.name());
282 out.write_short(x.type_modifier());
283 write(out, x.concrete_base_type());
285 out.write_long(x.member_count());
287 for (int i = 0; i < x.member_count(); i++)
289 out.write_string(x.member_name(i));
290 write(out, x.member_type(i));
291 out.write_short(x.member_visibility(i));