KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > CDRInputStream_1_1


1 /*
2  * @(#)CDRInputStream_1_1.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.corba.se.impl.encoding;
8
9 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
10
11 public class CDRInputStream_1_1 extends CDRInputStream_1_0
12 {
13     // See notes in CDROutputStream
14
protected int fragmentOffset = 0;
15
16     public GIOPVersion getGIOPVersion() {
17         return GIOPVersion.V1_1;
18     }
19
20     // Template method
21
public CDRInputStreamBase dup() {
22         CDRInputStreamBase result = super.dup();
23
24         ((CDRInputStream_1_1)result).fragmentOffset = this.fragmentOffset;
25
26         return result;
27     }
28
29     protected int get_offset() {
30     return bbwi.position() + fragmentOffset;
31     }
32
33     protected void alignAndCheck(int align, int n) {
34
35
36         checkBlockLength(align, n);
37
38         // WARNING: Must compute real alignment after calling
39
// checkBlockLength since it may move the position
40
int alignment = computeAlignment(bbwi.position(), align);
41
42         if (bbwi.position() + n + alignment > bbwi.buflen) {
43
44             // Some other ORBs may have found a way to send 1.1
45
// fragments which put alignment bytes at the end
46
// of a fragment
47
if (bbwi.position() + alignment == bbwi.buflen)
48             {
49                 bbwi.position(bbwi.position() + alignment);
50             }
51
52             grow(align, n);
53
54             // We must recalculate the alignment after a possible
55
// fragmentation since the new bbwi.position() (after the header)
56
// may require a different alignment.
57

58             alignment = computeAlignment(bbwi.position(), align);
59         }
60
61         bbwi.position(bbwi.position() + alignment);
62     }
63
64     //
65
// This can be overridden....
66
//
67
protected void grow(int align, int n) {
68                 
69         bbwi.needed = n;
70
71         // Save the size of the current buffer for
72
// possible fragmentOffset calculation
73
int oldSize = bbwi.position();
74
75         bbwi = bufferManagerRead.underflow(bbwi);
76
77         if (bbwi.fragmented) {
78             
79             // By this point we should be guaranteed to have
80
// a new fragment whose header has already been
81
// unmarshalled. bbwi.position() should point to the
82
// end of the header.
83
fragmentOffset += (oldSize - bbwi.position());
84
85             markAndResetHandler.fragmentationOccured(bbwi);
86
87             // Clear the flag
88
bbwi.fragmented = false;
89         }
90     }
91
92     // Mark/reset ---------------------------------------
93

94     private class FragmentableStreamMemento extends StreamMemento
95     {
96         private int fragmentOffset_;
97
98         public FragmentableStreamMemento()
99         {
100             super();
101
102             fragmentOffset_ = fragmentOffset;
103         }
104     }
105
106     public java.lang.Object JavaDoc createStreamMemento() {
107         return new FragmentableStreamMemento();
108     }
109
110     public void restoreInternalState(java.lang.Object JavaDoc streamMemento)
111     {
112         super.restoreInternalState(streamMemento);
113
114         fragmentOffset
115             = ((FragmentableStreamMemento)streamMemento).fragmentOffset_;
116     }
117
118     // --------------------------------------------------
119

120     public char read_wchar() {
121         // In GIOP 1.1, interoperability with wchar is limited
122
// to 2 byte fixed width encodings. CORBA formal 99-10-07 15.3.1.6.
123
// WARNING: For UTF-16, this means that there can be no
124
// byte order marker, so it must default to big endian!
125
alignAndCheck(2, 2);
126
127         // Because of the alignAndCheck, we should be guaranteed
128
// 2 bytes of real data.
129
char[] result = getConvertedChars(2, getWCharConverter());
130
131         // Did the provided bytes convert to more than one
132
// character? This may come up as more unicode values are
133
// assigned, and a single 16 bit Java char isn't enough.
134
// Better to use strings for i18n purposes.
135
if (getWCharConverter().getNumChars() > 1)
136         throw wrapper.btcResultMoreThanOneChar() ;
137
138         return result[0];
139     }
140
141     public String JavaDoc read_wstring() {
142         // In GIOP 1.1, interoperability with wchar is limited
143
// to 2 byte fixed width encodings. CORBA formal 99-10-07 15.3.1.6.
144
int len = read_long();
145
146         // Workaround for ORBs which send string lengths of
147
// zero to mean empty string.
148
//
149
// IMPORTANT: Do not replace 'new String("")' with "", it may result
150
// in a Serialization bug (See serialization.zerolengthstring) and
151
// bug id: 4728756 for details
152
if (len == 0)
153             return new String JavaDoc("");
154
155         checkForNegativeLength(len);
156
157         // Don't include the two byte null for the
158
// following computations. Remember that since we're limited
159
// to a 2 byte fixed width code set, the "length" was the
160
// number of such 2 byte code points plus a 2 byte null.
161
len = len - 1;
162
163         char[] result = getConvertedChars(len * 2, getWCharConverter());
164
165         // Skip over the 2 byte null
166
read_short();
167
168         return new String JavaDoc(result, 0, getWCharConverter().getNumChars());
169     }
170
171 }
172
Popular Tags