KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > CdrOutputStream


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 import org.apache.geronimo.interop.SystemException;
21 import org.apache.geronimo.interop.rmi.RmiTrace;
22 import org.apache.geronimo.interop.util.ArrayUtil;
23 import org.apache.geronimo.interop.util.BigEndian;
24 import org.apache.geronimo.interop.util.ExceptionUtil;
25 import org.apache.geronimo.interop.util.JavaClass;
26 import org.apache.geronimo.interop.util.UTF8;
27 import org.omg.CORBA.TCKind JavaDoc;
28 import org.omg.GIOP.LocateReplyHeader_1_2;
29 import org.omg.GIOP.LocateReplyHeader_1_2Helper;
30 import org.omg.GIOP.MessageHeader_1_1;
31 import org.omg.GIOP.MessageHeader_1_1Helper;
32 import org.omg.GIOP.MsgType_1_1;
33 import org.omg.GIOP.ReplyHeader_1_0;
34 import org.omg.GIOP.ReplyHeader_1_0Helper;
35 import org.omg.GIOP.ReplyHeader_1_2;
36 import org.omg.GIOP.ReplyHeader_1_2Helper;
37 import org.omg.GIOP.RequestHeader_1_0;
38 import org.omg.GIOP.RequestHeader_1_0Helper;
39 import org.omg.GIOP.RequestHeader_1_1;
40 import org.omg.GIOP.RequestHeader_1_1Helper;
41 import org.omg.GIOP.RequestHeader_1_2;
42 import org.omg.GIOP.RequestHeader_1_2Helper;
43 import org.omg.GIOP.Version;
44 import org.omg.IOP.IOR JavaDoc;
45 import org.omg.IOP.IORHelper JavaDoc;
46 import org.omg.IOP.ServiceContext JavaDoc;
47 import org.omg.IOP.TaggedProfile JavaDoc;
48
49 /**
50  ** CORBA 2.3 / GIOP 1.2 CDR OutputStream.
51  **/

52 public class CdrOutputStream extends org.omg.CORBA_2_3.portable.OutputStream JavaDoc
53 {
54     private final String JavaDoc CRLF = "\r\n";
55
56     public static CdrOutputStream getInstance()
57     {
58         CdrOutputStream output = new CdrOutputStream();
59         output.init(new byte[DEFAULT_BUFFER_LENGTH], 0);
60         return output;
61     }
62
63     public static CdrOutputStream getInstance(byte[] buffer)
64     {
65         CdrOutputStream output = new CdrOutputStream();
66         output.init(buffer, 0);
67         return output;
68     }
69
70     public static CdrOutputStream getInstance(byte[] buffer, int offset)
71     {
72         CdrOutputStream output = new CdrOutputStream();
73         output.init(buffer, offset);
74         return output;
75     }
76
77     public static CdrOutputStream getInstanceForEncapsulation()
78     {
79         CdrOutputStream output = getInstance();
80         output.write_boolean(false); // byte order: big endian
81
return output;
82     }
83
84     public static CdrOutputStream getPooledInstance()
85     {
86         CdrOutputStream output = null;
87         if (output == null)
88         {
89             output = getInstance();
90         }
91         return output;
92     }
93
94     // -----------------------------------------------------------------------
95
// private data
96
// -----------------------------------------------------------------------
97

98     private static final int DEFAULT_BUFFER_LENGTH = 64;
99
100     private static final int MAXIMUM_POOLED_BUFFER_LENGTH = 1024;
101
102     private static final boolean RMI_TRACE = true;
103
104     private static IOR JavaDoc NULL_IOR = new IOR JavaDoc("", new TaggedProfile JavaDoc[0]);
105
106     private static char[] GIOP_MAGIC = { 'G', 'I', 'O', 'P' };
107
108     private static ServiceContext JavaDoc[] EMPTY_SERVICE_CONTEXT_LIST = {};
109
110     private static Version GIOP_VERSION_1_0 = new Version((byte)1, (byte)0);
111     private static Version GIOP_VERSION_1_1 = new Version((byte)1, (byte)1);
112     private static Version GIOP_VERSION_1_2 = new Version((byte)1, (byte)2);
113
114     private int _giopVersion = GiopVersion.VERSION_1_2;
115
116     private boolean _unaligned;
117
118     private byte[] _pooledBuffer;
119
120     // -----------------------------------------------------------------------
121
// package-private data
122
// -----------------------------------------------------------------------
123

124     byte[] _buffer;
125
126     int _offset;
127
128     int _length;
129
130     // -----------------------------------------------------------------------
131
// public methods
132
// -----------------------------------------------------------------------
133

134     public void init(byte[] buffer, int offset)
135     {
136         _buffer = buffer;
137         _offset = offset;
138         _length = _buffer.length;
139     }
140
141     public void recycle()
142     {
143         reset();
144     }
145
146     public void reset()
147     {
148         _offset = 0;
149         if (_buffer.length > MAXIMUM_POOLED_BUFFER_LENGTH)
150         {
151             _buffer = _pooledBuffer;
152             _pooledBuffer = null;
153             if (_buffer == null)
154             {
155                 _buffer = new byte[DEFAULT_BUFFER_LENGTH];
156             }
157         }
158         _length = _buffer.length;
159     }
160
161     public void setUnaligned()
162     {
163         _unaligned = true;
164     }
165
166     public byte[] getBytes()
167     {
168         int n = _offset;
169         byte[] bytes = new byte[n];
170         System.arraycopy(_buffer, 0, bytes, 0, n);
171         return bytes;
172     }
173
174     public byte[] getBuffer()
175     {
176         return _buffer;
177     }
178
179     public int getOffset()
180     {
181         return _offset;
182     }
183
184     public int getLength()
185     {
186         return _length;
187     }
188
189     public byte[] getEncapsulation()
190     {
191         byte[] data = new byte[_offset];
192         System.arraycopy(_buffer, 0, data, 0, _offset);
193         return data;
194     }
195
196     public void setGiopVersion(int version)
197     {
198         _giopVersion = version;
199     }
200
201     /**
202      ** Align the buffer offset so the next item is written at an offset
203      ** aligned according to <code>alignment</code>, which must be a
204      ** power of 2 (and at least = 1).
205      ** <p>The padding bytes are set to zero, to prevent the
206      ** security problems inherent in uninitialised padding bytes.
207      ** <p>Then we check if there is enough space left in the buffer for
208      ** an item of <code>size</code> bytes; if not, we expand the buffer
209      ** to make space.
210      **/

211     public final void write_align(int alignment, int size)
212     {
213         if (_unaligned)
214         {
215             alignment = 1;
216         }
217         int needLength = _offset + alignment + size;
218         if (needLength > _length)
219         {
220             // We need to increase the buffer size. We allow for a bit
221
// of future expansion (if possible).
222
int factor8 = 32;
223             for (int pass = 1; pass <= 7; pass++, factor8 /= 2)
224             {
225                 // We try factors 5, 3, 2, 1.5, 1.25, 1.125, 1.
226
try
227                 {
228                     byte[] newBuffer = new byte[needLength + factor8 * needLength / 8];
229                     // Copy old buffer contents into new buffer.
230
System.arraycopy(_buffer, 0, newBuffer, 0, _offset);
231                     pool(_buffer);
232                     _buffer = newBuffer;
233                     _length = newBuffer.length;
234                     break;
235                 }
236                 catch (OutOfMemoryError JavaDoc ignore)
237                 {
238                     if (pass == 7)
239                     {
240                         throw new org.omg.CORBA.NO_MEMORY JavaDoc(needLength + " bytes");
241                     }
242                 }
243             }
244         }
245         int mask = alignment - 1;
246         for (int i = (alignment - (_offset & mask)) & mask; i > 0; i--)
247         {
248             _buffer[_offset++] = 0;
249         }
250     }
251
252     /**
253      ** Convenience method needed in many places.
254      **/

255     public void write_octet_sequence(byte[] bytes)
256     {
257         if (bytes == null)
258         {
259             bytes = ArrayUtil.EMPTY_BYTE_ARRAY;
260         }
261         int n = bytes.length;
262         write_long(n);
263         write_octet_array(bytes, 0, n);
264     }
265
266     public void write_message(int messageType)
267     {
268         MessageHeader_1_1 header = new MessageHeader_1_1();
269         header.magic = GIOP_MAGIC;
270         switch (_giopVersion)
271         {
272           case GiopVersion.VERSION_1_0:
273             header.GIOP_version = GIOP_VERSION_1_2;
274             break;
275           case GiopVersion.VERSION_1_1:
276             header.GIOP_version = GIOP_VERSION_1_1;
277             break;
278           case GiopVersion.VERSION_1_2:
279             header.GIOP_version = GIOP_VERSION_1_2;
280             break;
281           default:
282             throw new IllegalStateException JavaDoc();
283         }
284         header.flags = 0;
285         header.message_type = (byte)messageType;
286         header.message_size = 0;
287         // header.message_size is rewritten later
288
MessageHeader_1_1Helper.write(this, header);
289     }
290
291     public void write_message_size()
292     {
293         int messageSize = _offset - 12;
294         int saveOffset = _offset;
295         _offset = 8;
296         write_long(messageSize);
297         _offset = saveOffset;
298     }
299
300     public void write_request(RequestHeader_1_2 request, CdrOutputStream parameters)
301     {
302         if (request.service_context == null)
303         {
304             // Avoid allocation of empty array by Helper.
305
request.service_context = EMPTY_SERVICE_CONTEXT_LIST;
306         }
307         write_message(MsgType_1_1._Request);
308         switch (_giopVersion)
309         {
310           case GiopVersion.VERSION_1_0:
311             RequestHeader_1_0 req10 = new RequestHeader_1_0();
312             req10.service_context = request.service_context;
313             req10.request_id = request.request_id;
314             req10.response_expected = (request.response_flags & 1) != 0;
315             req10.operation = request.operation;
316             req10.object_key = request.target.object_key();
317             RequestHeader_1_0Helper.write(this, req10);
318             break;
319           case GiopVersion.VERSION_1_1:
320             RequestHeader_1_1 req11 = new RequestHeader_1_1();
321             req11.service_context = request.service_context;
322             req11.request_id = request.request_id;
323             req11.response_expected = (request.response_flags & 1) != 0;
324             req11.operation = request.operation;
325             req11.object_key = request.target.object_key();
326             RequestHeader_1_1Helper.write(this, req11);
327             break;
328           case GiopVersion.VERSION_1_2:
329             RequestHeader_1_2Helper.write(this, request);
330             break;
331           default:
332             throw new IllegalStateException JavaDoc();
333         }
334         byte[] parametersBuffer = parameters.getBuffer();
335         int parametersLength = parameters.getOffset();
336         if (parametersLength > 0)
337         {
338             if (_giopVersion >= GiopVersion.VERSION_1_2)
339             {
340                 write_align(8, 0); // parameters are 8-byte aligned
341
}
342             else
343             {
344                 // TODO: should have padded service context earlier
345
}
346             write_octet_array(parametersBuffer, 0, parametersLength);
347         }
348         write_message_size();
349     }
350
351     public void write_reply(ReplyHeader_1_2 reply, CdrOutputStream results)
352     {
353         if (reply.service_context == null)
354         {
355             // Avoid allocation of empty array by Helper.
356
reply.service_context = EMPTY_SERVICE_CONTEXT_LIST;
357         }
358         write_message(MsgType_1_1._Reply);
359         switch (_giopVersion)
360         {
361           case GiopVersion.VERSION_1_0:
362           case GiopVersion.VERSION_1_1:
363             ReplyHeader_1_0 rep10 = new ReplyHeader_1_0();
364             rep10.service_context = reply.service_context;
365             rep10.request_id = reply.request_id;
366             rep10.reply_status = reply.reply_status;
367             ReplyHeader_1_0Helper.write(this, rep10);
368             break;
369           case GiopVersion.VERSION_1_2:
370             ReplyHeader_1_2Helper.write(this, reply);
371             break;
372           default:
373             throw new IllegalStateException JavaDoc();
374         }
375         byte[] resultsBuffer = results.getBuffer();
376         int resultsLength = results.getOffset();
377         if (resultsLength > 0)
378         {
379             if (_giopVersion >= GiopVersion.VERSION_1_2)
380             {
381                 write_align(8, 0); // results are 8-byte aligned
382
}
383             else
384             {
385                 // TODO: should have padded service context earlier
386
}
387             write_octet_array(resultsBuffer, 0, resultsLength);
388         }
389         write_message_size();
390     }
391
392     public void write_reply(LocateReplyHeader_1_2 reply)
393     {
394         write_message(MsgType_1_1._LocateReply);
395         LocateReplyHeader_1_2Helper.write(this, reply);
396         write_message_size();
397     }
398
399     public void write_SystemException(Exception JavaDoc ex, boolean withStackTrace)
400     {
401         String JavaDoc type = "UNKNOWN";
402         if (ex instanceof org.omg.CORBA.SystemException JavaDoc)
403         {
404             type = JavaClass.getNameSuffix(ex.getClass().getName());
405         }
406         else if (ex instanceof UnsupportedOperationException JavaDoc)
407         {
408             type = "BAD_OPERATION";
409         }
410         else if (ex instanceof SecurityException JavaDoc)
411         {
412             type = "NO_PERMISSION";
413         }
414         else if (ex instanceof java.rmi.NoSuchObjectException JavaDoc)
415         {
416             type = "OBJECT_NOT_EXIST";
417         }
418         //else if (ex instanceof org.apache.geronimo.interop.transaction.TransactionRolledbackSystemException)
419
//{
420
// type = "TRANSACTION_ROLLEDBACK";
421
//}
422
String JavaDoc id = "IDL:omg.org/CORBA/" + type + ":1.0";
423         write_string(id);
424         write_long(0); // minor (TODO: other values?)
425
write_long(0); // completed (TODO: other values?)
426
if (withStackTrace)
427         {
428             write_string(ExceptionUtil.getStackTrace(ex));
429         }
430     }
431
432     public void send_http_response(java.io.OutputStream JavaDoc output, String JavaDoc url)
433     {
434         StringBuffer JavaDoc respHeader = new StringBuffer JavaDoc(256);
435
436         respHeader.append("HTTP/1.1 200 OK\r\n");
437         respHeader.append("Content-Length: ");
438         respHeader.append(java.lang.Integer.toString(_offset));
439         respHeader.append(CRLF + CRLF);
440
441         byte[] header = null;
442         try
443         {
444             String JavaDoc h = respHeader.toString();
445             header = h.getBytes("ASCII");
446         }
447         catch(Exception JavaDoc e)
448         {
449             throw new SystemException(org.apache.geronimo.interop.util.ExceptionUtil.causedBy(e));
450         }
451
452         byte[] data = new byte[header.length + _offset];
453         System.arraycopy(header, 0, data, 0, header.length);
454         System.arraycopy(_buffer, 0, data, header.length, _offset);
455
456         if (RMI_TRACE)
457         {
458             RmiTrace.send(url, data);
459         }
460         try
461         {
462             output.write(data, 0, data.length);
463             output.flush();
464         }
465         catch (java.io.IOException JavaDoc ex)
466         {
467             throw new org.omg.CORBA.COMM_FAILURE JavaDoc(ex.toString());
468         }
469     }
470
471     /**
472      * We support only POST message
473      */

474     public void send_http_request(java.io.OutputStream JavaDoc output, String JavaDoc url, String JavaDoc httpHeaders)
475     {
476         StringBuffer JavaDoc hdr = new StringBuffer JavaDoc(512);
477         hdr.append("POST /host/port/HIOP/2.0 HTTP/1.1\r\n");
478         hdr.append("Content-Length: ");
479         hdr.append(java.lang.Integer.toString(_offset));
480         hdr.append(CRLF);
481         hdr.append(httpHeaders);
482         hdr.append(CRLF);
483
484         byte[] header = null;
485         try
486         {
487             header = hdr.toString().getBytes("ASCII");
488         }
489         catch(Exception JavaDoc e)
490         {
491             throw new SystemException(org.apache.geronimo.interop.util.ExceptionUtil.causedBy(e));
492         }
493
494         byte[] data = new byte[header.length + _offset];
495         System.arraycopy(header, 0, data, 0, header.length);
496         System.arraycopy(_buffer, 0, data, header.length, _offset);
497
498         if (RMI_TRACE)
499         {
500             RmiTrace.send(url, data);
501         }
502         try
503         {
504             output.write(data, 0, data.length);
505             output.flush();
506         }
507         catch (java.io.IOException JavaDoc ex)
508         {
509             throw new org.omg.CORBA.COMM_FAILURE JavaDoc(ex.toString());
510         }
511     }
512
513     public void send_message(java.io.OutputStream JavaDoc output, String JavaDoc url)
514     {
515         if (RMI_TRACE)
516         {
517             byte[] data = new byte[_offset];
518             System.arraycopy(_buffer, 0, data, 0, _offset);
519             RmiTrace.send(url, data);
520         }
521         try
522         {
523             output.write(_buffer, 0, _offset);
524             output.flush();
525         }
526         catch (java.io.IOException JavaDoc ex)
527         {
528             throw new org.omg.CORBA.COMM_FAILURE JavaDoc(ex.toString());
529         }
530     }
531
532     // -----------------------------------------------------------------------
533
// public methods from org.omg.CORBA.portable.OutputStream
534
// -----------------------------------------------------------------------
535

536     public void write_boolean(boolean value)
537     {
538         write_align(1, 1);
539         if (value)
540         {
541             _buffer[_offset++] = 1;
542         }
543         else
544         {
545             _buffer[_offset++] = 0;
546         }
547     }
548
549     public void write_char(char value)
550     {
551         write_align(1, 1);
552         if ((int)value > 255)
553         {
554             throw new org.omg.CORBA.MARSHAL JavaDoc("write_char: value = " + value);
555         }
556         _buffer[_offset++] = (byte)value;
557     }
558
559     public void write_wchar(char value)
560     {
561         write_octet((byte)2); // size of wchar is 2 bytes
562
write_align(1, 2);
563         write_ushort_no_align_big_endian((int)value);
564     }
565
566     public void write_octet(byte value)
567     {
568         write_align(1, 1);
569         _buffer[_offset++] = value;
570     }
571
572     public void write_short(short value)
573     {
574         write_align(2, 2);
575         int oldOffset = _offset;
576         _offset += 2;
577         BigEndian.setShort(_buffer, oldOffset, value);
578     }
579
580     public void write_ushort(short value)
581     {
582         write_short(value);
583     }
584
585     public void write_long(int value)
586     {
587         write_align(4, 4);
588         int oldOffset = _offset;
589         _offset += 4;
590         BigEndian.setInt(_buffer, oldOffset, value);
591     }
592
593     public void write_ulong(int value)
594     {
595         write_long(value);
596     }
597
598     public void write_longlong(long value)
599     {
600         write_align(8, 8);
601         int oldOffset = _offset;
602         _offset += 8;
603         BigEndian.setLong(_buffer, oldOffset, value);
604     }
605
606     public void write_ulonglong(long value)
607     {
608         write_longlong(value);
609     }
610
611     public void write_float(float value)
612     {
613         write_long(Float.floatToIntBits(value));
614     }
615
616     public void write_double(double value)
617     {
618         write_longlong(Double.doubleToLongBits(value));
619     }
620
621     public void write_string(String JavaDoc value)
622     {
623         if (value == null)
624         {
625             value = "";
626         }
627         write_align(4, 4);
628         int size = UTF8.fromString(value, _buffer, _offset + 4, _length - 1);
629         if (size == -1)
630         {
631             // No room to convert in-place, ok to allocate new byte array.
632
byte[] bytes = UTF8.fromString(value);
633             size = bytes.length;
634             write_ulong(size + 1);
635             write_octet_array(bytes, 0, size);
636         }
637         else
638         {
639             // Already converted already into _buffer.
640
write_ulong(size + 1);
641             _offset += size;
642         }
643         write_octet((byte)0);
644     }
645
646     public void write_wstring(String JavaDoc value)
647     {
648         if (value == null)
649         {
650             value = "";
651         }
652         int size = value.length();
653         int numBytes = 2 * size;
654         write_ulong(numBytes); // No terminating NUL
655
write_align(1, numBytes);
656         for (int i = 0; i < size; i++)
657         {
658             char c = value.charAt(i);
659             BigEndian.setShort(_buffer, _offset, (short)c);
660             _offset += 2;
661         }
662     }
663
664     public void write_boolean_array(boolean[] value, int offset, int length)
665     {
666         for (int i = 0; i < length; i++)
667         {
668             write_boolean(value[offset + i]);
669         }
670     }
671
672     public void write_char_array(char[] value, int offset, int length)
673     {
674         for (int i = 0; i < length; i++)
675         {
676             write_char(value[offset + i]);
677         }
678     }
679
680     public void write_wchar_array(char[] value, int offset, int length)
681     {
682         for (int i = 0; i < length; i++)
683         {
684             write_wchar(value[offset + i]);
685         }
686     }
687
688     public void write_octet_array(byte[] value, int offset, int length)
689     {
690         write_align(1, length);
691         System.arraycopy(value, offset, _buffer, _offset, length);
692         _offset += length;
693     }
694
695     public void write_short_array(short[] value, int offset, int length)
696     {
697         for (int i = 0; i < length; i++)
698         {
699             write_short(value[offset + i]);
700         }
701     }
702
703     public void write_ushort_array(short[] value, int offset, int length)
704     {
705         for (int i = 0; i < length; i++)
706         {
707             write_ushort(value[offset + i]);
708         }
709     }
710
711     public void write_long_array(int[] value, int offset, int length)
712     {
713         for (int i = 0; i < length; i++)
714         {
715             write_long(value[offset + i]);
716         }
717     }
718
719     public void write_ulong_array(int[] value, int offset, int length)
720     {
721         for (int i = 0; i < length; i++)
722         {
723             write_ulong(value[offset + i]);
724         }
725     }
726
727     public void write_longlong_array(long[] value, int offset, int length)
728     {
729         for (int i = 0; i < length; i++)
730         {
731             write_longlong(value[offset + i]);
732         }
733     }
734
735     public void write_ulonglong_array(long[] value, int offset, int length)
736     {
737         for (int i = 0; i < length; i++)
738         {
739             write_ulonglong(value[offset + i]);
740         }
741     }
742
743     public void write_float_array(float[] value, int offset, int length)
744     {
745         for (int i = 0; i < length; i++)
746         {
747             write_float(value[offset + i]);
748         }
749     }
750
751     public void write_double_array(double[] value, int offset, int length)
752     {
753         for (int i = 0; i < length; i++)
754         {
755             write_double(value[offset + i]);
756         }
757     }
758
759     public void write_Object(org.omg.CORBA.Object JavaDoc value)
760     {
761         if (value == null)
762         {
763             write_IOR(null);
764         }
765         else if (value instanceof ObjectRef)
766         {
767             ObjectRef ref = (ObjectRef)value;
768             IOR JavaDoc ior = ref.$getIOR();
769             write_IOR(ior);
770         }
771         else
772         {
773             throw new org.omg.CORBA.MARSHAL JavaDoc(value.getClass().getName());
774         }
775     }
776
777     public void write_TypeCode(org.omg.CORBA.TypeCode JavaDoc tc)
778     {
779         write_TypeCode(tc, new java.util.HashMap JavaDoc());
780     }
781
782     public void write_Any(org.omg.CORBA.Any JavaDoc value)
783     {
784         org.omg.CORBA.TypeCode JavaDoc tc = value.type();
785         write_TypeCode(tc);
786         write_Any(value.create_input_stream(), tc);
787     }
788
789     public void write_Any(org.omg.CORBA.portable.InputStream JavaDoc is, org.omg.CORBA.TypeCode JavaDoc tc)
790     {
791         try
792         {
793             int tk = tc.kind().value();
794             switch (tk)
795             {
796                 case TCKind._tk_null:
797                 case TCKind._tk_void:
798                     break;
799                 case TCKind._tk_TypeCode:
800                     write_TypeCode(is.read_TypeCode());
801                     break;
802                 case TCKind._tk_any:
803                     write_Any(is.read_any());
804                     break;
805                 case TCKind._tk_boolean:
806                     write_boolean(is.read_boolean());
807                     break;
808                 case TCKind._tk_char:
809                     write_char(is.read_char());
810                     break;
811                 case TCKind._tk_wchar:
812                     write_wchar(is.read_wchar());
813                     break;
814                 case TCKind._tk_octet:
815                     write_octet(is.read_octet());
816                     break;
817                 case TCKind._tk_short:
818                     write_short(is.read_short());
819                     break;
820                 case TCKind._tk_ushort:
821                     write_ushort(is.read_ushort());
822                     break;
823                 case TCKind._tk_long:
824                     write_long(is.read_long());
825                     break;
826                 case TCKind._tk_ulong:
827                 case TCKind._tk_enum:
828                     write_ulong(is.read_ulong());
829                     break;
830                 case TCKind._tk_longlong:
831                     write_longlong(is.read_longlong());
832                     break;
833                 case TCKind._tk_ulonglong:
834                     write_ulonglong(is.read_ulonglong());
835                     break;
836                 case TCKind._tk_float:
837                     write_float(is.read_float());
838                     break;
839                 case TCKind._tk_double:
840                     write_double(is.read_double());
841                     break;
842                 case TCKind._tk_string:
843                     write_string(is.read_string());
844                     break;
845                 case TCKind._tk_wstring:
846                     write_wstring(is.read_wstring());
847                     break;
848                 case TCKind._tk_objref:
849                     write_Object(is.read_Object());
850                     break;
851                 case TCKind._tk_alias:
852                     write_Any(is, tc.content_type());
853                     break;
854                 case TCKind._tk_array:
855                     {
856                         int n = tc.length();
857                         org.omg.CORBA.TypeCode JavaDoc c = tc.content_type();
858                         for (int i = 0; i < n; i++)
859                         {
860                             write_Any(is, c);
861                         }
862                     }
863                     break;
864                 case TCKind._tk_sequence:
865                     {
866                         int n = is.read_ulong();
867                         write_ulong(n);
868                         org.omg.CORBA.TypeCode JavaDoc c = tc.content_type();
869                         for (int i = 0; i < n; i++)
870                         {
871                             write_Any(is, c);
872                         }
873                     }
874                     break;
875                 case TCKind._tk_struct:
876                 case TCKind._tk_except:
877                     {
878                         int n = tc.member_count();
879                         for (int i = 0; i < n; i++)
880                         {
881                             write_Any(is, tc.member_type(i));
882                         }
883                     }
884                     break;
885                 case TCKind._tk_union:
886                     {
887                         org.omg.CORBA.TypeCode JavaDoc dt = tc.discriminator_type();
888                         org.omg.CORBA.Any JavaDoc disc = read_disc(is, dt);
889                         write_Any(disc.create_input_stream(), dt);
890                         int di = tc.default_index();
891                         int i, n = tc.member_count();
892                         for (i = 0; i < n; i++)
893                         {
894                             org.omg.CORBA.Any JavaDoc label = tc.member_label(i);
895                             if (label.equal(disc))
896                             {
897                                 write_Any(is, tc.member_type(i));
898                             }
899                         }
900                         if (i == n && di >= 0)
901                         {
902                             write_Any(is, tc.member_type(di));
903                         }
904                     }
905                     break;
906                 case TCKind._tk_fixed: // TODO
907
case TCKind._tk_value: // TODO
908
default:
909                     throw new org.omg.CORBA.MARSHAL JavaDoc("write_Any: type = " + tc);
910             }
911         }
912         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
913         {
914             throw new org.omg.CORBA.MARSHAL JavaDoc("write_Any: " + ex.toString());
915         }
916         catch (org.omg.CORBA.TypeCodePackage.Bounds JavaDoc ex)
917         {
918             throw new org.omg.CORBA.MARSHAL JavaDoc("write_Any: " + ex.toString());
919         }
920     }
921
922     public void write_any(org.omg.CORBA.Any JavaDoc value) {
923         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_any: NOT IMPLMENTED");
924     }
925
926     public void write_Principal(org.omg.CORBA.Principal JavaDoc value) {
927         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_Principal: NOT IMPLMENTED");
928     }
929
930     public void write(int value) throws java.io.IOException JavaDoc {
931         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write: NOT IMPLMENTED");
932     }
933
934     public void write_fixed(java.math.BigDecimal JavaDoc value) {
935         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_fixed: NOT IMPLMENTED");
936     }
937
938     public void write_Context(org.omg.CORBA.Context JavaDoc context, org.omg.CORBA.ContextList JavaDoc list) {
939         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_Context: NOT IMPLMENTED");
940     }
941
942     public org.omg.CORBA.ORB JavaDoc orb() {
943         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("orb: NOT IMPLMENTED");
944     }
945
946
947     // -----------------------------------------------------------------------
948
// public methods from org.omg.CORBA_2_3.portable.OutputStream
949
// -----------------------------------------------------------------------
950

951     public void write_value(java.io.Serializable JavaDoc value) {
952         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_value: NOT IMPLMENTED");
953     }
954
955     public void write_value(java.io.Serializable JavaDoc value, Class JavaDoc _class) {
956         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_value: NOT IMPLMENTED");
957     }
958
959     public void write_value(java.io.Serializable JavaDoc value, String JavaDoc id) {
960         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_value: NOT IMPLMENTED");
961     }
962
963     public void write_value(java.io.Serializable JavaDoc value, org.omg.CORBA.portable.BoxedValueHelper JavaDoc helper) {
964         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_value: NOT IMPLMENTED");
965     }
966
967     public void write_abstract_interface(java.lang.Object JavaDoc value) {
968         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("write_abstract_interface: NOT IMPLMENTED");
969     }
970
971
972     // doing this specifically to handle Any. This implementation
973
// could be worng but will work for us
974
public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
975     {
976         CdrInputStream is = CdrInputStream.getInstance();
977         is._buffer = new byte[_buffer.length];
978         System.arraycopy(_buffer,0,is._buffer,0,_buffer.length);
979         is._length = _buffer.length;
980         is._offset = 0;
981         return is;
982     }
983
984     // -----------------------------------------------------------------------
985
// protected methods
986
// -----------------------------------------------------------------------
987

988     protected void pool(byte[] oldBuffer)
989     {
990         if (oldBuffer.length <= MAXIMUM_POOLED_BUFFER_LENGTH)
991         {
992             _pooledBuffer = oldBuffer;
993         }
994     }
995
996     protected final void write_ushort_no_align_big_endian(int value)
997     {
998         int oldOffset = _offset;
999         _offset += 2;
1000        BigEndian.setShort(_buffer, oldOffset, (short)value);
1001    }
1002
1003    protected void write_IOR(IOR JavaDoc ior)
1004    {
1005        if (ior == null)
1006        {
1007            ior = NULL_IOR;
1008        }
1009        IORHelper.write(this, ior);
1010    }
1011
1012    public int begin()
1013    {
1014        write_ulong(0);
1015        int saveOffset = _offset;
1016        write_boolean(false);
1017        return saveOffset;
1018    }
1019
1020    public void end(int saveOffset)
1021    {
1022        int endOffset = _offset;
1023        _offset = saveOffset - 4;
1024        write_ulong(endOffset - saveOffset);
1025        _offset = endOffset;
1026    }
1027
1028    private void write_TypeCode(org.omg.CORBA.TypeCode JavaDoc tc, java.util.HashMap JavaDoc table)
1029    {
1030        try
1031        {
1032            int tk = tc.kind().value();
1033            // Check if we need to write an indirection
1034
switch (tk)
1035            {
1036                case TCKind._tk_struct:
1037                case TCKind._tk_union:
1038                case TCKind._tk_value:
1039                    String JavaDoc id = tc.id();
1040                    if (! id.equals(""))
1041                    {
1042                        Integer JavaDoc key = (Integer JavaDoc)table.get(id);
1043                        if (key != null)
1044                        {
1045                            write_ulong(0xffffffff);
1046                            write_long(key.intValue() - _offset);
1047                            return;
1048                        }
1049                        table.put(id, new Integer JavaDoc(_offset));
1050                    }
1051            }
1052            write_ulong(tk);
1053            switch (tk)
1054            {
1055                case TCKind._tk_null:
1056                case TCKind._tk_void:
1057                case TCKind._tk_TypeCode:
1058                case TCKind._tk_any:
1059                case TCKind._tk_boolean:
1060                case TCKind._tk_char:
1061                case TCKind._tk_wchar:
1062                case TCKind._tk_octet:
1063                case TCKind._tk_short:
1064                case TCKind._tk_ushort:
1065                case TCKind._tk_long:
1066                case TCKind._tk_ulong:
1067                case TCKind._tk_longlong:
1068                case TCKind._tk_ulonglong:
1069                case TCKind._tk_float:
1070                case TCKind._tk_double:
1071                case TCKind._tk_longdouble:
1072                    // All these require only a TCKind.
1073
break;
1074                case TCKind._tk_fixed:
1075                    write_ushort(tc.fixed_digits());
1076                    write_short(tc.fixed_scale());
1077                    break;
1078                case TCKind._tk_string:
1079                case TCKind._tk_wstring:
1080                    write_ulong(tc.length());
1081                    break;
1082                case TCKind._tk_objref:
1083                case TCKind._tk_native:
1084                case TCKind._tk_abstract_interface:
1085                    {
1086                        int saveOffset = begin();
1087                        write_string(tc.id());
1088                        write_string(tc.name());
1089                        end(saveOffset);
1090                    }
1091                    break;
1092                case TCKind._tk_alias:
1093                case TCKind._tk_value_box:
1094                    {
1095                        int saveOffset = begin();
1096                        write_string(tc.id());
1097                        write_string(tc.name());
1098                        write_TypeCode(tc.content_type(), table);
1099                        end(saveOffset);
1100                    }
1101                    break;
1102                case TCKind._tk_sequence:
1103                case TCKind._tk_array:
1104                    {
1105                        int saveOffset = begin();
1106                        write_TypeCode(tc.content_type(), table);
1107                        write_ulong(tc.length());
1108                        end(saveOffset);
1109                    }
1110                    break;
1111                case TCKind._tk_enum:
1112                    {
1113                        int saveOffset = begin();
1114                        write_string(tc.id());
1115                        write_string(tc.name());
1116                        int count = tc.member_count();
1117                        write_ulong(count);
1118                        for (int i = 0; i < count; i++)
1119                        {
1120                            write_string(tc.member_name(i));
1121                        }
1122                        end(saveOffset);
1123                    }
1124                    break;
1125                case TCKind._tk_struct:
1126                case TCKind._tk_except:
1127                    {
1128                        int saveOffset = begin();
1129                        write_string(tc.id());
1130                        write_string(tc.name());
1131                        int count = tc.member_count();
1132                        write_ulong(count);
1133                        for (int i = 0; i < count; i++)
1134                        {
1135                            write_string(tc.member_name(i));
1136                            write_TypeCode(tc.member_type(i), table);
1137                        }
1138                        end(saveOffset);
1139                    }
1140                    break;
1141                case TCKind._tk_union:
1142                    {
1143                        int saveOffset = begin();
1144                        write_string(tc.id());
1145                        write_string(tc.name());
1146                        org.omg.CORBA.TypeCode JavaDoc dt = tc.discriminator_type();
1147                        write_TypeCode(dt, table);
1148                        int di = tc.default_index();
1149                        write_ulong(di);
1150                        int count = tc.member_count();
1151                        write_ulong(count);
1152                        for (int i = 0; i < count; i++)
1153                        {
1154                            write_Any(tc.member_label(i).create_input_stream(), dt);
1155                            write_string(tc.member_name(i));
1156                            write_TypeCode(tc.member_type(i), table);
1157                        }
1158                        end(saveOffset);
1159                    }
1160                    break;
1161                case TCKind._tk_value:
1162                    {
1163                        int saveOffset = begin();
1164                        write_string(tc.id());
1165                        write_string(tc.name());
1166                        write_short(tc.type_modifier());
1167                        write_TypeCode(tc.concrete_base_type(), table);
1168                        int count = tc.member_count();
1169                        write_ulong(count);
1170                        for (int i = 0; i < count; i++)
1171                        {
1172                            write_string(tc.member_name(i));
1173                            write_TypeCode(tc.member_type(i), table);
1174                            write_short(tc.member_visibility(i));
1175                        }
1176                        end(saveOffset);
1177                    }
1178                    break;
1179                default:
1180                    throw new org.omg.CORBA.MARSHAL JavaDoc("write_TypeCode: kind = " + tk);
1181            }
1182        }
1183        catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
1184        {
1185            throw new org.omg.CORBA.MARSHAL JavaDoc(ex.toString());
1186        }
1187        catch (org.omg.CORBA.TypeCodePackage.Bounds JavaDoc ex)
1188        {
1189            throw new org.omg.CORBA.MARSHAL JavaDoc(ex.toString());
1190        }
1191    }
1192
1193    private org.omg.CORBA.Any JavaDoc read_disc(org.omg.CORBA.portable.InputStream JavaDoc is, org.omg.CORBA.TypeCode JavaDoc dt)
1194    {
1195        int tk = dt.kind().value();
1196        if (tk == TCKind._tk_alias)
1197        {
1198            try
1199            {
1200                return read_disc(is, dt.content_type());
1201            }
1202            catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
1203            {
1204                throw new org.omg.CORBA.MARSHAL JavaDoc("read_disc: " + ex.toString());
1205            }
1206        }
1207        org.omg.CORBA.Any JavaDoc disc = new org.apache.geronimo.interop.rmi.iiop.Any();
1208        switch (tk)
1209        {
1210            case TCKind._tk_boolean:
1211                disc.insert_boolean(is.read_boolean());
1212                break;
1213            case TCKind._tk_octet:
1214                disc.insert_octet(is.read_octet());
1215                break;
1216            case TCKind._tk_short:
1217                disc.insert_short(is.read_short());
1218                break;
1219            case TCKind._tk_ushort:
1220                disc.insert_ushort(is.read_ushort());
1221                break;
1222            case TCKind._tk_long:
1223                disc.insert_long(is.read_long());
1224                break;
1225            case TCKind._tk_ulong:
1226            case TCKind._tk_enum:
1227                disc.insert_ulong(is.read_ulong());
1228                break;
1229            case TCKind._tk_longlong:
1230                disc.insert_longlong(is.read_longlong());
1231                break;
1232            case TCKind._tk_ulonglong:
1233                disc.insert_ulonglong(is.read_ulonglong());
1234                break;
1235            default:
1236                throw new org.omg.CORBA.MARSHAL JavaDoc("read_disc: type = " + dt);
1237        }
1238        return disc;
1239    }
1240}
1241
Popular Tags