1 /*
   2  * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * Licensed Materials - Property of IBM
  27  * RMI-IIOP v1.0
  28  * Copyright IBM Corp. 1998 1999  All Rights Reserved
  29  *
  30  */
  31 
  32 package com.sun.corba.se.impl.corba;
  33 
  34 import org.omg.CORBA.SystemException;
  35 import org.omg.CORBA.CompletionStatus;
  36 import org.omg.CORBA.UserException;
  37 import org.omg.CORBA.TCKind;
  38 import org.omg.CORBA.UNKNOWN;
  39 import org.omg.CORBA.INTERNAL;
  40 import org.omg.CORBA.NO_IMPLEMENT;
  41 import org.omg.CORBA.Any;
  42 import org.omg.CORBA.TypeCode;
  43 import org.omg.CORBA.Principal;
  44 import org.omg.CORBA_2_3.portable.InputStream;
  45 import org.omg.CORBA_2_3.portable.OutputStream;
  46 import org.omg.CORBA.portable.Streamable;
  47 import org.omg.CORBA.TypeCodePackage.BadKind;
  48 import java.util.Hashtable;
  49 import java.util.Enumeration;
  50 import java.util.NoSuchElementException;
  51 
  52 import com.sun.corba.se.impl.encoding.CDRInputStream;
  53 import com.sun.corba.se.impl.encoding.CDROutputStream;
  54 import java.io.Serializable;
  55 import java.math.BigDecimal;
  56 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  57 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  58 
  59 /**
  60  *  Static functions for TypeCode interpretation.
  61  */
  62 public final class TCUtility {
  63 
  64     static void marshalIn(org.omg.CORBA.portable.OutputStream s, TypeCode typeCode, long l, Object o) {
  65         switch (typeCode.kind().value()) {
  66         case TCKind._tk_null:
  67         case TCKind._tk_void:
  68         case TCKind._tk_native:
  69             // nothing to write
  70             break;
  71 
  72         case TCKind._tk_short:
  73             s.write_short((short)(l & 0xFFFFL));
  74             break;
  75 
  76         case TCKind._tk_ushort:
  77             s.write_ushort((short)(l & 0xFFFFL));
  78             break;
  79 
  80         case TCKind._tk_enum:
  81         case TCKind._tk_long:
  82             s.write_long((int)(l & 0xFFFFFFFFL));
  83             break;
  84 
  85         case TCKind._tk_ulong:
  86             s.write_ulong((int)(l & 0xFFFFFFFFL));
  87             break;
  88 
  89         case TCKind._tk_float:
  90             s.write_float(Float.intBitsToFloat((int)(l & 0xFFFFFFFFL)));
  91             break;
  92 
  93         case TCKind._tk_double:
  94             s.write_double(Double.longBitsToDouble(l));
  95             break;
  96 
  97         case TCKind._tk_boolean:
  98             if ( l == 0 )
  99                 s.write_boolean(false);
 100             else
 101                 s.write_boolean(true);
 102             break;
 103 
 104         case TCKind._tk_char:
 105             s.write_char((char)(l & 0xFFFFL));
 106             break;
 107 
 108         case TCKind._tk_octet:
 109             s.write_octet((byte)(l & 0xFFL));
 110             break;
 111 
 112         case TCKind._tk_any:
 113             s.write_any((Any)o);
 114             break;
 115 
 116         case TCKind._tk_TypeCode:
 117             s.write_TypeCode((TypeCode)o);
 118             break;
 119 
 120         case TCKind._tk_Principal:
 121             s.write_Principal((Principal)o);
 122             break;
 123 
 124         case TCKind._tk_objref:
 125             s.write_Object((org.omg.CORBA.Object)o);
 126             break;
 127 
 128         case TCKind._tk_longlong:
 129             s.write_longlong(l);
 130             break;
 131 
 132         case TCKind._tk_ulonglong:
 133             s.write_ulonglong(l);
 134             break;
 135 
 136         case TCKind._tk_wchar:
 137             s.write_wchar((char)(l & 0xFFFFL));
 138             break;
 139 
 140         case TCKind._tk_string:
 141             s.write_string((String)o);
 142             break;
 143 
 144         case TCKind._tk_wstring:
 145             s.write_wstring((String)o);
 146             break;
 147 
 148         case TCKind._tk_value:
 149         case TCKind._tk_value_box:
 150             ((org.omg.CORBA_2_3.portable.OutputStream)s).write_value((Serializable)o);
 151             break;
 152 
 153         case TCKind._tk_fixed:
 154             // _REVISIT_ As soon as the java-rtf adds digits and scale parameters to
 155             // OutputStream, this check will be unnecessary
 156             if (s instanceof CDROutputStream) {
 157                 try {
 158                     ((CDROutputStream)s).write_fixed((BigDecimal)o,
 159                                                     typeCode.fixed_digits(),
 160                                                     typeCode.fixed_scale());
 161                 } catch (BadKind badKind) { // impossible
 162                 }
 163             } else {
 164                 s.write_fixed((BigDecimal)o);
 165             }
 166             break;
 167 
 168         case TCKind._tk_struct:
 169         case TCKind._tk_union:
 170         case TCKind._tk_sequence:
 171         case TCKind._tk_array:
 172         case TCKind._tk_alias:
 173         case TCKind._tk_except:
 174             ((Streamable)o)._write(s);
 175             break;
 176 
 177         case TCKind._tk_abstract_interface:
 178             ((org.omg.CORBA_2_3.portable.OutputStream)s).write_abstract_interface(o);
 179             break;
 180 
 181         case TCKind._tk_longdouble:
 182             // Unspecified for Java
 183         default:
 184             ORBUtilSystemException wrapper = ORBUtilSystemException.get(
 185                 (com.sun.corba.se.spi.orb.ORB)s.orb(),
 186                 CORBALogDomains.RPC_PRESENTATION ) ;
 187             throw wrapper.typecodeNotSupported() ;
 188         }
 189     }
 190 
 191     static void unmarshalIn(org.omg.CORBA.portable.InputStream s, TypeCode typeCode, long[] la, Object[] oa)
 192     {
 193         int type = typeCode.kind().value();
 194         long l=0;
 195         Object o=oa[0];
 196 
 197         switch (type) {
 198         case TCKind._tk_null:
 199         case TCKind._tk_void:
 200         case TCKind._tk_native:
 201             // Nothing to read
 202             break;
 203 
 204         case TCKind._tk_short:
 205             l = s.read_short() & 0xFFFFL;
 206             break;
 207 
 208         case TCKind._tk_ushort:
 209             l = s.read_ushort() & 0xFFFFL;
 210             break;
 211 
 212         case TCKind._tk_enum:
 213         case TCKind._tk_long:
 214             l = s.read_long() & 0xFFFFFFFFL;
 215             break;
 216 
 217         case TCKind._tk_ulong:
 218             l = s.read_ulong() & 0xFFFFFFFFL;
 219             break;
 220 
 221         case TCKind._tk_float:
 222             l = Float.floatToIntBits(s.read_float()) & 0xFFFFFFFFL;
 223             break;
 224 
 225         case TCKind._tk_double:
 226             l = Double.doubleToLongBits(s.read_double());
 227             break;
 228 
 229         case TCKind._tk_char:
 230             l = s.read_char() & 0xFFFFL;
 231             break;
 232 
 233         case TCKind._tk_octet:
 234             l = s.read_octet() & 0xFFL;
 235             break;
 236 
 237         case TCKind._tk_boolean:
 238             if ( s.read_boolean() )
 239                 l = 1;
 240             else
 241                 l = 0;
 242             break;
 243 
 244         case TCKind._tk_any:
 245             o = s.read_any();
 246             break;
 247 
 248         case TCKind._tk_TypeCode:
 249             o = s.read_TypeCode();
 250             break;
 251 
 252         case TCKind._tk_Principal:
 253             o = s.read_Principal();
 254             break;
 255 
 256         case TCKind._tk_objref:
 257             if (o instanceof Streamable)
 258                 ((Streamable)o)._read(s);
 259             else
 260                 o = s.read_Object();
 261             break;
 262 
 263         case TCKind._tk_longlong:
 264             l = s.read_longlong();
 265             break;
 266 
 267         case TCKind._tk_ulonglong:
 268             l = s.read_ulonglong();
 269             break;
 270 
 271         case TCKind._tk_wchar:
 272             l = s.read_wchar() & 0xFFFFL;
 273             break;
 274 
 275         case TCKind._tk_string:
 276             o = s.read_string();
 277             break;
 278 
 279         case TCKind._tk_wstring:
 280             o = s.read_wstring();
 281             break;
 282 
 283         case TCKind._tk_value:
 284         case TCKind._tk_value_box:
 285             o = ((org.omg.CORBA_2_3.portable.InputStream)s).read_value ();
 286             break;
 287 
 288         case TCKind._tk_fixed:
 289             try {
 290                 // _REVISIT_ As soon as the java-rtf adds digits and scale parameters to
 291                 // InputStream, this check will be unnecessary
 292                 if (s instanceof CDRInputStream) {
 293                     o = ((CDRInputStream)s).read_fixed(typeCode.fixed_digits(),
 294                                                                 typeCode.fixed_scale());
 295                 } else {
 296                     BigDecimal bigDecimal = s.read_fixed();
 297                     o = bigDecimal.movePointLeft((int)typeCode.fixed_scale());
 298                 }
 299             } catch (BadKind badKind) { // impossible
 300             }
 301             break;
 302 
 303         case TCKind._tk_struct:
 304         case TCKind._tk_union:
 305         case TCKind._tk_sequence:
 306         case TCKind._tk_array:
 307         case TCKind._tk_alias:
 308         case TCKind._tk_except:
 309             ((Streamable)o)._read(s);
 310             break;
 311 
 312         case TCKind._tk_abstract_interface:
 313             o = ((org.omg.CORBA_2_3.portable.InputStream)s).read_abstract_interface();
 314             break;
 315 
 316         case TCKind._tk_longdouble:
 317             // Unspecified for Java
 318         default:
 319             ORBUtilSystemException wrapper = ORBUtilSystemException.get(
 320                 (com.sun.corba.se.spi.orb.ORB)s.orb(),
 321                 CORBALogDomains.RPC_PRESENTATION ) ;
 322             throw wrapper.typecodeNotSupported() ;
 323         }
 324 
 325         oa[0] = o;
 326         la[0] = l;
 327     }
 328 
 329 }