1 /*
   2  * Copyright (c) 2001, 2013, 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 package com.sun.corba.se.impl.encoding;
  27 
  28 import java.nio.ByteBuffer;
  29 import org.omg.CORBA.CompletionStatus;
  30 import com.sun.org.omg.SendingContext.CodeBase;
  31 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  32 import com.sun.corba.se.impl.encoding.CDRInputStream;
  33 import com.sun.corba.se.impl.encoding.BufferManagerFactory;
  34 import com.sun.corba.se.impl.encoding.CodeSetConversion;
  35 import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
  36 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  37 
  38 import com.sun.corba.se.spi.orb.ORB;
  39 import com.sun.corba.se.spi.logging.CORBALogDomains;
  40 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  41 
  42 import sun.corba.EncapsInputStreamFactory;
  43 /**
  44  * Encapsulations are supposed to explicitly define their
  45  * code sets and GIOP version.  The original resolution to issue 2784
  46  * said that the defaults were UTF-8 and UTF-16, but that was not
  47  * agreed upon.
  48  *
  49  * These streams currently use CDR 1.2 with ISO8859-1 for char/string and
  50  * UTF16 for wchar/wstring.  If no byte order marker is available,
  51  * the endianness of the encapsulation is used.
  52  *
  53  * When more encapsulations arise that have their own special code
  54  * sets defined, we can make all constructors take such parameters.
  55  */
  56 public class EncapsInputStream extends CDRInputStream
  57 {
  58     private ORBUtilSystemException wrapper ;
  59 
  60     // corba/EncapsOutputStream
  61     // corba/ORBSingleton
  62     // iiop/ORB
  63     public EncapsInputStream(org.omg.CORBA.ORB orb, byte[] buf,
  64                              int size, boolean littleEndian,
  65                              GIOPVersion version) {
  66         super(orb, ByteBuffer.wrap(buf), size, littleEndian,
  67               version, Message.CDR_ENC_VERSION,
  68               BufferManagerFactory.newBufferManagerRead(
  69                                       BufferManagerFactory.GROW,
  70                                       Message.CDR_ENC_VERSION,
  71                                       (ORB)orb));
  72 
  73         wrapper = ORBUtilSystemException.get( (ORB)orb,
  74             CORBALogDomains.RPC_ENCODING ) ;
  75 
  76         performORBVersionSpecificInit();
  77     }
  78 
  79     public EncapsInputStream(org.omg.CORBA.ORB orb, ByteBuffer byteBuffer,
  80                              int size, boolean littleEndian,
  81                              GIOPVersion version) {
  82         super(orb, byteBuffer, size, littleEndian,
  83               version, Message.CDR_ENC_VERSION,
  84               BufferManagerFactory.newBufferManagerRead(
  85                                       BufferManagerFactory.GROW,
  86                                       Message.CDR_ENC_VERSION,
  87                                       (com.sun.corba.se.spi.orb.ORB)orb));
  88 
  89         performORBVersionSpecificInit();
  90     }
  91 
  92     // ior/IdentifiableBase
  93     // ior/IIOPProfile
  94     // corba/ORBSingleton
  95     // iiop/ORB
  96     public EncapsInputStream(org.omg.CORBA.ORB orb, byte[] data, int size)
  97     {
  98         this(orb, data, size, GIOPVersion.V1_2);
  99     }
 100 
 101     // corba/AnyImpl
 102     public EncapsInputStream(EncapsInputStream eis)
 103     {
 104         super(eis);
 105 
 106         wrapper = ORBUtilSystemException.get( (ORB)(eis.orb()),
 107             CORBALogDomains.RPC_ENCODING ) ;
 108 
 109         performORBVersionSpecificInit();
 110     }
 111 
 112     // CDREncapsCodec
 113     // ServiceContext
 114     //
 115     // Assumes big endian (can use consumeEndian to read and set
 116     // the endianness if it is an encapsulation with a byte order
 117     // mark at the beginning)
 118     public EncapsInputStream(org.omg.CORBA.ORB orb, byte[] data, int size, GIOPVersion version)
 119     {
 120         this(orb, data, size, false, version);
 121     }
 122 
 123     /**
 124      * Full constructor with a CodeBase parameter useful for
 125      * unmarshaling RMI-IIOP valuetypes (technically against the
 126      * intention of an encapsulation, but necessary due to OMG
 127      * issue 4795.  Used by ServiceContexts.
 128      */
 129     public EncapsInputStream(org.omg.CORBA.ORB orb,
 130                              byte[] data,
 131                              int size,
 132                              GIOPVersion version,
 133                              CodeBase codeBase) {
 134         super(orb,
 135               ByteBuffer.wrap(data),
 136               size,
 137               false,
 138               version, Message.CDR_ENC_VERSION,
 139               BufferManagerFactory.newBufferManagerRead(
 140                                       BufferManagerFactory.GROW,
 141                                       Message.CDR_ENC_VERSION,
 142                                       (ORB)orb));
 143 
 144         this.codeBase = codeBase;
 145 
 146         performORBVersionSpecificInit();
 147     }
 148 
 149     public CDRInputStream dup() {
 150         return EncapsInputStreamFactory.newEncapsInputStream(this);
 151     }
 152 
 153     protected CodeSetConversion.BTCConverter createCharBTCConverter() {
 154         return CodeSetConversion.impl().getBTCConverter(OSFCodeSetRegistry.ISO_8859_1);
 155     }
 156 
 157     protected CodeSetConversion.BTCConverter createWCharBTCConverter() {
 158         // Wide characters don't exist in GIOP 1.0
 159         if (getGIOPVersion().equals(GIOPVersion.V1_0))
 160             throw wrapper.wcharDataInGiop10( CompletionStatus.COMPLETED_MAYBE);
 161 
 162         // In GIOP 1.1, we shouldn't have byte order markers.  Take the order
 163         // of the stream if we don't see them.
 164         if (getGIOPVersion().equals(GIOPVersion.V1_1))
 165             return CodeSetConversion.impl().getBTCConverter(OSFCodeSetRegistry.UTF_16,
 166                                                             isLittleEndian());
 167 
 168         // Assume anything else adheres to GIOP 1.2 requirements.
 169         //
 170         // Our UTF_16 converter will work with byte order markers, and if
 171         // they aren't present, it will use the provided endianness.
 172         //
 173         // With no byte order marker, it's big endian in GIOP 1.2.
 174         // formal 00-11-03 15.3.16.
 175         return CodeSetConversion.impl().getBTCConverter(OSFCodeSetRegistry.UTF_16,
 176                                                         false);
 177     }
 178 
 179     public CodeBase getCodeBase() {
 180         return codeBase;
 181     }
 182 
 183     private CodeBase codeBase;
 184 }