KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)CDROutputStream_1_1.java 1.17 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 org.omg.CORBA.CompletionStatus JavaDoc;
10 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
11 import com.sun.corba.se.impl.encoding.CodeSetConversion;
12
13 public class CDROutputStream_1_1 extends CDROutputStream_1_0
14 {
15     // This is used to keep indirections working across fragments. When added
16
// to the current bbwi.position(), the result is the current position
17
// in the byte stream without any fragment headers.
18
//
19
// It is equal to the following:
20
//
21
// n = number of buffers (0 is original buffer, 1 is first fragment, etc)
22
//
23
// n == 0, fragmentOffset = 0
24
//
25
// n > 0, fragmentOffset
26
// = sum i=[1,n] { bbwi_i-1_.size - buffer i header length }
27
//
28
protected int fragmentOffset = 0;
29
30     protected void alignAndReserve(int align, int n) {
31
32         // Notice that in 1.1, we won't end a fragment with
33
// alignment padding. We also won't guarantee that
34
// our fragments end on evenly divisible 8 byte
35
// boundaries. There may be alignment
36
// necessary with the header of the next fragment
37
// since the header isn't aligned on an 8 byte
38
// boundary, so we have to calculate it twice.
39

40         int alignment = computeAlignment(align);
41
42         if (bbwi.position() + n + alignment > bbwi.buflen) {
43             grow(align, n);
44
45             // Must recompute the alignment after a grow.
46
// In the case of fragmentation, the alignment
47
// calculation may no longer be correct.
48

49             // People shouldn't be able to set their fragment
50
// sizes so small that the fragment header plus
51
// this alignment fills the entire buffer.
52
alignment = computeAlignment(align);
53         }
54
55         bbwi.position(bbwi.position() + alignment);
56     }
57
58     protected void grow(int align, int n) {
59         // Save the current size for possible post-fragmentation calculation
60
int oldSize = bbwi.position();
61
62         super.grow(align, n);
63    
64         // At this point, if we fragmented, we should have a ByteBufferWithInfo
65
// with the fragment header already marshalled. The size and length fields
66
// should be updated accordingly, and the fragmented flag should be set.
67
if (bbwi.fragmented) {
68
69             // Clear the flag
70
bbwi.fragmented = false;
71
72             // Update fragmentOffset so indirections work properly.
73
// At this point, oldSize is the entire length of the
74
// previous buffer. bbwi.position() is the length of the
75
// fragment header of this buffer.
76
fragmentOffset += (oldSize - bbwi.position());
77         }
78     }
79
80     public int get_offset() {
81     return bbwi.position() + fragmentOffset;
82     }
83
84     public GIOPVersion getGIOPVersion() {
85         return GIOPVersion.V1_1;
86     }
87
88     public void write_wchar(char x)
89     {
90         // In GIOP 1.1, interoperability with wchar is limited
91
// to 2 byte fixed width encodings. CORBA formal 99-10-07 15.3.1.6.
92
// Note that the following code prohibits UTF-16 with a byte
93
// order marker (which would result in 4 bytes).
94
CodeSetConversion.CTBConverter converter = getWCharConverter();
95
96         converter.convert(x);
97
98         if (converter.getNumBytes() != 2)
99         throw wrapper.badGiop11Ctb(CompletionStatus.COMPLETED_MAYBE);
100
101         alignAndReserve(converter.getAlignment(),
102                         converter.getNumBytes());
103
104         parent.write_octet_array(converter.getBytes(),
105                                  0,
106                                  converter.getNumBytes());
107     }
108
109     public void write_wstring(String JavaDoc value)
110     {
111         if (value == null) {
112         throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE);
113         }
114
115         // The length is the number of code points (which are 2 bytes each)
116
// including the 2 byte null. See CORBA formal 99-10-07 15.3.2.7.
117

118         int len = value.length() + 1;
119
120         write_long(len);
121
122         CodeSetConversion.CTBConverter converter = getWCharConverter();
123
124         converter.convert(value);
125
126         internalWriteOctetArray(converter.getBytes(), 0, converter.getNumBytes());
127
128         // Write the 2 byte null ending
129
write_short((short)0);
130     }
131 }
132
133
Popular Tags