author | coffeys |
Fri, 11 Nov 2011 10:16:17 +0000 | |
changeset 11044 | 758078ec0816 |
parent 5555 | b2b5ed3f0d0d |
child 16136 | f5a39cbcd82f |
permissions | -rw-r--r-- |
4 | 1 |
/* |
11044
758078ec0816
7091388: Regular unexplained npe's from corba libs after system has been running for days
coffeys
parents:
5555
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
4 | 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 |
|
5555 | 7 |
* published by the Free Software Foundation. Oracle designates this |
4 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5555 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4 | 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 |
* |
|
5555 | 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. |
|
4 | 24 |
*/ |
25 |
/* |
|
26 |
* Licensed Materials - Property of IBM |
|
27 |
* RMI-IIOP v1.0 |
|
28 |
* Copyright IBM Corp. 1998 1999 All Rights Reserved |
|
29 |
* |
|
30 |
*/ |
|
31 |
||
32 |
package com.sun.corba.se.impl.encoding; |
|
33 |
||
34 |
import java.io.ByteArrayOutputStream; |
|
35 |
import java.io.IOException; |
|
36 |
import java.io.Serializable; |
|
37 |
import java.io.ByteArrayOutputStream; |
|
38 |
import java.io.ObjectOutputStream; |
|
39 |
import java.io.IOException; |
|
40 |
import java.lang.reflect.Method; |
|
41 |
import java.lang.reflect.InvocationTargetException; |
|
42 |
import java.math.BigDecimal; |
|
43 |
import java.nio.ByteBuffer; |
|
44 |
import java.rmi.Remote; |
|
45 |
import java.security.AccessController; |
|
46 |
import java.security.PrivilegedExceptionAction; |
|
47 |
import java.security.PrivilegedActionException; |
|
48 |
import java.util.Hashtable; |
|
49 |
import java.util.Stack; |
|
50 |
||
51 |
import javax.rmi.CORBA.Util; |
|
52 |
import javax.rmi.CORBA.ValueHandler; |
|
53 |
import javax.rmi.CORBA.ValueHandlerMultiFormat; |
|
54 |
||
55 |
import org.omg.CORBA.CustomMarshal; |
|
56 |
import org.omg.CORBA.DataOutputStream; |
|
57 |
import org.omg.CORBA.TypeCodePackage.BadKind; |
|
58 |
import org.omg.CORBA.SystemException; |
|
59 |
import org.omg.CORBA.CompletionStatus; |
|
60 |
import org.omg.CORBA.Object; |
|
61 |
import org.omg.CORBA.Principal; |
|
62 |
import org.omg.CORBA.TypeCode; |
|
63 |
import org.omg.CORBA.Any; |
|
64 |
import org.omg.CORBA.VM_CUSTOM; |
|
65 |
import org.omg.CORBA.VM_TRUNCATABLE; |
|
66 |
import org.omg.CORBA.VM_NONE; |
|
67 |
import org.omg.CORBA.portable.IDLEntity; |
|
68 |
import org.omg.CORBA.portable.CustomValue; |
|
69 |
import org.omg.CORBA.portable.StreamableValue; |
|
70 |
import org.omg.CORBA.portable.BoxedValueHelper; |
|
71 |
import org.omg.CORBA.portable.OutputStream; |
|
72 |
import org.omg.CORBA.portable.ValueBase; |
|
73 |
||
74 |
import com.sun.org.omg.CORBA.portable.ValueHelper; |
|
75 |
||
76 |
import com.sun.corba.se.pept.protocol.MessageMediator; |
|
77 |
import com.sun.corba.se.pept.transport.ByteBufferPool; |
|
78 |
||
79 |
import com.sun.corba.se.spi.ior.iiop.GIOPVersion; |
|
80 |
import com.sun.corba.se.spi.ior.IOR; |
|
81 |
import com.sun.corba.se.spi.ior.IORFactories; |
|
82 |
import com.sun.corba.se.spi.orb.ORB; |
|
83 |
import com.sun.corba.se.spi.orb.ORBVersionFactory; |
|
84 |
import com.sun.corba.se.spi.orb.ORBVersion; |
|
85 |
import com.sun.corba.se.spi.protocol.CorbaMessageMediator; |
|
86 |
import com.sun.corba.se.spi.logging.CORBALogDomains; |
|
87 |
||
88 |
import com.sun.corba.se.impl.encoding.ByteBufferWithInfo; |
|
89 |
import com.sun.corba.se.impl.encoding.MarshalOutputStream; |
|
90 |
import com.sun.corba.se.impl.encoding.CodeSetConversion; |
|
91 |
import com.sun.corba.se.impl.corba.TypeCodeImpl; |
|
92 |
import com.sun.corba.se.impl.orbutil.CacheTable; |
|
93 |
import com.sun.corba.se.impl.orbutil.ORBUtility; |
|
94 |
import com.sun.corba.se.impl.orbutil.RepositoryIdStrings; |
|
95 |
import com.sun.corba.se.impl.orbutil.RepositoryIdUtility; |
|
96 |
import com.sun.corba.se.impl.orbutil.RepositoryIdFactory; |
|
97 |
import com.sun.corba.se.impl.util.Utility; |
|
98 |
import com.sun.corba.se.impl.logging.ORBUtilSystemException; |
|
99 |
||
100 |
public class CDROutputStream_1_0 extends CDROutputStreamBase |
|
101 |
{ |
|
102 |
private static final int INDIRECTION_TAG = 0xffffffff; |
|
103 |
||
104 |
protected boolean littleEndian; |
|
105 |
protected BufferManagerWrite bufferManagerWrite; |
|
106 |
ByteBufferWithInfo bbwi; |
|
107 |
||
108 |
protected ORB orb; |
|
109 |
protected ORBUtilSystemException wrapper ; |
|
110 |
||
111 |
protected boolean debug = false; |
|
112 |
||
113 |
protected int blockSizeIndex = -1; |
|
114 |
protected int blockSizePosition = 0; |
|
115 |
||
116 |
protected byte streamFormatVersion; |
|
117 |
||
118 |
private static final int DEFAULT_BUFFER_SIZE = 1024; |
|
119 |
private static final String kWriteMethod = "write"; |
|
120 |
||
121 |
// Codebase cache |
|
122 |
private CacheTable codebaseCache = null; |
|
123 |
||
124 |
// Value cache |
|
125 |
private CacheTable valueCache = null; |
|
126 |
||
127 |
// Repository ID cache |
|
128 |
private CacheTable repositoryIdCache = null; |
|
129 |
||
130 |
// Write end flag |
|
131 |
private int end_flag = 0; |
|
132 |
||
133 |
// Beginning with the resolution to interop issue 3526, |
|
134 |
// only enclosing chunked valuetypes are taken into account |
|
135 |
// when computing the nesting level. However, we still need |
|
136 |
// the old computation around for interoperability with our |
|
137 |
// older ORBs. |
|
138 |
private int chunkedValueNestingLevel = 0; |
|
139 |
||
140 |
private boolean mustChunk = false; |
|
141 |
||
142 |
// In block marker |
|
143 |
protected boolean inBlock = false; |
|
144 |
||
145 |
// Last end tag position |
|
146 |
private int end_flag_position = 0; |
|
147 |
private int end_flag_index = 0; |
|
148 |
||
149 |
// ValueHandler |
|
150 |
private ValueHandler valueHandler = null; |
|
151 |
||
152 |
// Repository ID handlers |
|
153 |
private RepositoryIdUtility repIdUtil; |
|
154 |
private RepositoryIdStrings repIdStrs; |
|
155 |
||
156 |
// Code set converters (created when first needed) |
|
157 |
private CodeSetConversion.CTBConverter charConverter; |
|
158 |
private CodeSetConversion.CTBConverter wcharConverter; |
|
159 |
||
160 |
// REVISIT - This should be re-factored so that including whether |
|
161 |
// to use pool byte buffers or not doesn't need to be known. |
|
162 |
public void init(org.omg.CORBA.ORB orb, |
|
163 |
boolean littleEndian, |
|
164 |
BufferManagerWrite bufferManager, |
|
165 |
byte streamFormatVersion, |
|
166 |
boolean usePooledByteBuffers) |
|
167 |
{ |
|
168 |
// ORB must not be null. See CDROutputStream constructor. |
|
169 |
this.orb = (ORB)orb; |
|
170 |
this.wrapper = ORBUtilSystemException.get( this.orb, |
|
171 |
CORBALogDomains.RPC_ENCODING ) ; |
|
172 |
debug = this.orb.transportDebugFlag; |
|
173 |
||
174 |
this.littleEndian = littleEndian; |
|
175 |
this.bufferManagerWrite = bufferManager; |
|
176 |
this.bbwi = new ByteBufferWithInfo(orb, bufferManager, usePooledByteBuffers); |
|
177 |
this.streamFormatVersion = streamFormatVersion; |
|
178 |
||
179 |
createRepositoryIdHandlers(); |
|
180 |
} |
|
181 |
||
182 |
public void init(org.omg.CORBA.ORB orb, |
|
183 |
boolean littleEndian, |
|
184 |
BufferManagerWrite bufferManager, |
|
185 |
byte streamFormatVersion) |
|
186 |
{ |
|
187 |
init(orb, littleEndian, bufferManager, streamFormatVersion, true); |
|
188 |
} |
|
189 |
||
190 |
private final void createRepositoryIdHandlers() |
|
191 |
{ |
|
192 |
if (orb != null) { |
|
193 |
// Get the appropriate versions based on the ORB version. The |
|
194 |
// ORB versioning info is only in the core ORB. |
|
195 |
repIdUtil |
|
196 |
= RepositoryIdFactory.getRepIdUtility(orb); |
|
197 |
repIdStrs |
|
198 |
= RepositoryIdFactory.getRepIdStringsFactory(orb); |
|
199 |
} else { |
|
200 |
// Get the latest versions |
|
201 |
repIdUtil = RepositoryIdFactory.getRepIdUtility(); |
|
202 |
repIdStrs = RepositoryIdFactory.getRepIdStringsFactory(); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
public BufferManagerWrite getBufferManager() |
|
207 |
{ |
|
208 |
return bufferManagerWrite; |
|
209 |
} |
|
210 |
||
211 |
public byte[] toByteArray() { |
|
212 |
byte[] it; |
|
213 |
||
214 |
it = new byte[bbwi.position()]; |
|
215 |
||
216 |
// Micro-benchmarks show ByteBuffer.get(int) out perform the bulk |
|
217 |
// ByteBuffer.get(byte[], offset, length). |
|
218 |
for (int i = 0; i < bbwi.position(); i++) |
|
219 |
it[i] = bbwi.byteBuffer.get(i); |
|
220 |
||
221 |
return it; |
|
222 |
} |
|
223 |
||
224 |
public GIOPVersion getGIOPVersion() { |
|
225 |
return GIOPVersion.V1_0; |
|
226 |
} |
|
227 |
||
228 |
// Called by Request and Reply message. Valid for GIOP versions >= 1.2 only. |
|
229 |
// Illegal for GIOP versions < 1.2. |
|
230 |
void setHeaderPadding(boolean headerPadding) { |
|
231 |
throw wrapper.giopVersionError(); |
|
232 |
} |
|
233 |
||
234 |
protected void handleSpecialChunkBegin(int requiredSize) |
|
235 |
{ |
|
236 |
// No-op for GIOP 1.0 |
|
237 |
} |
|
238 |
||
239 |
protected void handleSpecialChunkEnd() |
|
240 |
{ |
|
241 |
// No-op for GIOP 1.0 |
|
242 |
} |
|
243 |
||
244 |
protected final int computeAlignment(int align) { |
|
245 |
if (align > 1) { |
|
246 |
int incr = bbwi.position() & (align - 1); |
|
247 |
if (incr != 0) |
|
248 |
return align - incr; |
|
249 |
} |
|
250 |
||
251 |
return 0; |
|
252 |
} |
|
253 |
||
254 |
protected void alignAndReserve(int align, int n) { |
|
255 |
||
256 |
bbwi.position(bbwi.position() + computeAlignment(align)); |
|
257 |
||
258 |
if (bbwi.position() + n > bbwi.buflen) |
|
259 |
grow(align, n); |
|
260 |
} |
|
261 |
||
262 |
// |
|
263 |
// Default implementation of grow. Subclassers may override this. |
|
264 |
// Always grow the single buffer. This needs to delegate |
|
265 |
// fragmentation policy for IIOP 1.1. |
|
266 |
// |
|
267 |
protected void grow(int align, int n) |
|
268 |
{ |
|
269 |
bbwi.needed = n; |
|
270 |
||
271 |
bufferManagerWrite.overflow(bbwi); |
|
272 |
} |
|
273 |
||
274 |
public final void putEndian() throws SystemException { |
|
275 |
write_boolean(littleEndian); |
|
276 |
} |
|
277 |
||
278 |
public final boolean littleEndian() { |
|
279 |
return littleEndian; |
|
280 |
} |
|
281 |
||
282 |
void freeInternalCaches() { |
|
283 |
if (codebaseCache != null) |
|
284 |
codebaseCache.done(); |
|
285 |
||
286 |
if (valueCache != null) |
|
287 |
valueCache.done(); |
|
288 |
||
289 |
if (repositoryIdCache != null) |
|
290 |
repositoryIdCache.done(); |
|
291 |
} |
|
292 |
||
293 |
// No such type in java |
|
294 |
public final void write_longdouble(double x) |
|
295 |
{ |
|
296 |
throw wrapper.longDoubleNotImplemented( |
|
297 |
CompletionStatus.COMPLETED_MAYBE ) ; |
|
298 |
} |
|
299 |
||
300 |
public void write_octet(byte x) |
|
301 |
{ |
|
302 |
// The 'if' stmt is commented out since we need the alignAndReserve to |
|
303 |
// be called, particularly when the first body byte is written, |
|
304 |
// to induce header padding to align the body on a 8-octet boundary, |
|
305 |
// for GIOP versions 1.2 and above. Refer to internalWriteOctetArray() |
|
306 |
// method that also has a similar change. |
|
307 |
//if (bbwi.position() + 1 > bbwi.buflen) |
|
308 |
alignAndReserve(1, 1); |
|
309 |
||
310 |
// REVISIT - Should just use ByteBuffer.put(byte) and let it |
|
311 |
// increment the ByteBuffer position. This is true |
|
312 |
// for all write operations in this file. |
|
313 |
||
314 |
bbwi.byteBuffer.put(bbwi.position(), x); |
|
315 |
bbwi.position(bbwi.position() + 1); |
|
316 |
||
317 |
} |
|
318 |
||
319 |
public final void write_boolean(boolean x) |
|
320 |
{ |
|
321 |
write_octet(x? (byte)1:(byte)0); |
|
322 |
} |
|
323 |
||
324 |
public void write_char(char x) |
|
325 |
{ |
|
326 |
CodeSetConversion.CTBConverter converter = getCharConverter(); |
|
327 |
||
328 |
converter.convert(x); |
|
329 |
||
330 |
// CORBA formal 99-10-07 15.3.1.6: "In the case of multi-byte encodings |
|
331 |
// of characters, a single instance of the char type may only |
|
332 |
// hold one octet of any multi-byte character encoding." |
|
333 |
if (converter.getNumBytes() > 1) |
|
334 |
throw wrapper.invalidSingleCharCtb(CompletionStatus.COMPLETED_MAYBE); |
|
335 |
||
336 |
write_octet(converter.getBytes()[0]); |
|
337 |
} |
|
338 |
||
339 |
// These wchar methods are only used when talking to |
|
340 |
// legacy ORBs, now. |
|
341 |
private final void writeLittleEndianWchar(char x) { |
|
342 |
bbwi.byteBuffer.put(bbwi.position(), (byte)(x & 0xFF)); |
|
343 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)((x >>> 8) & 0xFF)); |
|
344 |
bbwi.position(bbwi.position() + 2); |
|
345 |
} |
|
346 |
||
347 |
private final void writeBigEndianWchar(char x) { |
|
348 |
bbwi.byteBuffer.put(bbwi.position(), (byte)((x >>> 8) & 0xFF)); |
|
349 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)(x & 0xFF)); |
|
350 |
bbwi.position(bbwi.position() + 2); |
|
351 |
} |
|
352 |
||
353 |
private final void writeLittleEndianShort(short x) { |
|
354 |
bbwi.byteBuffer.put(bbwi.position(), (byte)(x & 0xFF)); |
|
355 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)((x >>> 8) & 0xFF)); |
|
356 |
bbwi.position(bbwi.position() + 2); |
|
357 |
} |
|
358 |
||
359 |
private final void writeBigEndianShort(short x) { |
|
360 |
bbwi.byteBuffer.put(bbwi.position(), (byte)((x >>> 8) & 0xFF)); |
|
361 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)(x & 0xFF)); |
|
362 |
bbwi.position(bbwi.position() + 2); |
|
363 |
} |
|
364 |
||
365 |
private final void writeLittleEndianLong(int x) { |
|
366 |
bbwi.byteBuffer.put(bbwi.position(), (byte)(x & 0xFF)); |
|
367 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)((x >>> 8) & 0xFF)); |
|
368 |
bbwi.byteBuffer.put(bbwi.position() + 2, (byte)((x >>> 16) & 0xFF)); |
|
369 |
bbwi.byteBuffer.put(bbwi.position() + 3, (byte)((x >>> 24) & 0xFF)); |
|
370 |
bbwi.position(bbwi.position() + 4); |
|
371 |
} |
|
372 |
||
373 |
private final void writeBigEndianLong(int x) { |
|
374 |
bbwi.byteBuffer.put(bbwi.position(), (byte)((x >>> 24) & 0xFF)); |
|
375 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)((x >>> 16) & 0xFF)); |
|
376 |
bbwi.byteBuffer.put(bbwi.position() + 2, (byte)((x >>> 8) & 0xFF)); |
|
377 |
bbwi.byteBuffer.put(bbwi.position() + 3, (byte)(x & 0xFF)); |
|
378 |
bbwi.position(bbwi.position() + 4); |
|
379 |
} |
|
380 |
||
381 |
private final void writeLittleEndianLongLong(long x) { |
|
382 |
bbwi.byteBuffer.put(bbwi.position(), (byte)(x & 0xFF)); |
|
383 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)((x >>> 8) & 0xFF)); |
|
384 |
bbwi.byteBuffer.put(bbwi.position() + 2, (byte)((x >>> 16) & 0xFF)); |
|
385 |
bbwi.byteBuffer.put(bbwi.position() + 3, (byte)((x >>> 24) & 0xFF)); |
|
386 |
bbwi.byteBuffer.put(bbwi.position() + 4, (byte)((x >>> 32) & 0xFF)); |
|
387 |
bbwi.byteBuffer.put(bbwi.position() + 5, (byte)((x >>> 40) & 0xFF)); |
|
388 |
bbwi.byteBuffer.put(bbwi.position() + 6, (byte)((x >>> 48) & 0xFF)); |
|
389 |
bbwi.byteBuffer.put(bbwi.position() + 7, (byte)((x >>> 56) & 0xFF)); |
|
390 |
bbwi.position(bbwi.position() + 8); |
|
391 |
} |
|
392 |
||
393 |
private final void writeBigEndianLongLong(long x) { |
|
394 |
bbwi.byteBuffer.put(bbwi.position(), (byte)((x >>> 56) & 0xFF)); |
|
395 |
bbwi.byteBuffer.put(bbwi.position() + 1, (byte)((x >>> 48) & 0xFF)); |
|
396 |
bbwi.byteBuffer.put(bbwi.position() + 2, (byte)((x >>> 40) & 0xFF)); |
|
397 |
bbwi.byteBuffer.put(bbwi.position() + 3, (byte)((x >>> 32) & 0xFF)); |
|
398 |
bbwi.byteBuffer.put(bbwi.position() + 4, (byte)((x >>> 24) & 0xFF)); |
|
399 |
bbwi.byteBuffer.put(bbwi.position() + 5, (byte)((x >>> 16) & 0xFF)); |
|
400 |
bbwi.byteBuffer.put(bbwi.position() + 6, (byte)((x >>> 8) & 0xFF)); |
|
401 |
bbwi.byteBuffer.put(bbwi.position() + 7, (byte)(x & 0xFF)); |
|
402 |
bbwi.position(bbwi.position() + 8); |
|
403 |
} |
|
404 |
||
405 |
public void write_wchar(char x) |
|
406 |
{ |
|
407 |
// Don't allow transmission of wchar/wstring data with |
|
408 |
// foreign ORBs since it's against the spec. |
|
409 |
if (ORBUtility.isForeignORB(orb)) { |
|
410 |
throw wrapper.wcharDataInGiop10(CompletionStatus.COMPLETED_MAYBE); |
|
411 |
} |
|
412 |
||
413 |
// If it's one of our legacy ORBs, do what they did: |
|
414 |
alignAndReserve(2, 2); |
|
415 |
||
416 |
if (littleEndian) { |
|
417 |
writeLittleEndianWchar(x); |
|
418 |
} else { |
|
419 |
writeBigEndianWchar(x); |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
public void write_short(short x) |
|
424 |
{ |
|
425 |
alignAndReserve(2, 2); |
|
426 |
||
427 |
if (littleEndian) { |
|
428 |
writeLittleEndianShort(x); |
|
429 |
} else { |
|
430 |
writeBigEndianShort(x); |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
public final void write_ushort(short x) |
|
435 |
{ |
|
436 |
write_short(x); |
|
437 |
} |
|
438 |
||
439 |
public void write_long(int x) |
|
440 |
{ |
|
441 |
alignAndReserve(4, 4); |
|
442 |
||
443 |
if (littleEndian) { |
|
444 |
writeLittleEndianLong(x); |
|
445 |
} else { |
|
446 |
writeBigEndianLong(x); |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
public final void write_ulong(int x) |
|
451 |
{ |
|
452 |
write_long(x); |
|
453 |
} |
|
454 |
||
455 |
public void write_longlong(long x) |
|
456 |
{ |
|
457 |
alignAndReserve(8, 8); |
|
458 |
||
459 |
if (littleEndian) { |
|
460 |
writeLittleEndianLongLong(x); |
|
461 |
} else { |
|
462 |
writeBigEndianLongLong(x); |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
public final void write_ulonglong(long x) |
|
467 |
{ |
|
468 |
write_longlong(x); |
|
469 |
} |
|
470 |
||
471 |
public final void write_float(float x) |
|
472 |
{ |
|
473 |
write_long(Float.floatToIntBits(x)); |
|
474 |
} |
|
475 |
||
476 |
public final void write_double(double x) |
|
477 |
{ |
|
478 |
write_longlong(Double.doubleToLongBits(x)); |
|
479 |
} |
|
480 |
||
481 |
public void write_string(String value) |
|
482 |
{ |
|
483 |
writeString(value); |
|
484 |
} |
|
485 |
||
486 |
protected int writeString(String value) |
|
487 |
{ |
|
488 |
if (value == null) { |
|
489 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
490 |
} |
|
491 |
||
492 |
CodeSetConversion.CTBConverter converter = getCharConverter(); |
|
493 |
||
494 |
converter.convert(value); |
|
495 |
||
496 |
// A string is encoded as an unsigned CORBA long for the |
|
497 |
// number of bytes to follow (including a terminating null). |
|
498 |
// There is only one octet per character in the string. |
|
499 |
int len = converter.getNumBytes() + 1; |
|
500 |
||
501 |
handleSpecialChunkBegin(computeAlignment(4) + 4 + len); |
|
502 |
||
503 |
write_long(len); |
|
504 |
int indirection = get_offset() - 4; |
|
505 |
||
506 |
internalWriteOctetArray(converter.getBytes(), 0, converter.getNumBytes()); |
|
507 |
||
508 |
// Write the null ending |
|
509 |
write_octet((byte)0); |
|
510 |
||
511 |
handleSpecialChunkEnd(); |
|
512 |
return indirection; |
|
513 |
} |
|
514 |
||
515 |
public void write_wstring(String value) |
|
516 |
{ |
|
517 |
if (value == null) |
|
518 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
519 |
||
520 |
// Don't allow transmission of wchar/wstring data with |
|
521 |
// foreign ORBs since it's against the spec. |
|
522 |
if (ORBUtility.isForeignORB(orb)) { |
|
523 |
throw wrapper.wcharDataInGiop10(CompletionStatus.COMPLETED_MAYBE); |
|
524 |
} |
|
525 |
||
526 |
// When talking to our legacy ORBs, do what they did: |
|
527 |
int len = value.length() + 1; |
|
528 |
||
529 |
// This will only have an effect if we're already chunking |
|
530 |
handleSpecialChunkBegin(4 + (len * 2) + computeAlignment(4)); |
|
531 |
||
532 |
write_long(len); |
|
533 |
||
534 |
for (int i = 0; i < len - 1; i++) |
|
535 |
write_wchar(value.charAt(i)); |
|
536 |
||
537 |
// Write the null ending |
|
538 |
write_short((short)0); |
|
539 |
||
540 |
// This will only have an effect if we're already chunking |
|
541 |
handleSpecialChunkEnd(); |
|
542 |
} |
|
543 |
||
544 |
// Performs no checks and doesn't tamper with chunking |
|
545 |
void internalWriteOctetArray(byte[] value, int offset, int length) |
|
546 |
{ |
|
547 |
int n = offset; |
|
548 |
||
549 |
// This flag forces the alignAndReserve method to be called the |
|
550 |
// first time an octet is written. This is necessary to ensure |
|
551 |
// that the body is aligned on an 8-octet boundary. Note the 'if' |
|
552 |
// condition inside the 'while' loop below. Also, refer to the |
|
553 |
// write_octet() method that has a similar change. |
|
554 |
boolean align = true; |
|
555 |
||
556 |
while (n < length+offset) { |
|
557 |
int avail; |
|
558 |
int bytes; |
|
559 |
int wanted; |
|
560 |
||
561 |
if ((bbwi.position() + 1 > bbwi.buflen) || align) { |
|
562 |
align = false; |
|
563 |
alignAndReserve(1, 1); |
|
564 |
} |
|
565 |
avail = bbwi.buflen - bbwi.position(); |
|
566 |
wanted = (length + offset) - n; |
|
567 |
bytes = (wanted < avail) ? wanted : avail; |
|
568 |
for (int i = 0; i < bytes; i++) |
|
569 |
bbwi.byteBuffer.put(bbwi.position() + i, value[n+i]); |
|
570 |
bbwi.position(bbwi.position() + bytes); |
|
571 |
n += bytes; |
|
572 |
} |
|
573 |
} |
|
574 |
||
575 |
public final void write_octet_array(byte b[], int offset, int length) |
|
576 |
{ |
|
577 |
if ( b == null ) |
|
578 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
579 |
||
580 |
// This will only have an effect if we're already chunking |
|
581 |
handleSpecialChunkBegin(length); |
|
582 |
||
583 |
internalWriteOctetArray(b, offset, length); |
|
584 |
||
585 |
// This will only have an effect if we're already chunking |
|
586 |
handleSpecialChunkEnd(); |
|
587 |
} |
|
588 |
||
589 |
public void write_Principal(Principal p) |
|
590 |
{ |
|
591 |
write_long(p.name().length); |
|
592 |
write_octet_array(p.name(), 0, p.name().length); |
|
593 |
} |
|
594 |
||
595 |
public void write_any(Any any) |
|
596 |
{ |
|
597 |
if ( any == null ) |
|
598 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
599 |
||
600 |
write_TypeCode(any.type()); |
|
601 |
any.write_value(parent); |
|
602 |
} |
|
603 |
||
604 |
public void write_TypeCode(TypeCode tc) |
|
605 |
{ |
|
606 |
if ( tc == null ) { |
|
607 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
608 |
} |
|
609 |
TypeCodeImpl tci; |
|
610 |
if (tc instanceof TypeCodeImpl) { |
|
611 |
tci = (TypeCodeImpl)tc; |
|
612 |
} |
|
613 |
else { |
|
614 |
tci = new TypeCodeImpl(orb, tc); |
|
615 |
} |
|
616 |
||
617 |
tci.write_value((org.omg.CORBA_2_3.portable.OutputStream)parent); |
|
618 |
} |
|
619 |
||
620 |
public void write_Object(org.omg.CORBA.Object ref) |
|
621 |
{ |
|
622 |
if (ref == null) { |
|
623 |
IOR nullIOR = IORFactories.makeIOR( orb ) ; |
|
624 |
nullIOR.write(parent); |
|
625 |
return; |
|
626 |
} |
|
627 |
||
628 |
// IDL to Java formal 01-06-06 1.21.4.2 |
|
629 |
if (ref instanceof org.omg.CORBA.LocalObject) |
|
630 |
throw wrapper.writeLocalObject(CompletionStatus.COMPLETED_MAYBE); |
|
631 |
||
632 |
IOR ior = ORBUtility.connectAndGetIOR( orb, ref ) ; |
|
633 |
ior.write(parent); |
|
634 |
return; |
|
635 |
} |
|
636 |
||
637 |
// ------------ RMI related methods -------------------------- |
|
638 |
||
639 |
public void write_abstract_interface(java.lang.Object obj) { |
|
640 |
boolean corbaObject = false; // Assume value type. |
|
641 |
org.omg.CORBA.Object theObject = null; |
|
642 |
||
643 |
// Is it a CORBA.Object? |
|
644 |
||
645 |
if (obj != null && obj instanceof org.omg.CORBA.Object) { |
|
646 |
||
647 |
// Yes. |
|
648 |
||
649 |
theObject = (org.omg.CORBA.Object)obj; |
|
650 |
corbaObject = true; |
|
651 |
} |
|
652 |
||
653 |
// Write our flag... |
|
654 |
||
655 |
write_boolean(corbaObject); |
|
656 |
||
657 |
// Now write out the object... |
|
658 |
||
659 |
if (corbaObject) { |
|
660 |
write_Object(theObject); |
|
661 |
} else { |
|
662 |
try { |
|
663 |
write_value((java.io.Serializable)obj); |
|
664 |
} catch(ClassCastException cce) { |
|
665 |
if (obj instanceof java.io.Serializable) |
|
666 |
throw cce; |
|
667 |
else |
|
668 |
ORBUtility.throwNotSerializableForCorba(obj.getClass().getName()); |
|
669 |
} |
|
670 |
} |
|
671 |
} |
|
672 |
||
673 |
public void write_value(Serializable object, Class clz) { |
|
674 |
||
675 |
write_value(object); |
|
676 |
} |
|
677 |
||
678 |
private void writeWStringValue(String string) { |
|
679 |
||
680 |
int indirection = writeValueTag(mustChunk, true, null); |
|
681 |
||
682 |
// Write WStringValue's repository ID |
|
683 |
write_repositoryId(repIdStrs.getWStringValueRepId()); |
|
684 |
||
685 |
// Add indirection for object to indirection table |
|
686 |
updateIndirectionTable(indirection, string, string); |
|
687 |
||
688 |
// Write Value chunk |
|
689 |
if (mustChunk) { |
|
690 |
start_block(); |
|
691 |
end_flag--; |
|
692 |
chunkedValueNestingLevel--; |
|
693 |
} else |
|
694 |
end_flag--; |
|
695 |
||
696 |
write_wstring(string); |
|
697 |
||
698 |
if (mustChunk) |
|
699 |
end_block(); |
|
700 |
||
701 |
// Write end tag |
|
702 |
writeEndTag(mustChunk); |
|
703 |
} |
|
704 |
||
705 |
private void writeArray(Serializable array, Class clazz) { |
|
706 |
||
707 |
if (valueHandler == null) |
|
708 |
valueHandler = ORBUtility.createValueHandler(orb); //d11638 |
|
709 |
||
710 |
// Write value_tag |
|
711 |
int indirection = writeValueTag(mustChunk, true, |
|
712 |
Util.getCodebase(clazz)); |
|
713 |
||
714 |
// Write repository ID |
|
715 |
write_repositoryId(repIdStrs.createSequenceRepID(clazz)); |
|
716 |
||
717 |
// Add indirection for object to indirection table |
|
718 |
updateIndirectionTable(indirection, array, array); |
|
719 |
||
720 |
// Write Value chunk |
|
721 |
if (mustChunk) { |
|
722 |
start_block(); |
|
723 |
end_flag--; |
|
724 |
chunkedValueNestingLevel--; |
|
725 |
} else |
|
726 |
end_flag--; |
|
727 |
||
728 |
if (valueHandler instanceof ValueHandlerMultiFormat) { |
|
729 |
ValueHandlerMultiFormat vh = (ValueHandlerMultiFormat)valueHandler; |
|
730 |
vh.writeValue(parent, array, streamFormatVersion); |
|
731 |
} else |
|
732 |
valueHandler.writeValue(parent, array); |
|
733 |
||
734 |
if (mustChunk) |
|
735 |
end_block(); |
|
736 |
||
737 |
// Write end tag |
|
738 |
writeEndTag(mustChunk); |
|
739 |
} |
|
740 |
||
741 |
private void writeValueBase(org.omg.CORBA.portable.ValueBase object, |
|
742 |
Class clazz) { |
|
743 |
// _REVISIT_ could check to see whether chunking really needed |
|
744 |
mustChunk = true; |
|
745 |
||
746 |
// Write value_tag |
|
747 |
int indirection = writeValueTag(true, true, Util.getCodebase(clazz)); |
|
748 |
||
749 |
// Get rep id |
|
750 |
String repId = ((ValueBase)object)._truncatable_ids()[0]; |
|
751 |
||
752 |
// Write rep id |
|
753 |
write_repositoryId(repId); |
|
754 |
||
755 |
// Add indirection for object to indirection table |
|
756 |
updateIndirectionTable(indirection, object, object); |
|
757 |
||
758 |
// Write Value chunk |
|
759 |
start_block(); |
|
760 |
end_flag--; |
|
761 |
chunkedValueNestingLevel--; |
|
762 |
writeIDLValue(object, repId); |
|
763 |
end_block(); |
|
764 |
||
765 |
// Write end tag |
|
766 |
writeEndTag(true); |
|
767 |
} |
|
768 |
||
769 |
private void writeRMIIIOPValueType(Serializable object, Class clazz) { |
|
770 |
if (valueHandler == null) |
|
771 |
valueHandler = ORBUtility.createValueHandler(orb); //d11638 |
|
772 |
||
773 |
Serializable key = object; |
|
774 |
||
775 |
// Allow the ValueHandler to call writeReplace on |
|
776 |
// the Serializable (if the method is present) |
|
777 |
object = valueHandler.writeReplace(key); |
|
778 |
||
779 |
if (object == null) { |
|
780 |
// Write null tag and return |
|
781 |
write_long(0); |
|
782 |
return; |
|
783 |
} |
|
784 |
||
785 |
if (object != key) { |
|
786 |
if (valueCache != null && valueCache.containsKey(object)) { |
|
787 |
writeIndirection(INDIRECTION_TAG, valueCache.getVal(object)); |
|
788 |
return; |
|
789 |
} |
|
790 |
||
791 |
clazz = object.getClass(); |
|
792 |
} |
|
793 |
||
794 |
if (mustChunk || valueHandler.isCustomMarshaled(clazz)) { |
|
795 |
mustChunk = true; |
|
796 |
} |
|
797 |
||
798 |
// Write value_tag |
|
799 |
int indirection = writeValueTag(mustChunk, true, Util.getCodebase(clazz)); |
|
800 |
||
801 |
// Write rep. id |
|
802 |
write_repositoryId(repIdStrs.createForJavaType(clazz)); |
|
803 |
||
804 |
// Add indirection for object to indirection table |
|
805 |
updateIndirectionTable(indirection, object, key); |
|
806 |
||
807 |
if (mustChunk) { |
|
808 |
// Write Value chunk |
|
809 |
end_flag--; |
|
810 |
chunkedValueNestingLevel--; |
|
811 |
start_block(); |
|
812 |
} else |
|
813 |
end_flag--; |
|
814 |
||
815 |
if (valueHandler instanceof ValueHandlerMultiFormat) { |
|
816 |
ValueHandlerMultiFormat vh = (ValueHandlerMultiFormat)valueHandler; |
|
817 |
vh.writeValue(parent, object, streamFormatVersion); |
|
818 |
} else |
|
819 |
valueHandler.writeValue(parent, object); |
|
820 |
||
821 |
if (mustChunk) |
|
822 |
end_block(); |
|
823 |
||
824 |
// Write end tag |
|
825 |
writeEndTag(mustChunk); |
|
826 |
} |
|
827 |
||
828 |
public void write_value(Serializable object, String repository_id) { |
|
829 |
||
830 |
// Handle null references |
|
831 |
if (object == null) { |
|
832 |
// Write null tag and return |
|
833 |
write_long(0); |
|
834 |
return; |
|
835 |
} |
|
836 |
||
837 |
// Handle shared references |
|
838 |
if (valueCache != null && valueCache.containsKey(object)) { |
|
839 |
writeIndirection(INDIRECTION_TAG, valueCache.getVal(object)); |
|
840 |
return; |
|
841 |
} |
|
842 |
||
843 |
Class clazz = object.getClass(); |
|
844 |
boolean oldMustChunk = mustChunk; |
|
845 |
||
846 |
if (mustChunk) |
|
847 |
mustChunk = true; |
|
848 |
||
849 |
if (inBlock) |
|
850 |
end_block(); |
|
851 |
||
852 |
if (clazz.isArray()) { |
|
853 |
// Handle arrays |
|
854 |
writeArray(object, clazz); |
|
855 |
} else if (object instanceof org.omg.CORBA.portable.ValueBase) { |
|
856 |
// Handle IDL Value types |
|
857 |
writeValueBase((org.omg.CORBA.portable.ValueBase)object, clazz); |
|
858 |
} else if (shouldWriteAsIDLEntity(object)) { |
|
859 |
writeIDLEntity((IDLEntity)object); |
|
860 |
} else if (object instanceof java.lang.String) { |
|
861 |
writeWStringValue((String)object); |
|
862 |
} else if (object instanceof java.lang.Class) { |
|
863 |
writeClass(repository_id, (Class)object); |
|
864 |
} else { |
|
865 |
// RMI-IIOP value type |
|
866 |
writeRMIIIOPValueType(object, clazz); |
|
867 |
} |
|
868 |
||
869 |
mustChunk = oldMustChunk; |
|
870 |
||
871 |
// Check to see if we need to start another block for a |
|
872 |
// possible outer value |
|
873 |
if (mustChunk) |
|
874 |
start_block(); |
|
875 |
||
876 |
} |
|
877 |
||
878 |
public void write_value(Serializable object) |
|
879 |
{ |
|
880 |
write_value(object, (String)null); |
|
881 |
} |
|
882 |
||
883 |
public void write_value(Serializable object, org.omg.CORBA.portable.BoxedValueHelper factory) |
|
884 |
{ |
|
885 |
// Handle null references |
|
886 |
if (object == null) { |
|
887 |
// Write null tag and return |
|
888 |
write_long(0); |
|
889 |
return; |
|
890 |
} |
|
891 |
||
892 |
// Handle shared references |
|
893 |
if ((valueCache != null) && valueCache.containsKey(object)) { |
|
894 |
writeIndirection(INDIRECTION_TAG, valueCache.getVal(object)); |
|
895 |
return; |
|
896 |
} |
|
897 |
||
898 |
boolean oldMustChunk = mustChunk; |
|
899 |
||
900 |
boolean isCustom = false; |
|
901 |
if (factory instanceof ValueHelper) { |
|
902 |
short modifier; |
|
903 |
try { |
|
904 |
modifier = ((ValueHelper)factory).get_type().type_modifier(); |
|
905 |
} catch(BadKind ex) { // tk_value_box |
|
906 |
modifier = VM_NONE.value; |
|
907 |
} |
|
908 |
if (object instanceof CustomMarshal && |
|
909 |
modifier == VM_CUSTOM.value) { |
|
910 |
isCustom = true; |
|
911 |
mustChunk = true; |
|
912 |
} |
|
913 |
if (modifier == VM_TRUNCATABLE.value) |
|
914 |
mustChunk = true; |
|
915 |
} |
|
916 |
||
917 |
if (mustChunk) { |
|
918 |
||
919 |
if (inBlock) |
|
920 |
end_block(); |
|
921 |
||
922 |
// Write value_tag |
|
923 |
int indirection = writeValueTag(true, |
|
924 |
orb.getORBData().useRepId(), |
|
925 |
Util.getCodebase(object.getClass()) |
|
926 |
); |
|
927 |
||
928 |
if (orb.getORBData().useRepId()) { |
|
929 |
write_repositoryId(factory.get_id()); |
|
930 |
} |
|
931 |
||
932 |
// Add indirection for object to indirection table |
|
933 |
updateIndirectionTable(indirection, object, object); |
|
934 |
||
935 |
// Write Value chunk |
|
936 |
start_block(); |
|
937 |
end_flag--; |
|
938 |
chunkedValueNestingLevel--; |
|
939 |
if (isCustom) |
|
940 |
((CustomMarshal)object).marshal(parent); |
|
941 |
else |
|
942 |
factory.write_value(parent, object); |
|
943 |
end_block(); |
|
944 |
||
945 |
// Write end tag |
|
946 |
writeEndTag(true); |
|
947 |
} |
|
948 |
else { |
|
949 |
// Write value_tag |
|
950 |
int indirection = writeValueTag(false, |
|
951 |
orb.getORBData().useRepId(), |
|
952 |
Util.getCodebase(object.getClass()) |
|
953 |
); |
|
954 |
||
955 |
if (orb.getORBData().useRepId()) { |
|
956 |
write_repositoryId(factory.get_id()); |
|
957 |
} |
|
958 |
||
959 |
// Add indirection for object to indirection table |
|
960 |
updateIndirectionTable(indirection, object, object); |
|
961 |
||
962 |
// Write Value chunk |
|
963 |
end_flag--; |
|
964 |
// no need to test for custom on the non-chunked path |
|
965 |
factory.write_value(parent, object); |
|
966 |
||
967 |
// Write end tag |
|
968 |
writeEndTag(false); |
|
969 |
} |
|
970 |
||
971 |
mustChunk = oldMustChunk; |
|
972 |
||
973 |
// Check to see if we need to start another block for a |
|
974 |
// possible outer value |
|
975 |
if (mustChunk) |
|
976 |
start_block(); |
|
977 |
||
978 |
} |
|
979 |
||
980 |
public int get_offset() { |
|
981 |
return bbwi.position(); |
|
982 |
} |
|
983 |
||
984 |
public void start_block() { |
|
985 |
if (debug) { |
|
986 |
dprint("CDROutputStream_1_0 start_block, position" + bbwi.position()); |
|
987 |
} |
|
988 |
||
989 |
//Move inBlock=true to after write_long since write_long might |
|
990 |
//trigger grow which will lead to erroneous behavior with a |
|
991 |
//missing blockSizeIndex. |
|
992 |
//inBlock = true; |
|
993 |
||
994 |
// Save space in the buffer for block size |
|
995 |
write_long(0); |
|
996 |
||
997 |
//Has to happen after write_long since write_long could |
|
998 |
//trigger grow which is overridden by supper classes to |
|
999 |
//depend on inBlock. |
|
1000 |
inBlock = true; |
|
1001 |
||
1002 |
blockSizePosition = get_offset(); |
|
1003 |
||
1004 |
// Remember where to put the size of the endblock less 4 |
|
1005 |
blockSizeIndex = bbwi.position(); |
|
1006 |
||
1007 |
if (debug) { |
|
1008 |
dprint("CDROutputStream_1_0 start_block, blockSizeIndex " |
|
1009 |
+ blockSizeIndex); |
|
1010 |
} |
|
1011 |
||
1012 |
} |
|
1013 |
||
1014 |
// Utility method which will hopefully decrease chunking complexity |
|
1015 |
// by allowing us to end_block and update chunk lengths without |
|
1016 |
// calling alignAndReserve. Otherwise, it's possible to get into |
|
1017 |
// recursive scenarios which lose the chunking state. |
|
1018 |
protected void writeLongWithoutAlign(int x) { |
|
1019 |
if (littleEndian) { |
|
1020 |
writeLittleEndianLong(x); |
|
1021 |
} else { |
|
1022 |
writeBigEndianLong(x); |
|
1023 |
} |
|
1024 |
} |
|
1025 |
||
1026 |
public void end_block() { |
|
1027 |
if (debug) { |
|
1028 |
dprint("CDROutputStream_1_0.java end_block"); |
|
1029 |
} |
|
1030 |
||
1031 |
if (!inBlock) |
|
1032 |
return; |
|
1033 |
||
1034 |
if (debug) { |
|
1035 |
dprint("CDROutputStream_1_0.java end_block, in a block"); |
|
1036 |
} |
|
1037 |
||
1038 |
inBlock = false; |
|
1039 |
||
1040 |
// Test to see if the block was of zero length |
|
1041 |
// If so, remove the block instead of ending it |
|
1042 |
// (This can happen if the last field written |
|
1043 |
// in a value was another value) |
|
1044 |
if (get_offset() == blockSizePosition) { |
|
1045 |
// Need to assert that blockSizeIndex == bbwi.position()? REVISIT |
|
1046 |
||
1047 |
bbwi.position(bbwi.position() - 4); |
|
1048 |
blockSizeIndex = -1; |
|
1049 |
blockSizePosition = -1; |
|
1050 |
return; |
|
1051 |
} |
|
1052 |
||
1053 |
int oldSize = bbwi.position(); |
|
1054 |
bbwi.position(blockSizeIndex - 4); |
|
1055 |
||
1056 |
writeLongWithoutAlign(oldSize - blockSizeIndex); |
|
1057 |
||
1058 |
bbwi.position(oldSize); |
|
1059 |
blockSizeIndex = -1; |
|
1060 |
blockSizePosition = -1; |
|
1061 |
||
1062 |
// System.out.println(" post end_block: " + get_offset() + " " + bbwi.position()); |
|
1063 |
} |
|
1064 |
||
1065 |
public org.omg.CORBA.ORB orb() |
|
1066 |
{ |
|
1067 |
return orb; |
|
1068 |
} |
|
1069 |
||
1070 |
// ------------ End RMI related methods -------------------------- |
|
1071 |
||
1072 |
public final void write_boolean_array(boolean[]value, int offset, int length) { |
|
1073 |
if ( value == null ) |
|
1074 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1075 |
||
1076 |
// This will only have an effect if we're already chunking |
|
1077 |
handleSpecialChunkBegin(length); |
|
1078 |
||
1079 |
for (int i = 0; i < length; i++) |
|
1080 |
write_boolean(value[offset + i]); |
|
1081 |
||
1082 |
// This will only have an effect if we're already chunking |
|
1083 |
handleSpecialChunkEnd(); |
|
1084 |
} |
|
1085 |
||
1086 |
public final void write_char_array(char[]value, int offset, int length) { |
|
1087 |
if ( value == null ) |
|
1088 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1089 |
||
1090 |
// This will only have an effect if we're already chunking |
|
1091 |
handleSpecialChunkBegin(length); |
|
1092 |
||
1093 |
for (int i = 0; i < length; i++) |
|
1094 |
write_char(value[offset + i]); |
|
1095 |
||
1096 |
// This will only have an effect if we're already chunking |
|
1097 |
handleSpecialChunkEnd(); |
|
1098 |
} |
|
1099 |
||
1100 |
public void write_wchar_array(char[]value, int offset, int length) { |
|
1101 |
if ( value == null ) |
|
1102 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1103 |
||
1104 |
// This will only have an effect if we're already chunking |
|
1105 |
handleSpecialChunkBegin(computeAlignment(2) + (length * 2)); |
|
1106 |
||
1107 |
for (int i = 0; i < length; i++) |
|
1108 |
write_wchar(value[offset + i]); |
|
1109 |
||
1110 |
// This will only have an effect if we're already chunking |
|
1111 |
handleSpecialChunkEnd(); |
|
1112 |
} |
|
1113 |
||
1114 |
public final void write_short_array(short[]value, int offset, int length) { |
|
1115 |
if ( value == null ) |
|
1116 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1117 |
||
1118 |
// This will only have an effect if we're already chunking |
|
1119 |
handleSpecialChunkBegin(computeAlignment(2) + (length * 2)); |
|
1120 |
||
1121 |
for (int i = 0; i < length; i++) |
|
1122 |
write_short(value[offset + i]); |
|
1123 |
||
1124 |
// This will only have an effect if we're already chunking |
|
1125 |
handleSpecialChunkEnd(); |
|
1126 |
} |
|
1127 |
||
1128 |
public final void write_ushort_array(short[]value, int offset, int length) { |
|
1129 |
write_short_array(value, offset, length); |
|
1130 |
} |
|
1131 |
||
1132 |
public final void write_long_array(int[]value, int offset, int length) { |
|
1133 |
if ( value == null ) |
|
1134 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1135 |
||
1136 |
// This will only have an effect if we're already chunking |
|
1137 |
handleSpecialChunkBegin(computeAlignment(4) + (length * 4)); |
|
1138 |
||
1139 |
for (int i = 0; i < length; i++) |
|
1140 |
write_long(value[offset + i]); |
|
1141 |
||
1142 |
// This will only have an effect if we're already chunking |
|
1143 |
handleSpecialChunkEnd(); |
|
1144 |
} |
|
1145 |
||
1146 |
public final void write_ulong_array(int[]value, int offset, int length) { |
|
1147 |
write_long_array(value, offset, length); |
|
1148 |
} |
|
1149 |
||
1150 |
public final void write_longlong_array(long[]value, int offset, int length) { |
|
1151 |
if ( value == null ) |
|
1152 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1153 |
||
1154 |
// This will only have an effect if we're already chunking |
|
1155 |
handleSpecialChunkBegin(computeAlignment(8) + (length * 8)); |
|
1156 |
||
1157 |
for (int i = 0; i < length; i++) |
|
1158 |
write_longlong(value[offset + i]); |
|
1159 |
||
1160 |
// This will only have an effect if we're already chunking |
|
1161 |
handleSpecialChunkEnd(); |
|
1162 |
} |
|
1163 |
||
1164 |
public final void write_ulonglong_array(long[]value, int offset, int length) { |
|
1165 |
write_longlong_array(value, offset, length); |
|
1166 |
} |
|
1167 |
||
1168 |
public final void write_float_array(float[]value, int offset, int length) { |
|
1169 |
if ( value == null ) |
|
1170 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1171 |
||
1172 |
// This will only have an effect if we're already chunking |
|
1173 |
handleSpecialChunkBegin(computeAlignment(4) + (length * 4)); |
|
1174 |
||
1175 |
for (int i = 0; i < length; i++) |
|
1176 |
write_float(value[offset + i]); |
|
1177 |
||
1178 |
// This will only have an effect if we're already chunking |
|
1179 |
handleSpecialChunkEnd(); |
|
1180 |
} |
|
1181 |
||
1182 |
public final void write_double_array(double[]value, int offset, int length) { |
|
1183 |
if ( value == null ) |
|
1184 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1185 |
||
1186 |
// This will only have an effect if we're already chunking |
|
1187 |
handleSpecialChunkBegin(computeAlignment(8) + (length * 8)); |
|
1188 |
||
1189 |
for (int i = 0; i < length; i++) |
|
1190 |
write_double(value[offset + i]); |
|
1191 |
||
1192 |
// This will only have an effect if we're already chunking |
|
1193 |
handleSpecialChunkEnd(); |
|
1194 |
} |
|
1195 |
||
1196 |
public void write_string_array(String[] value, int offset, int length) { |
|
1197 |
if ( value == null ) |
|
1198 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1199 |
||
1200 |
for(int i = 0; i < length; i++) |
|
1201 |
write_string(value[offset + i]); |
|
1202 |
} |
|
1203 |
||
1204 |
public void write_wstring_array(String[] value, int offset, int length) { |
|
1205 |
if ( value == null ) |
|
1206 |
throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); |
|
1207 |
||
1208 |
for(int i = 0; i < length; i++) |
|
1209 |
write_wstring(value[offset + i]); |
|
1210 |
} |
|
1211 |
||
1212 |
public final void write_any_array(org.omg.CORBA.Any value[], int offset, int length) |
|
1213 |
{ |
|
1214 |
for(int i = 0; i < length; i++) |
|
1215 |
write_any(value[offset + i]); |
|
1216 |
} |
|
1217 |
||
1218 |
//--------------------------------------------------------------------// |
|
1219 |
// CDROutputStream state management. |
|
1220 |
// |
|
1221 |
||
1222 |
public void writeTo(java.io.OutputStream s) |
|
1223 |
throws java.io.IOException |
|
1224 |
{ |
|
1225 |
byte[] tmpBuf = null; |
|
1226 |
||
1227 |
if (bbwi.byteBuffer.hasArray()) |
|
1228 |
{ |
|
1229 |
tmpBuf = bbwi.byteBuffer.array(); |
|
1230 |
} |
|
1231 |
else |
|
1232 |
{ |
|
1233 |
int size = bbwi.position(); |
|
1234 |
tmpBuf = new byte[size]; |
|
1235 |
// Micro-benchmarks are showing a loop of ByteBuffer.get(int) is |
|
1236 |
// faster than ByteBuffer.get(byte[], offset, length) |
|
1237 |
for (int i = 0; i < size; i++) |
|
1238 |
tmpBuf[i] = bbwi.byteBuffer.get(i); |
|
1239 |
} |
|
1240 |
||
1241 |
s.write(tmpBuf, 0, bbwi.position()); |
|
1242 |
} |
|
1243 |
||
1244 |
public void writeOctetSequenceTo(org.omg.CORBA.portable.OutputStream s) { |
|
1245 |
||
1246 |
byte[] buf = null; |
|
1247 |
||
1248 |
if (bbwi.byteBuffer.hasArray()) |
|
1249 |
{ |
|
1250 |
buf = bbwi.byteBuffer.array(); |
|
1251 |
} |
|
1252 |
else |
|
1253 |
{ |
|
1254 |
int size = bbwi.position(); |
|
1255 |
buf = new byte[size]; |
|
1256 |
// Micro-benchmarks are showing a loop of ByteBuffer.get(int) is |
|
1257 |
// faster than ByteBuffer.get(byte[], offset, length) |
|
1258 |
for (int i = 0; i < size; i++) |
|
1259 |
buf[i] = bbwi.byteBuffer.get(i); |
|
1260 |
} |
|
1261 |
||
1262 |
s.write_long(bbwi.position()); |
|
1263 |
s.write_octet_array(buf, 0, bbwi.position()); |
|
1264 |
||
1265 |
} |
|
1266 |
||
1267 |
public final int getSize() { |
|
1268 |
return bbwi.position(); |
|
1269 |
} |
|
1270 |
||
1271 |
public int getIndex() { |
|
1272 |
return bbwi.position(); |
|
1273 |
} |
|
1274 |
||
1275 |
public boolean isLittleEndian() { |
|
1276 |
return littleEndian; |
|
1277 |
} |
|
1278 |
||
1279 |
public void setIndex(int value) { |
|
1280 |
bbwi.position(value); |
|
1281 |
} |
|
1282 |
||
1283 |
public ByteBufferWithInfo getByteBufferWithInfo() { |
|
1284 |
return bbwi; |
|
1285 |
} |
|
1286 |
||
1287 |
public void setByteBufferWithInfo(ByteBufferWithInfo bbwi) { |
|
1288 |
this.bbwi = bbwi; |
|
1289 |
} |
|
1290 |
||
1291 |
public ByteBuffer getByteBuffer() { |
|
1292 |
ByteBuffer result = null;; |
|
1293 |
if (bbwi != null) { |
|
1294 |
result = bbwi.byteBuffer; |
|
1295 |
} |
|
1296 |
return result; |
|
1297 |
} |
|
1298 |
||
1299 |
public void setByteBuffer(ByteBuffer byteBuffer) { |
|
1300 |
bbwi.byteBuffer = byteBuffer; |
|
1301 |
} |
|
1302 |
||
1303 |
private final void updateIndirectionTable(int indirection, java.lang.Object object, |
|
1304 |
java.lang.Object key) { |
|
1305 |
// int indirection = get_offset(); |
|
1306 |
if (valueCache == null) |
|
1307 |
valueCache = new CacheTable(orb,true); |
|
1308 |
valueCache.put(object, indirection); |
|
1309 |
if (key != object) |
|
1310 |
valueCache.put(key, indirection); |
|
1311 |
} |
|
1312 |
||
1313 |
private final void write_repositoryId(String id) { |
|
1314 |
// Use an indirection if available |
|
1315 |
if (repositoryIdCache != null && repositoryIdCache.containsKey(id)) { |
|
1316 |
writeIndirection(INDIRECTION_TAG, repositoryIdCache.getVal(id)); |
|
1317 |
return; |
|
1318 |
} |
|
1319 |
||
1320 |
// Write it as a string. Note that we have already done the |
|
1321 |
// special case conversion of non-Latin-1 characters to escaped |
|
1322 |
// Latin-1 sequences in RepositoryId. |
|
1323 |
||
1324 |
// It's not a good idea to cache them now that we can have |
|
1325 |
// multiple code sets. |
|
1326 |
int indirection = writeString(id); |
|
1327 |
||
1328 |
// Add indirection for id to indirection table |
|
1329 |
if (repositoryIdCache == null) |
|
1330 |
repositoryIdCache = new CacheTable(orb,true); |
|
1331 |
repositoryIdCache.put(id, indirection); |
|
1332 |
} |
|
1333 |
||
1334 |
private void write_codebase(String str, int pos) { |
|
1335 |
if (codebaseCache != null && codebaseCache.containsKey(str)) { |
|
1336 |
writeIndirection(INDIRECTION_TAG, codebaseCache.getVal(str)); |
|
1337 |
} |
|
1338 |
else { |
|
1339 |
write_string(str); |
|
1340 |
if (codebaseCache == null) |
|
1341 |
codebaseCache = new CacheTable(orb,true); |
|
1342 |
codebaseCache.put(str, pos); |
|
1343 |
} |
|
1344 |
} |
|
1345 |
||
1346 |
private final int writeValueTag(boolean chunkIt, boolean useRepId, |
|
1347 |
String codebase) { |
|
1348 |
int indirection = 0; |
|
1349 |
if (chunkIt && !useRepId){ |
|
1350 |
if (codebase == null) { |
|
1351 |
write_long(repIdUtil.getStandardRMIChunkedNoRepStrId()); |
|
1352 |
indirection = get_offset() - 4; |
|
1353 |
} else { |
|
1354 |
write_long(repIdUtil.getCodeBaseRMIChunkedNoRepStrId()); |
|
1355 |
indirection = get_offset() - 4; |
|
1356 |
write_codebase(codebase, get_offset()); |
|
1357 |
} |
|
1358 |
} else if (chunkIt && useRepId){ |
|
1359 |
if (codebase == null) { |
|
1360 |
write_long(repIdUtil.getStandardRMIChunkedId()); |
|
1361 |
indirection = get_offset() - 4; |
|
1362 |
} else { |
|
1363 |
write_long(repIdUtil.getCodeBaseRMIChunkedId()); |
|
1364 |
indirection = get_offset() - 4; |
|
1365 |
write_codebase(codebase, get_offset()); |
|
1366 |
} |
|
1367 |
} else if (!chunkIt && !useRepId) { |
|
1368 |
if (codebase == null) { |
|
1369 |
write_long(repIdUtil.getStandardRMIUnchunkedNoRepStrId()); |
|
1370 |
indirection = get_offset() - 4; |
|
1371 |
} else { |
|
1372 |
write_long(repIdUtil.getCodeBaseRMIUnchunkedNoRepStrId()); |
|
1373 |
indirection = get_offset() - 4; |
|
1374 |
write_codebase(codebase, get_offset()); |
|
1375 |
} |
|
1376 |
} else if (!chunkIt && useRepId) { |
|
1377 |
if (codebase == null) { |
|
1378 |
write_long(repIdUtil.getStandardRMIUnchunkedId()); |
|
1379 |
indirection = get_offset() - 4; |
|
1380 |
} else { |
|
1381 |
write_long(repIdUtil.getCodeBaseRMIUnchunkedId()); |
|
1382 |
indirection = get_offset() - 4; |
|
1383 |
write_codebase(codebase, get_offset()); |
|
1384 |
} |
|
1385 |
} |
|
1386 |
return indirection; |
|
1387 |
} |
|
1388 |
||
1389 |
private void writeIDLValue(Serializable object, String repID) |
|
1390 |
{ |
|
1391 |
if (object instanceof StreamableValue) { |
|
1392 |
((StreamableValue)object)._write(parent); |
|
1393 |
||
1394 |
} else if (object instanceof CustomValue) { |
|
1395 |
((CustomValue)object).marshal(parent); |
|
1396 |
||
1397 |
} else { |
|
1398 |
BoxedValueHelper helper = Utility.getHelper(object.getClass(), null, repID); |
|
1399 |
boolean isCustom = false; |
|
1400 |
if (helper instanceof ValueHelper && object instanceof CustomMarshal) { |
|
1401 |
try { |
|
1402 |
if (((ValueHelper)helper).get_type().type_modifier() == VM_CUSTOM.value) |
|
1403 |
isCustom = true; |
|
1404 |
} catch(BadKind ex) { |
|
1405 |
throw wrapper.badTypecodeForCustomValue( CompletionStatus.COMPLETED_MAYBE, |
|
1406 |
ex ) ; |
|
1407 |
} |
|
1408 |
} |
|
1409 |
if (isCustom) |
|
1410 |
((CustomMarshal)object).marshal(parent); |
|
1411 |
else |
|
1412 |
helper.write_value(parent, object); |
|
1413 |
} |
|
1414 |
} |
|
1415 |
||
1416 |
// Handles end tag compaction... |
|
1417 |
private void writeEndTag(boolean chunked){ |
|
1418 |
||
1419 |
if (chunked) { |
|
1420 |
if (get_offset() == end_flag_position) { |
|
1421 |
||
1422 |
if (bbwi.position() == end_flag_index) { |
|
1423 |
||
1424 |
// We are exactly at the same position and index as the |
|
1425 |
// end of the last end tag. Thus, we can back up over it |
|
1426 |
// and compact the tags. |
|
1427 |
bbwi.position(bbwi.position() - 4); |
|
1428 |
||
1429 |
} else { |
|
1430 |
||
1431 |
// Special case in which we're at the beginning of a new |
|
1432 |
// fragment, but the position is the same. We can't back up, |
|
1433 |
// so we just write the new end tag without compaction. This |
|
1434 |
// occurs when a value ends and calls start_block to open a |
|
1435 |
// continuation chunk, but it's called at the very end of |
|
1436 |
// a fragment. |
|
1437 |
} |
|
1438 |
} |
|
1439 |
||
1440 |
writeNestingLevel(); |
|
1441 |
||
1442 |
// Remember the last index and position. These are only used when chunking. |
|
1443 |
end_flag_index = bbwi.position(); |
|
1444 |
end_flag_position = get_offset(); |
|
1445 |
||
1446 |
chunkedValueNestingLevel++; |
|
1447 |
} |
|
1448 |
||
1449 |
// Increment the nesting level |
|
1450 |
end_flag++; |
|
1451 |
} |
|
1452 |
||
1453 |
/** |
|
1454 |
* Handles ORB versioning of the end tag. Should only |
|
1455 |
* be called if chunking. |
|
1456 |
* |
|
1457 |
* If talking to our older ORBs (Standard Extension, |
|
1458 |
* Kestrel, and Ladybird), write the end flag that takes |
|
1459 |
* into account all enclosing valuetypes. |
|
1460 |
* |
|
1461 |
* If talking a newer or foreign ORB, or if the orb |
|
1462 |
* instance is null, write the end flag that only takes |
|
1463 |
* into account the enclosing chunked valuetypes. |
|
1464 |
*/ |
|
1465 |
private void writeNestingLevel() { |
|
1466 |
if (orb == null || |
|
1467 |
ORBVersionFactory.getFOREIGN().equals(orb.getORBVersion()) || |
|
1468 |
ORBVersionFactory.getNEWER().compareTo(orb.getORBVersion()) <= 0) { |
|
1469 |
||
1470 |
write_long(chunkedValueNestingLevel); |
|
1471 |
||
1472 |
} else { |
|
1473 |
write_long(end_flag); |
|
1474 |
} |
|
1475 |
} |
|
1476 |
||
1477 |
private void writeClass(String repository_id, Class clz) { |
|
1478 |
||
1479 |
if (repository_id == null) |
|
1480 |
repository_id = repIdStrs.getClassDescValueRepId(); |
|
1481 |
||
1482 |
// Write value_tag |
|
1483 |
int indirection = writeValueTag(mustChunk, true, null); |
|
1484 |
updateIndirectionTable(indirection, clz, clz); |
|
1485 |
||
1486 |
write_repositoryId(repository_id); |
|
1487 |
||
1488 |
if (mustChunk) { |
|
1489 |
// Write Value chunk |
|
1490 |
start_block(); |
|
1491 |
end_flag--; |
|
1492 |
chunkedValueNestingLevel--; |
|
1493 |
} else |
|
1494 |
end_flag--; |
|
1495 |
||
1496 |
writeClassBody(clz); |
|
1497 |
||
1498 |
if (mustChunk) |
|
1499 |
end_block(); |
|
1500 |
||
1501 |
// Write end tag |
|
1502 |
writeEndTag(mustChunk); |
|
1503 |
} |
|
1504 |
||
1505 |
// Pre-Merlin/J2EE 1.3 ORBs wrote the repository ID |
|
1506 |
// and codebase strings in the wrong order. This handles |
|
1507 |
// backwards compatibility. |
|
1508 |
private void writeClassBody(Class clz) { |
|
1509 |
if (orb == null || |
|
1510 |
ORBVersionFactory.getFOREIGN().equals(orb.getORBVersion()) || |
|
1511 |
ORBVersionFactory.getNEWER().compareTo(orb.getORBVersion()) <= 0) { |
|
1512 |
||
1513 |
write_value(Util.getCodebase(clz)); |
|
1514 |
write_value(repIdStrs.createForAnyType(clz)); |
|
1515 |
} else { |
|
1516 |
||
1517 |
write_value(repIdStrs.createForAnyType(clz)); |
|
1518 |
write_value(Util.getCodebase(clz)); |
|
1519 |
} |
|
1520 |
} |
|
1521 |
||
1522 |
// Casts and returns an Object as a Serializable |
|
1523 |
// This is required for JDK 1.1 only to avoid VerifyErrors when |
|
1524 |
// passing arrays as Serializable |
|
1525 |
// private java.io.Serializable make_serializable(java.lang.Object object) |
|
1526 |
// { |
|
1527 |
// return (java.io.Serializable)object; |
|
1528 |
// } |
|
1529 |
||
1530 |
private boolean shouldWriteAsIDLEntity(Serializable object) |
|
1531 |
{ |
|
1532 |
return ((object instanceof IDLEntity) && (!(object instanceof ValueBase)) && |
|
1533 |
(!(object instanceof org.omg.CORBA.Object))); |
|
1534 |
||
1535 |
} |
|
1536 |
||
1537 |
private void writeIDLEntity(IDLEntity object) { |
|
1538 |
||
1539 |
// _REVISIT_ could check to see whether chunking really needed |
|
1540 |
mustChunk = true; |
|
1541 |
||
1542 |
String repository_id = repIdStrs.createForJavaType(object); |
|
1543 |
Class clazz = object.getClass(); |
|
1544 |
String codebase = Util.getCodebase(clazz); |
|
1545 |
||
1546 |
// Write value_tag |
|
1547 |
int indirection = writeValueTag(true, true, codebase); |
|
1548 |
updateIndirectionTable(indirection, object, object); |
|
1549 |
||
1550 |
// Write rep. id |
|
1551 |
write_repositoryId(repository_id); |
|
1552 |
||
1553 |
// Write Value chunk |
|
1554 |
end_flag--; |
|
1555 |
chunkedValueNestingLevel--; |
|
1556 |
start_block(); |
|
1557 |
||
1558 |
// Write the IDLEntity using reflection |
|
1559 |
try { |
|
1560 |
ClassLoader clazzLoader = (clazz == null ? null : clazz.getClassLoader()); |
|
1561 |
final Class helperClass = Utility.loadClassForClass(clazz.getName()+"Helper", codebase, |
|
1562 |
clazzLoader, clazz, clazzLoader); |
|
1563 |
final Class argTypes[] = {org.omg.CORBA.portable.OutputStream.class, clazz}; |
|
1564 |
// getDeclaredMethod requires RuntimePermission accessDeclaredMembers |
|
1565 |
// if a different class loader is used (even though the javadoc says otherwise) |
|
1566 |
Method writeMethod = null; |
|
1567 |
try { |
|
1568 |
writeMethod = (Method)AccessController.doPrivileged( |
|
1569 |
new PrivilegedExceptionAction() { |
|
1570 |
public java.lang.Object run() throws NoSuchMethodException { |
|
1571 |
return helperClass.getDeclaredMethod(kWriteMethod, argTypes); |
|
1572 |
} |
|
1573 |
} |
|
1574 |
); |
|
1575 |
} catch (PrivilegedActionException pae) { |
|
1576 |
// this gets caught below |
|
1577 |
throw (NoSuchMethodException)pae.getException(); |
|
1578 |
} |
|
1579 |
java.lang.Object args[] = {parent, object}; |
|
1580 |
writeMethod.invoke(null, args); |
|
1581 |
} catch (ClassNotFoundException cnfe) { |
|
1582 |
throw wrapper.errorInvokingHelperWrite( CompletionStatus.COMPLETED_MAYBE, cnfe ) ; |
|
1583 |
} catch(NoSuchMethodException nsme) { |
|
1584 |
throw wrapper.errorInvokingHelperWrite( CompletionStatus.COMPLETED_MAYBE, nsme ) ; |
|
1585 |
} catch(IllegalAccessException iae) { |
|
1586 |
throw wrapper.errorInvokingHelperWrite( CompletionStatus.COMPLETED_MAYBE, iae ) ; |
|
1587 |
} catch(InvocationTargetException ite) { |
|
1588 |
throw wrapper.errorInvokingHelperWrite( CompletionStatus.COMPLETED_MAYBE, ite ) ; |
|
1589 |
} |
|
1590 |
end_block(); |
|
1591 |
||
1592 |
// Write end tag |
|
1593 |
writeEndTag(true); |
|
1594 |
} |
|
1595 |
||
1596 |
/* DataOutputStream methods */ |
|
1597 |
||
1598 |
public void write_Abstract (java.lang.Object value) { |
|
1599 |
write_abstract_interface(value); |
|
1600 |
} |
|
1601 |
||
1602 |
public void write_Value (java.io.Serializable value) { |
|
1603 |
write_value(value); |
|
1604 |
} |
|
1605 |
||
1606 |
// This will stay a custom add-on until the java-rtf issue is resolved. |
|
1607 |
// Then it should be declared in org.omg.CORBA.portable.OutputStream. |
|
1608 |
// |
|
1609 |
// Pads the string representation of bigDecimal with zeros to fit the given |
|
1610 |
// digits and scale before it gets written to the stream. |
|
1611 |
public void write_fixed(java.math.BigDecimal bigDecimal, short digits, short scale) { |
|
1612 |
String string = bigDecimal.toString(); |
|
1613 |
String integerPart; |
|
1614 |
String fractionPart; |
|
1615 |
StringBuffer stringBuffer; |
|
1616 |
||
1617 |
// Get rid of the sign |
|
1618 |
if (string.charAt(0) == '-' || string.charAt(0) == '+') { |
|
1619 |
string = string.substring(1); |
|
1620 |
} |
|
1621 |
||
1622 |
// Determine integer and fraction parts |
|
1623 |
int dotIndex = string.indexOf('.'); |
|
1624 |
if (dotIndex == -1) { |
|
1625 |
integerPart = string; |
|
1626 |
fractionPart = null; |
|
1627 |
} else if (dotIndex == 0 ) { |
|
1628 |
integerPart = null; |
|
1629 |
fractionPart = string; |
|
1630 |
} else { |
|
1631 |
integerPart = string.substring(0, dotIndex); |
|
1632 |
fractionPart = string.substring(dotIndex + 1); |
|
1633 |
} |
|
1634 |
||
1635 |
// Pad both parts with zeros as necessary |
|
1636 |
stringBuffer = new StringBuffer(digits); |
|
1637 |
if (fractionPart != null) { |
|
1638 |
stringBuffer.append(fractionPart); |
|
1639 |
} |
|
1640 |
while (stringBuffer.length() < scale) { |
|
1641 |
stringBuffer.append('0'); |
|
1642 |
} |
|
1643 |
if (integerPart != null) { |
|
1644 |
stringBuffer.insert(0, integerPart); |
|
1645 |
} |
|
1646 |
while (stringBuffer.length() < digits) { |
|
1647 |
stringBuffer.insert(0, '0'); |
|
1648 |
} |
|
1649 |
||
1650 |
// This string contains no sign or dot |
|
1651 |
this.write_fixed(stringBuffer.toString(), bigDecimal.signum()); |
|
1652 |
} |
|
1653 |
||
1654 |
// This method should be remove by the java-rtf issue. |
|
1655 |
// Right now the scale and digits information of the type code is lost. |
|
1656 |
public void write_fixed(java.math.BigDecimal bigDecimal) { |
|
1657 |
// This string might contain sign and/or dot |
|
1658 |
this.write_fixed(bigDecimal.toString(), bigDecimal.signum()); |
|
1659 |
} |
|
1660 |
||
1661 |
// The string may contain a sign and dot |
|
1662 |
public void write_fixed(String string, int signum) { |
|
1663 |
int stringLength = string.length(); |
|
1664 |
// Each octet contains (up to) two decimal digits |
|
1665 |
byte doubleDigit = 0; |
|
1666 |
char ch; |
|
1667 |
byte digit; |
|
1668 |
||
1669 |
// First calculate the length of the string without optional sign and dot |
|
1670 |
int numDigits = 0; |
|
1671 |
for (int i=0; i<stringLength; i++) { |
|
1672 |
ch = string.charAt(i); |
|
1673 |
if (ch == '-' || ch == '+' || ch == '.') |
|
1674 |
continue; |
|
1675 |
numDigits++; |
|
1676 |
} |
|
1677 |
for (int i=0; i<stringLength; i++) { |
|
1678 |
ch = string.charAt(i); |
|
1679 |
if (ch == '-' || ch == '+' || ch == '.') |
|
1680 |
continue; |
|
1681 |
digit = (byte)Character.digit(ch, 10); |
|
1682 |
if (digit == -1) { |
|
1683 |
throw wrapper.badDigitInFixed( CompletionStatus.COMPLETED_MAYBE ) ; |
|
1684 |
} |
|
1685 |
// If the fixed type has an odd number of decimal digits, |
|
1686 |
// then the representation begins with the first (most significant) digit. |
|
1687 |
// Otherwise, this first half-octet is all zero, and the first digit |
|
1688 |
// is in the second half-octet. |
|
1689 |
if (numDigits % 2 == 0) { |
|
1690 |
doubleDigit |= digit; |
|
1691 |
this.write_octet(doubleDigit); |
|
1692 |
doubleDigit = 0; |
|
1693 |
} else { |
|
1694 |
doubleDigit |= (digit << 4); |
|
1695 |
} |
|
1696 |
numDigits--; |
|
1697 |
} |
|
1698 |
// The sign configuration, in the last half-octet of the representation, |
|
1699 |
// is 0xD for negative numbers and 0xC for positive and zero values |
|
1700 |
if (signum == -1) { |
|
1701 |
doubleDigit |= 0xd; |
|
1702 |
} else { |
|
1703 |
doubleDigit |= 0xc; |
|
1704 |
} |
|
1705 |
this.write_octet(doubleDigit); |
|
1706 |
} |
|
1707 |
||
1708 |
private final static String _id = "IDL:omg.org/CORBA/DataOutputStream:1.0"; |
|
1709 |
private final static String[] _ids = { _id }; |
|
1710 |
||
1711 |
public String[] _truncatable_ids() { |
|
1712 |
if (_ids == null) |
|
1713 |
return null; |
|
1714 |
||
1715 |
return (String[])_ids.clone(); |
|
1716 |
} |
|
1717 |
||
1718 |
/* for debugging */ |
|
1719 |
||
1720 |
public void printBuffer() { |
|
1721 |
CDROutputStream_1_0.printBuffer(this.bbwi); |
|
1722 |
} |
|
1723 |
||
1724 |
public static void printBuffer(ByteBufferWithInfo bbwi) { |
|
1725 |
||
1726 |
System.out.println("+++++++ Output Buffer ++++++++"); |
|
1727 |
System.out.println(); |
|
1728 |
System.out.println("Current position: " + bbwi.position()); |
|
1729 |
System.out.println("Total length : " + bbwi.buflen); |
|
1730 |
System.out.println(); |
|
1731 |
||
1732 |
char[] charBuf = new char[16]; |
|
1733 |
||
1734 |
try { |
|
1735 |
||
1736 |
for (int i = 0; i < bbwi.position(); i += 16) { |
|
1737 |
||
1738 |
int j = 0; |
|
1739 |
||
1740 |
// For every 16 bytes, there is one line |
|
1741 |
// of output. First, the hex output of |
|
1742 |
// the 16 bytes with each byte separated |
|
1743 |
// by a space. |
|
1744 |
while (j < 16 && j + i < bbwi.position()) { |
|
1745 |
int k = bbwi.byteBuffer.get(i + j); |
|
1746 |
if (k < 0) |
|
1747 |
k = 256 + k; |
|
1748 |
String hex = Integer.toHexString(k); |
|
1749 |
if (hex.length() == 1) |
|
1750 |
hex = "0" + hex; |
|
1751 |
System.out.print(hex + " "); |
|
1752 |
j++; |
|
1753 |
} |
|
1754 |
||
1755 |
// Add any extra spaces to align the |
|
1756 |
// text column in case we didn't end |
|
1757 |
// at 16 |
|
1758 |
while (j < 16) { |
|
1759 |
System.out.print(" "); |
|
1760 |
j++; |
|
1761 |
} |
|
1762 |
||
1763 |
// Now output the ASCII equivalents. Non-ASCII |
|
1764 |
// characters are shown as periods. |
|
1765 |
int x = 0; |
|
1766 |
||
1767 |
while (x < 16 && x + i < bbwi.position()) { |
|
1768 |
if (ORBUtility.isPrintable((char)bbwi.byteBuffer.get(i + x))) |
|
1769 |
charBuf[x] = (char)bbwi.byteBuffer.get(i + x); |
|
1770 |
else |
|
1771 |
charBuf[x] = '.'; |
|
1772 |
x++; |
|
1773 |
} |
|
1774 |
System.out.println(new String(charBuf, 0, x)); |
|
1775 |
} |
|
1776 |
} catch (Throwable t) { |
|
1777 |
t.printStackTrace(); |
|
1778 |
} |
|
1779 |
System.out.println("++++++++++++++++++++++++++++++"); |
|
1780 |
} |
|
1781 |
||
1782 |
public void writeIndirection(int tag, int posIndirectedTo) |
|
1783 |
{ |
|
1784 |
// Must ensure that there are no chunks between the tag |
|
1785 |
// and the actual indirection value. This isn't talked about |
|
1786 |
// in the spec, but seems to cause headaches in our code. |
|
1787 |
// At the very least, this method isolates the indirection code |
|
1788 |
// that was duplicated so often. |
|
1789 |
||
1790 |
handleSpecialChunkBegin(computeAlignment(4) + 8); |
|
1791 |
||
1792 |
// write indirection tag |
|
1793 |
write_long(tag); |
|
1794 |
||
1795 |
// write indirection |
|
1796 |
// Use parent.getRealIndex() so that it can be overridden by TypeCodeOutputStreams |
|
1797 |
/* |
|
1798 |
System.out.println("CDROutputStream_1_0 writing indirection pos " + posIndirectedTo + |
|
1799 |
" - real index " + parent.getRealIndex(get_offset()) + " = " + |
|
1800 |
(posIndirectedTo - parent.getRealIndex(get_offset()))); |
|
1801 |
*/ |
|
1802 |
write_long(posIndirectedTo - parent.getRealIndex(get_offset())); |
|
1803 |
||
1804 |
handleSpecialChunkEnd(); |
|
1805 |
} |
|
1806 |
||
1807 |
protected CodeSetConversion.CTBConverter getCharConverter() { |
|
1808 |
if (charConverter == null) |
|
1809 |
charConverter = parent.createCharCTBConverter(); |
|
1810 |
||
1811 |
return charConverter; |
|
1812 |
} |
|
1813 |
||
1814 |
protected CodeSetConversion.CTBConverter getWCharConverter() { |
|
1815 |
if (wcharConverter == null) |
|
1816 |
wcharConverter = parent.createWCharCTBConverter(); |
|
1817 |
||
1818 |
return wcharConverter; |
|
1819 |
} |
|
1820 |
||
1821 |
protected void dprint(String msg) { |
|
1822 |
if (debug) |
|
1823 |
ORBUtility.dprint(this, msg); |
|
1824 |
} |
|
1825 |
||
1826 |
void alignOnBoundary(int octetBoundary) { |
|
1827 |
alignAndReserve(octetBoundary, 0); |
|
1828 |
} |
|
1829 |
||
1830 |
public void start_value(String rep_id) { |
|
1831 |
||
1832 |
if (debug) { |
|
1833 |
dprint("start_value w/ rep id " |
|
1834 |
+ rep_id |
|
1835 |
+ " called at pos " |
|
1836 |
+ get_offset() |
|
1837 |
+ " position " |
|
1838 |
+ bbwi.position()); |
|
1839 |
} |
|
1840 |
||
1841 |
if (inBlock) |
|
1842 |
end_block(); |
|
1843 |
||
1844 |
// Write value_tag |
|
1845 |
writeValueTag(true, true, null); |
|
1846 |
||
1847 |
// Write rep. id |
|
1848 |
write_repositoryId(rep_id); |
|
1849 |
||
1850 |
// Write Value chunk |
|
1851 |
end_flag--; |
|
1852 |
chunkedValueNestingLevel--; |
|
1853 |
||
1854 |
// Make sure to chunk the custom data |
|
1855 |
start_block(); |
|
1856 |
} |
|
1857 |
||
1858 |
public void end_value() { |
|
1859 |
||
1860 |
if (debug) { |
|
1861 |
dprint("end_value called at pos " |
|
1862 |
+ get_offset() |
|
1863 |
+ " position " |
|
1864 |
+ bbwi.position()); |
|
1865 |
} |
|
1866 |
||
1867 |
end_block(); |
|
1868 |
||
1869 |
writeEndTag(true); |
|
1870 |
||
1871 |
// Check to see if we need to start another block for a |
|
1872 |
// possible outer value. Since we're in the stream |
|
1873 |
// format 2 custom type contained by another custom |
|
1874 |
// type, mustChunk should always be true. |
|
1875 |
// |
|
1876 |
// Here's why we need to open a continuation chunk: |
|
1877 |
// |
|
1878 |
// We need to enclose the default data of the |
|
1879 |
// next subclass down in chunks. There won't be |
|
1880 |
// an end tag separating the superclass optional |
|
1881 |
// data and the subclass's default data. |
|
1882 |
||
1883 |
if (debug) { |
|
1884 |
dprint("mustChunk is " + mustChunk); |
|
1885 |
} |
|
1886 |
||
1887 |
if (mustChunk) { |
|
1888 |
start_block(); |
|
1889 |
} |
|
1890 |
} |
|
1891 |
||
1892 |
public void close() throws IOException |
|
1893 |
{ |
|
1894 |
// tell BufferManagerWrite to release any ByteBuffers |
|
1895 |
getBufferManager().close(); |
|
1896 |
||
1897 |
// It's possible bbwi.byteBuffer is shared between |
|
1898 |
// this OutputStream and an InputStream. Thus, we check |
|
1899 |
// if the Input/Output streams are using the same ByteBuffer. |
|
1900 |
// If they sharing the same ByteBuffer we need to ensure only |
|
1901 |
// one of those ByteBuffers are released to the ByteBufferPool. |
|
1902 |
||
1903 |
if (getByteBufferWithInfo() != null && getByteBuffer() != null) |
|
1904 |
{ |
|
1905 |
MessageMediator messageMediator = parent.getMessageMediator(); |
|
1906 |
if (messageMediator != null) |
|
1907 |
{ |
|
1908 |
CDRInputObject inputObj = |
|
1909 |
(CDRInputObject)messageMediator.getInputObject(); |
|
1910 |
if (inputObj != null) |
|
1911 |
{ |
|
11044
758078ec0816
7091388: Regular unexplained npe's from corba libs after system has been running for days
coffeys
parents:
5555
diff
changeset
|
1912 |
if (inputObj.isSharing(getByteBuffer())) |
4 | 1913 |
{ |
11044
758078ec0816
7091388: Regular unexplained npe's from corba libs after system has been running for days
coffeys
parents:
5555
diff
changeset
|
1914 |
// Set InputStream's ByteBuffer and bbwi to null |
758078ec0816
7091388: Regular unexplained npe's from corba libs after system has been running for days
coffeys
parents:
5555
diff
changeset
|
1915 |
// so its ByteBuffer cannot be released to the pool |
758078ec0816
7091388: Regular unexplained npe's from corba libs after system has been running for days
coffeys
parents:
5555
diff
changeset
|
1916 |
inputObj.setByteBuffer(null); |
758078ec0816
7091388: Regular unexplained npe's from corba libs after system has been running for days
coffeys
parents:
5555
diff
changeset
|
1917 |
inputObj.setByteBufferWithInfo(null); |
4 | 1918 |
} |
1919 |
} |
|
1920 |
} |
|
1921 |
||
1922 |
// release this stream's ByteBuffer to the pool |
|
1923 |
ByteBufferPool byteBufferPool = orb.getByteBufferPool(); |
|
1924 |
if (debug) |
|
1925 |
{ |
|
1926 |
// print address of ByteBuffer being released |
|
1927 |
int bbAddress = System.identityHashCode(bbwi.byteBuffer); |
|
1928 |
StringBuffer sb = new StringBuffer(80); |
|
1929 |
sb.append(".close - releasing ByteBuffer id ("); |
|
1930 |
sb.append(bbAddress).append(") to ByteBufferPool."); |
|
1931 |
String msg = sb.toString(); |
|
1932 |
dprint(msg); |
|
1933 |
} |
|
1934 |
byteBufferPool.releaseByteBuffer(getByteBuffer()); |
|
1935 |
bbwi.byteBuffer = null; |
|
1936 |
bbwi = null; |
|
1937 |
} |
|
1938 |
} |
|
1939 |
} |