CDRInputStream_1_2.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2000, 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 */
25package com.sun.corba.se.impl.encoding;
26
27import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
28import com.sun.corba.se.impl.orbutil.ORBConstants;
29
30public class CDRInputStream_1_2 extends CDRInputStream_1_1
31{
32    // Indicates whether the header is padded. In GIOP 1.2 and above,
33    // the body must be aligned on an 8-octet boundary, and so the header is
34    // padded appropriately. However, if there is no body to a request or reply
35    // message, there is no header padding, in the unfragmented case.
36    protected boolean headerPadding;
37
38    // used to remember headerPadding flag when mark() and restore() are used.
39    protected boolean restoreHeaderPadding;
40
41    // Called by RequestMessage_1_2 or ReplyMessage_1_2 classes only.
42    void setHeaderPadding(boolean headerPadding) {
43        this.headerPadding = headerPadding;
44    }
45
46    // the mark and reset methods have been overridden to remember the
47    // headerPadding flag.
48
49    public void mark(int readlimit) {
50        super.mark(readlimit);
51        restoreHeaderPadding = headerPadding;
52    }
53
54    public void reset() {
55        super.reset();
56        headerPadding = restoreHeaderPadding;
57        restoreHeaderPadding = false;
58    }
59
60    // Template method
61    // This method has been overriden to ensure that the duplicated stream
62    // inherits the headerPadding flag, in case of GIOP 1.2 and above, streams.
63    public CDRInputStreamBase dup() {
64        CDRInputStreamBase result = super.dup();
65        ((CDRInputStream_1_2)result).headerPadding = this.headerPadding;
66        return result;
67    }
68
69    protected void alignAndCheck(int align, int n) {
70
71        // headerPadding bit is set by read method of the RequestMessage_1_2
72        // or ReplyMessage_1_2 classes. When set, the very first body read
73        // operation (from the stub code) would trigger an alignAndCheck
74        // method call, that would in turn skip the header padding that was
75        // inserted during the earlier write operation by the sender. The
76        // padding ensures that the body is aligned on an 8-octet boundary,
77        // for GIOP versions 1.2 and beyond.
78        if (headerPadding == true) {
79            headerPadding = false;
80            alignOnBoundary(ORBConstants.GIOP_12_MSG_BODY_ALIGNMENT);
81        }
82
83        checkBlockLength(align, n);
84
85        // WARNING: Must compute real alignment after calling
86        // checkBlockLength since it may move the position
87
88        // In GIOP 1.2, a fragment may end with some alignment
89        // padding (which leads to all fragments ending perfectly
90        // on evenly divisible 8 byte boundaries).  A new fragment
91        // never requires alignment with the header since it ends
92        // on an 8 byte boundary.
93
94        int alignIncr = computeAlignment(bbwi.position(),align);
95        bbwi.position(bbwi.position() + alignIncr);
96
97        if (bbwi.position() + n > bbwi.buflen) {
98            grow(1, n);
99        }
100    }
101
102    public GIOPVersion getGIOPVersion() {
103        return GIOPVersion.V1_2;
104    }
105
106    public char read_wchar() {
107        // In GIOP 1.2, a wchar is encoded as an unsigned octet length
108        // followed by the octets of the converted wchar.
109        int numBytes = read_octet();
110
111        char[] result = getConvertedChars(numBytes, getWCharConverter());
112
113        // Did the provided bytes convert to more than one
114        // character?  This may come up as more unicode values are
115        // assigned, and a single 16 bit Java char isn't enough.
116        // Better to use strings for i18n purposes.
117        if (getWCharConverter().getNumChars() > 1)
118            throw wrapper.btcResultMoreThanOneChar() ;
119
120        return result[0];
121    }
122
123    public String read_wstring() {
124        // In GIOP 1.2, wstrings are not terminated by a null.  The
125        // length is the number of octets in the converted format.
126        // A zero length string is represented with the 4 byte length
127        // value of 0.
128
129        int len = read_long();
130
131        //
132        // IMPORTANT: Do not replace 'new String("")' with "", it may result
133        // in a Serialization bug (See serialization.zerolengthstring) and
134        // bug id: 4728756 for details
135        if (len == 0)
136            return new String("");
137
138        checkForNegativeLength(len);
139
140        return new String(getConvertedChars(len, getWCharConverter()),
141                          0,
142                          getWCharConverter().getNumChars());
143    }
144}
145