1 /*
   2  * Copyright (c) 2000, 2011, 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 package com.sun.corba.se.impl.encoding;
  26 
  27 import java.io.IOException;
  28 import java.io.Serializable;
  29 import java.math.BigDecimal;
  30 import java.nio.ByteBuffer;
  31 
  32 import org.omg.CORBA.TypeCode;
  33 import org.omg.CORBA.Principal;
  34 import org.omg.CORBA.Any;
  35 
  36 import com.sun.org.omg.SendingContext.CodeBase;
  37 
  38 import com.sun.corba.se.pept.protocol.MessageMediator;
  39 
  40 import com.sun.corba.se.spi.logging.CORBALogDomains;
  41 import com.sun.corba.se.spi.orb.ORB;
  42 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  43 import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
  44 
  45 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  46 import com.sun.corba.se.impl.encoding.CodeSetConversion;
  47 import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
  48 import com.sun.corba.se.impl.orbutil.ORBUtility;
  49 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  50 
  51 /**
  52  * This is delegates to the real implementation.
  53  *
  54  * NOTE:
  55  *
  56  * Before using the stream for valuetype unmarshaling, one must call
  57  * performORBVersionSpecificInit().
  58  */
  59 public abstract class CDRInputStream
  60     extends org.omg.CORBA_2_3.portable.InputStream
  61     implements com.sun.corba.se.impl.encoding.MarshalInputStream,
  62                org.omg.CORBA.DataInputStream, org.omg.CORBA.portable.ValueInputStream
  63 {
  64     protected CorbaMessageMediator messageMediator;
  65     private CDRInputStreamBase impl;
  66 
  67     // We can move this out somewhere later.  For now, it serves its purpose
  68     // to create a concrete CDR delegate based on the GIOP version.
  69     private static class InputStreamFactory {
  70 
  71         public static CDRInputStreamBase newInputStream(
  72                 ORB orb, GIOPVersion version, byte encodingVersion) {
  73             switch(version.intValue()) {
  74                 case GIOPVersion.VERSION_1_0:
  75                     return new CDRInputStream_1_0();
  76                 case GIOPVersion.VERSION_1_1:
  77                     return new CDRInputStream_1_1();
  78                 case GIOPVersion.VERSION_1_2:
  79                     if (encodingVersion != Message.CDR_ENC_VERSION) {
  80                         return
  81                           new IDLJavaSerializationInputStream(encodingVersion);
  82                     }
  83                     return new CDRInputStream_1_2();
  84                     // else fall through and report exception.
  85                 default:
  86                     ORBUtilSystemException wrapper = ORBUtilSystemException.get( orb,
  87                         CORBALogDomains.RPC_ENCODING ) ;
  88                     throw wrapper.unsupportedGiopVersion( version ) ;
  89             }
  90         }
  91     }
  92 
  93     // Required for the case when a ClientResponseImpl is
  94     // created with a SystemException due to a dead server/closed
  95     // connection with no warning.  Note that the stream will
  96     // not be initialized in this case.
  97     //
  98     // Probably also required by ServerRequestImpl.
  99     //
 100     // REVISIT.
 101     public CDRInputStream() {
 102     }
 103 
 104     public CDRInputStream(CDRInputStream is) {
 105         impl = is.impl.dup();
 106         impl.setParent(this);
 107     }
 108 
 109     public CDRInputStream(org.omg.CORBA.ORB orb,
 110                           ByteBuffer byteBuffer,
 111                           int size,
 112                           boolean littleEndian,
 113                           GIOPVersion version,
 114                           byte encodingVersion,
 115                           BufferManagerRead bufMgr)
 116     {
 117         impl = InputStreamFactory.newInputStream((ORB)orb, version,
 118                                                  encodingVersion);
 119 
 120         impl.init(orb, byteBuffer, size, littleEndian, bufMgr);
 121 
 122         impl.setParent(this);
 123     }
 124 
 125     // org.omg.CORBA.portable.InputStream
 126     public final boolean read_boolean() {
 127         return impl.read_boolean();
 128     }
 129 
 130     public final char read_char() {
 131         return impl.read_char();
 132     }
 133 
 134     public final char read_wchar() {
 135         return impl.read_wchar();
 136     }
 137 
 138     public final byte read_octet() {
 139         return impl.read_octet();
 140     }
 141 
 142     public final short read_short() {
 143         return impl.read_short();
 144     }
 145 
 146     public final short read_ushort() {
 147         return impl.read_ushort();
 148     }
 149 
 150     public final int read_long() {
 151         return impl.read_long();
 152     }
 153 
 154     public final int read_ulong() {
 155         return impl.read_ulong();
 156     }
 157 
 158     public final long read_longlong() {
 159         return impl.read_longlong();
 160     }
 161 
 162     public final long read_ulonglong() {
 163         return impl.read_ulonglong();
 164     }
 165 
 166     public final float read_float() {
 167         return impl.read_float();
 168     }
 169 
 170     public final double read_double() {
 171         return impl.read_double();
 172     }
 173 
 174     public final String read_string() {
 175         return impl.read_string();
 176     }
 177 
 178     public final String read_wstring() {
 179         return impl.read_wstring();
 180     }
 181 
 182     public final void read_boolean_array(boolean[] value, int offset, int length) {
 183         impl.read_boolean_array(value, offset, length);
 184     }
 185 
 186     public final void read_char_array(char[] value, int offset, int length) {
 187         impl.read_char_array(value, offset, length);
 188     }
 189 
 190     public final void read_wchar_array(char[] value, int offset, int length) {
 191         impl.read_wchar_array(value, offset, length);
 192     }
 193 
 194     public final void read_octet_array(byte[] value, int offset, int length) {
 195         impl.read_octet_array(value, offset, length);
 196     }
 197 
 198     public final void read_short_array(short[] value, int offset, int length) {
 199         impl.read_short_array(value, offset, length);
 200     }
 201 
 202     public final void read_ushort_array(short[] value, int offset, int length) {
 203         impl.read_ushort_array(value, offset, length);
 204     }
 205 
 206     public final void read_long_array(int[] value, int offset, int length) {
 207         impl.read_long_array(value, offset, length);
 208     }
 209 
 210     public final void read_ulong_array(int[] value, int offset, int length) {
 211         impl.read_ulong_array(value, offset, length);
 212     }
 213 
 214     public final void read_longlong_array(long[] value, int offset, int length) {
 215         impl.read_longlong_array(value, offset, length);
 216     }
 217 
 218     public final void read_ulonglong_array(long[] value, int offset, int length) {
 219         impl.read_ulonglong_array(value, offset, length);
 220     }
 221 
 222     public final void read_float_array(float[] value, int offset, int length) {
 223         impl.read_float_array(value, offset, length);
 224     }
 225 
 226     public final void read_double_array(double[] value, int offset, int length) {
 227         impl.read_double_array(value, offset, length);
 228     }
 229 
 230     public final org.omg.CORBA.Object read_Object() {
 231         return impl.read_Object();
 232     }
 233 
 234     public final TypeCode read_TypeCode() {
 235         return impl.read_TypeCode();
 236     }
 237     public final Any read_any() {
 238         return impl.read_any();
 239     }
 240 
 241     public final Principal read_Principal() {
 242         return impl.read_Principal();
 243     }
 244 
 245     public final int read() throws java.io.IOException {
 246         return impl.read();
 247     }
 248 
 249     public final java.math.BigDecimal read_fixed() {
 250         return impl.read_fixed();
 251     }
 252 
 253     public final org.omg.CORBA.Context read_Context() {
 254         return impl.read_Context();
 255     }
 256 
 257     public final org.omg.CORBA.Object read_Object(java.lang.Class clz) {
 258         return impl.read_Object(clz);
 259     }
 260 
 261     public final org.omg.CORBA.ORB orb() {
 262         return impl.orb();
 263     }
 264 
 265     // org.omg.CORBA_2_3.portable.InputStream
 266     public final java.io.Serializable read_value() {
 267         return impl.read_value();
 268     }
 269 
 270     public final java.io.Serializable read_value(java.lang.Class clz) {
 271         return impl.read_value(clz);
 272     }
 273 
 274     public final java.io.Serializable read_value(org.omg.CORBA.portable.BoxedValueHelper factory) {
 275         return impl.read_value(factory);
 276     }
 277 
 278     public final java.io.Serializable read_value(java.lang.String rep_id) {
 279         return impl.read_value(rep_id);
 280     }
 281 
 282     public final java.io.Serializable read_value(java.io.Serializable value) {
 283         return impl.read_value(value);
 284     }
 285 
 286     public final java.lang.Object read_abstract_interface() {
 287         return impl.read_abstract_interface();
 288     }
 289 
 290     public final java.lang.Object read_abstract_interface(java.lang.Class clz) {
 291         return impl.read_abstract_interface(clz);
 292     }
 293     // com.sun.corba.se.impl.encoding.MarshalInputStream
 294 
 295     public final void consumeEndian() {
 296         impl.consumeEndian();
 297     }
 298 
 299     public final int getPosition() {
 300         return impl.getPosition();
 301     }
 302 
 303     // org.omg.CORBA.DataInputStream
 304 
 305     public final java.lang.Object read_Abstract () {
 306         return impl.read_Abstract();
 307     }
 308 
 309     public final java.io.Serializable read_Value () {
 310         return impl.read_Value();
 311     }
 312 
 313     public final void read_any_array (org.omg.CORBA.AnySeqHolder seq, int offset, int length) {
 314         impl.read_any_array(seq, offset, length);
 315     }
 316 
 317     public final void read_boolean_array (org.omg.CORBA.BooleanSeqHolder seq, int offset, int length) {
 318         impl.read_boolean_array(seq, offset, length);
 319     }
 320 
 321     public final void read_char_array (org.omg.CORBA.CharSeqHolder seq, int offset, int length) {
 322         impl.read_char_array(seq, offset, length);
 323     }
 324 
 325     public final void read_wchar_array (org.omg.CORBA.WCharSeqHolder seq, int offset, int length) {
 326         impl.read_wchar_array(seq, offset, length);
 327     }
 328 
 329     public final void read_octet_array (org.omg.CORBA.OctetSeqHolder seq, int offset, int length) {
 330         impl.read_octet_array(seq, offset, length);
 331     }
 332 
 333     public final void read_short_array (org.omg.CORBA.ShortSeqHolder seq, int offset, int length) {
 334         impl.read_short_array(seq, offset, length);
 335     }
 336 
 337     public final void read_ushort_array (org.omg.CORBA.UShortSeqHolder seq, int offset, int length) {
 338         impl.read_ushort_array(seq, offset, length);
 339     }
 340 
 341     public final void read_long_array (org.omg.CORBA.LongSeqHolder seq, int offset, int length) {
 342         impl.read_long_array(seq, offset, length);
 343     }
 344 
 345     public final void read_ulong_array (org.omg.CORBA.ULongSeqHolder seq, int offset, int length) {
 346         impl.read_ulong_array(seq, offset, length);
 347     }
 348 
 349     public final void read_ulonglong_array (org.omg.CORBA.ULongLongSeqHolder seq, int offset, int length) {
 350         impl.read_ulonglong_array(seq, offset, length);
 351     }
 352 
 353     public final void read_longlong_array (org.omg.CORBA.LongLongSeqHolder seq, int offset, int length) {
 354         impl.read_longlong_array(seq, offset, length);
 355     }
 356 
 357     public final void read_float_array (org.omg.CORBA.FloatSeqHolder seq, int offset, int length) {
 358         impl.read_float_array(seq, offset, length);
 359     }
 360 
 361     public final void read_double_array (org.omg.CORBA.DoubleSeqHolder seq, int offset, int length) {
 362         impl.read_double_array(seq, offset, length);
 363     }
 364 
 365     // org.omg.CORBA.portable.ValueBase
 366     public final String[] _truncatable_ids() {
 367         return impl._truncatable_ids();
 368     }
 369 
 370     // java.io.InputStream
 371     public final int read(byte b[]) throws IOException {
 372         return impl.read(b);
 373     }
 374 
 375     public final int read(byte b[], int off, int len) throws IOException {
 376         return impl.read(b, off, len);
 377     }
 378 
 379     public final long skip(long n) throws IOException {
 380         return impl.skip(n);
 381     }
 382 
 383     public final int available() throws IOException {
 384         return impl.available();
 385     }
 386 
 387     public final void close() throws IOException {
 388         impl.close();
 389     }
 390 
 391     public final void mark(int readlimit) {
 392         impl.mark(readlimit);
 393     }
 394 
 395     public final void reset() {
 396         impl.reset();
 397     }
 398 
 399     public final boolean markSupported() {
 400         return impl.markSupported();
 401     }
 402 
 403     public abstract CDRInputStream dup();
 404 
 405     // Needed by TCUtility
 406     public final java.math.BigDecimal read_fixed(short digits, short scale) {
 407         return impl.read_fixed(digits, scale);
 408     }
 409 
 410     public final boolean isLittleEndian() {
 411         return impl.isLittleEndian();
 412     }
 413 
 414     protected final ByteBuffer getByteBuffer() {
 415         return impl.getByteBuffer();
 416     }
 417 
 418     protected final void setByteBuffer(ByteBuffer byteBuffer) {
 419         impl.setByteBuffer(byteBuffer);
 420     }
 421 
 422     protected final void setByteBufferWithInfo(ByteBufferWithInfo bbwi) {
 423         impl.setByteBufferWithInfo(bbwi);
 424     }
 425 
 426     /**
 427      * return true if our ByteBuffer is sharing/equal to bb
 428      */
 429     protected final boolean isSharing(ByteBuffer bb) {
 430         return (getByteBuffer() ==  bb);
 431     }
 432 
 433     public final int getBufferLength() {
 434         return impl.getBufferLength();
 435     }
 436 
 437     protected final void setBufferLength(int value) {
 438         impl.setBufferLength(value);
 439     }
 440 
 441     protected final int getIndex() {
 442         return impl.getIndex();
 443     }
 444 
 445     protected final void setIndex(int value) {
 446         impl.setIndex(value);
 447     }
 448 
 449     public final void orb(org.omg.CORBA.ORB orb) {
 450         impl.orb(orb);
 451     }
 452 
 453     public final GIOPVersion getGIOPVersion() {
 454         return impl.getGIOPVersion();
 455     }
 456 
 457     public final BufferManagerRead getBufferManager() {
 458         return impl.getBufferManager();
 459     }
 460 
 461     // This should be overridden by any stream (ex: IIOPInputStream)
 462     // which wants to read values.  Thus, TypeCodeInputStream doesn't
 463     // have to do this.
 464     public CodeBase getCodeBase() {
 465         return null;
 466     }
 467 
 468     // Use Latin-1 for GIOP 1.0 or when code set negotiation was not
 469     // performed.
 470     protected CodeSetConversion.BTCConverter createCharBTCConverter() {
 471         return CodeSetConversion.impl().getBTCConverter(OSFCodeSetRegistry.ISO_8859_1,
 472                                                         impl.isLittleEndian());
 473     }
 474 
 475     // Subclasses must decide what to do here.  It's inconvenient to
 476     // make the class and this method abstract because of dup().
 477     protected abstract CodeSetConversion.BTCConverter createWCharBTCConverter();
 478 
 479     // Prints the current buffer in a human readable form
 480     void printBuffer() {
 481         impl.printBuffer();
 482     }
 483 
 484     /**
 485      * Aligns the current position on the given octet boundary
 486      * if there are enough bytes available to do so.  Otherwise,
 487      * it just returns.  This is used for some (but not all)
 488      * GIOP 1.2 message headers.
 489      */
 490     public void alignOnBoundary(int octetBoundary) {
 491         impl.alignOnBoundary(octetBoundary);
 492     }
 493 
 494     // Needed by request and reply messages for GIOP versions >= 1.2 only.
 495     public void setHeaderPadding(boolean headerPadding) {
 496         impl.setHeaderPadding(headerPadding);
 497     }
 498 
 499     /**
 500      * This must be called after determining the proper ORB version,
 501      * and setting it on the stream's ORB instance.  It can be called
 502      * after reading the service contexts, since that is the only place
 503      * we can get the ORB version info.
 504      *
 505      * Trying to unmarshal things requiring repository IDs before calling
 506      * this will result in NullPtrExceptions.
 507      */
 508     public void performORBVersionSpecificInit() {
 509         // In the case of SystemExceptions, a stream is created
 510         // with its default constructor (and thus no impl is set).
 511         if (impl != null)
 512             impl.performORBVersionSpecificInit();
 513     }
 514 
 515     /**
 516      * Resets any internal references to code set converters.
 517      * This is useful for forcing the CDR stream to reacquire
 518      * converters (probably from its subclasses) when state
 519      * has changed.
 520      */
 521     public void resetCodeSetConverters() {
 522         impl.resetCodeSetConverters();
 523     }
 524 
 525     public void setMessageMediator(MessageMediator messageMediator)
 526     {
 527         this.messageMediator = (CorbaMessageMediator) messageMediator;
 528     }
 529 
 530     public MessageMediator getMessageMediator()
 531     {
 532         return messageMediator;
 533     }
 534 
 535     // ValueInputStream -----------------------------
 536 
 537     public void start_value() {
 538         impl.start_value();
 539     }
 540 
 541     public void end_value() {
 542         impl.end_value();
 543     }
 544 }