corba/src/share/classes/com/sun/corba/se/impl/encoding/CDRInputStream_1_2.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
package com.sun.corba.se.impl.encoding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import com.sun.corba.se.impl.orbutil.ORBConstants;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
public class CDRInputStream_1_2 extends CDRInputStream_1_1
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
    // Indicates whether the header is padded. In GIOP 1.2 and above,
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
    // the body must be aligned on an 8-octet boundary, and so the header is
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
    // padded appropriately. However, if there is no body to a request or reply
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
    // message, there is no header padding, in the unfragmented case.
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
    protected boolean headerPadding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
    // used to remember headerPadding flag when mark() and restore() are used.
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
    protected boolean restoreHeaderPadding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
    // Called by RequestMessage_1_2 or ReplyMessage_1_2 classes only.
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
    void setHeaderPadding(boolean headerPadding) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
        this.headerPadding = headerPadding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    // the mark and reset methods have been overridden to remember the
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
    // headerPadding flag.
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
    public void mark(int readlimit) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
        super.mark(readlimit);
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
        restoreHeaderPadding = headerPadding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    public void reset() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
        super.reset();
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
        headerPadding = restoreHeaderPadding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
        restoreHeaderPadding = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
    // Template method
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
    // This method has been overriden to ensure that the duplicated stream
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    // inherits the headerPadding flag, in case of GIOP 1.2 and above, streams.
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    public CDRInputStreamBase dup() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
        CDRInputStreamBase result = super.dup();
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        ((CDRInputStream_1_2)result).headerPadding = this.headerPadding;
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    protected void alignAndCheck(int align, int n) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
        // headerPadding bit is set by read method of the RequestMessage_1_2
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
        // or ReplyMessage_1_2 classes. When set, the very first body read
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        // operation (from the stub code) would trigger an alignAndCheck
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        // method call, that would in turn skip the header padding that was
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        // inserted during the earlier write operation by the sender. The
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
        // padding ensures that the body is aligned on an 8-octet boundary,
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
        // for GIOP versions 1.2 and beyond.
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
        if (headerPadding == true) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
            headerPadding = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
            alignOnBoundary(ORBConstants.GIOP_12_MSG_BODY_ALIGNMENT);
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
        checkBlockLength(align, n);
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
        // WARNING: Must compute real alignment after calling
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
        // checkBlockLength since it may move the position
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
        // In GIOP 1.2, a fragment may end with some alignment
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
        // padding (which leads to all fragments ending perfectly
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
        // on evenly divisible 8 byte boundaries).  A new fragment
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        // never requires alignment with the header since it ends
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        // on an 8 byte boundary.
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        int alignIncr = computeAlignment(bbwi.position(),align);
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
        bbwi.position(bbwi.position() + alignIncr);
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        if (bbwi.position() + n > bbwi.buflen) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
            grow(1, n);
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    public GIOPVersion getGIOPVersion() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        return GIOPVersion.V1_2;
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
    public char read_wchar() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
        // In GIOP 1.2, a wchar is encoded as an unsigned octet length
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        // followed by the octets of the converted wchar.
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
        int numBytes = read_octet();
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        char[] result = getConvertedChars(numBytes, getWCharConverter());
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        // Did the provided bytes convert to more than one
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        // character?  This may come up as more unicode values are
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
        // assigned, and a single 16 bit Java char isn't enough.
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
        // Better to use strings for i18n purposes.
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
        if (getWCharConverter().getNumChars() > 1)
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
            throw wrapper.btcResultMoreThanOneChar() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
        return result[0];
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
    public String read_wstring() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
        // In GIOP 1.2, wstrings are not terminated by a null.  The
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
        // length is the number of octets in the converted format.
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
        // A zero length string is represented with the 4 byte length
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
        // value of 0.
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
        int len = read_long();
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
        //
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
        // IMPORTANT: Do not replace 'new String("")' with "", it may result
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
        // in a Serialization bug (See serialization.zerolengthstring) and
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        // bug id: 4728756 for details
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
        if (len == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
            return new String("");
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
        checkForNegativeLength(len);
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
        return new String(getConvertedChars(len, getWCharConverter()),
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
                          0,
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
                          getWCharConverter().getNumChars());
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
}