KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > CdrOutputStream


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop;
20
21 import gcc.*;
22 import gcc.rmi.*;
23 import gcc.util.*;
24 import gcc.org.omg.IOP.*;
25 import gcc.org.omg.GIOP.*;
26 import org.omg.CORBA.TCKind;
27 import java.io.*;
28
29 /**
30  ** CORBA 2.3 / GIOP 1.2 CDR OutputStream.
31  **/

32 public class CdrOutputStream extends org.omg.CORBA_2_3.portable.OutputStream
33 {
34     public static CdrOutputStream getInstance()
35     {
36         CdrOutputStream output = new CdrOutputStream();
37         output.init(new byte[DEFAULT_BUFFER_LENGTH], 0);
38         return output;
39     }
40
41     public static CdrOutputStream getInstance(byte[] buffer)
42     {
43         CdrOutputStream output = new CdrOutputStream();
44         output.init(buffer, 0);
45         return output;
46     }
47
48     public static CdrOutputStream getInstance(byte[] buffer, int offset)
49     {
50         CdrOutputStream output = new CdrOutputStream();
51         output.init(buffer, offset);
52         return output;
53     }
54
55     public static CdrOutputStream getInstanceForEncapsulation()
56     {
57         CdrOutputStream output = getInstance();
58         output.write_boolean(false); // byte order: big endian
59
return output;
60     }
61
62     public static CdrOutputStream getPooledInstance()
63     {
64         CdrOutputStream output = null; //(CdrOutputStream)_pool.get();
65
if (output == null)
66         {
67             output = getInstance();
68         }
69         return output;
70     }
71
72     // -----------------------------------------------------------------------
73
// private data
74
// -----------------------------------------------------------------------
75

76     private static final int DEFAULT_BUFFER_LENGTH = 64;
77
78     private static final int MAXIMUM_POOLED_BUFFER_LENGTH = 1024;
79
80     private static final boolean RMI_TRACE = true; //SystemProperties.rmiTrace();
81

82     private static IOR NULL_IOR = new IOR("", new TaggedProfile[0]);
83
84     private static char[] GIOP_MAGIC = { 'G', 'I', 'O', 'P' };
85
86     private static ServiceContext[] EMPTY_SERVICE_CONTEXT_LIST = {};
87
88     private static Version GIOP_VERSION_1_0 = new Version((byte)1, (byte)0);
89     private static Version GIOP_VERSION_1_1 = new Version((byte)1, (byte)1);
90     private static Version GIOP_VERSION_1_2 = new Version((byte)1, (byte)2);
91
92     //private static ThreadLocalInstancePool _pool = new ThreadLocalInstancePool(CdrOutputStream.class.getName());
93

94     private int _giopVersion = GiopVersion.VERSION_1_2;
95
96     private boolean _unaligned;
97
98     private byte[] _pooledBuffer;
99
100     // -----------------------------------------------------------------------
101
// package-private data
102
// -----------------------------------------------------------------------
103

104     byte[] _buffer;
105
106     int _offset;
107
108     int _length;
109
110     // -----------------------------------------------------------------------
111
// public methods
112
// -----------------------------------------------------------------------
113

114     public void init(byte[] buffer, int offset)
115     {
116         _buffer = buffer;
117         _offset = offset;
118         _length = _buffer.length;
119     }
120
121     public void recycle()
122     {
123         reset();
124         //_pool.put(this);
125
}
126
127     public void reset()
128     {
129         _offset = 0;
130         if (_buffer.length > MAXIMUM_POOLED_BUFFER_LENGTH)
131         {
132             _buffer = _pooledBuffer;
133             _pooledBuffer = null;
134             if (_buffer == null)
135             {
136                 _buffer = new byte[DEFAULT_BUFFER_LENGTH];
137             }
138         }
139         _length = _buffer.length;
140     }
141
142     public void setUnaligned()
143     {
144         _unaligned = true;
145     }
146
147     public byte[] getBytes()
148     {
149         int n = _offset;
150         byte[] bytes = new byte[n];
151         System.arraycopy(_buffer, 0, bytes, 0, n);
152         return bytes;
153     }
154
155     public byte[] getBuffer()
156     {
157         return _buffer;
158     }
159
160     public int getOffset()
161     {
162         return _offset;
163     }
164
165     public int getLength()
166     {
167         return _length;
168     }
169
170     public byte[] getEncapsulation()
171     {
172         byte[] data = new byte[_offset];
173         System.arraycopy(_buffer, 0, data, 0, _offset);
174         return data;
175     }
176
177     public void setGiopVersion(int version)
178     {
179         _giopVersion = version;
180     }
181
182     /**
183      ** Align the buffer offset so the next item is written at an offset
184      ** aligned according to <code>alignment</code>, which must be a
185      ** power of 2 (and at least = 1).
186      ** <p>The padding bytes are set to zero, to prevent the
187      ** security problems inherent in uninitialised padding bytes.
188      ** <p>Then we check if there is enough space left in the buffer for
189      ** an item of <code>size</code> bytes; if not, we expand the buffer
190      ** to make space.
191      **/

192     public final void write_align(int alignment, int size)
193     {
194         if (_unaligned)
195         {
196             alignment = 1;
197         }
198         int needLength = _offset + alignment + size;
199         if (needLength > _length)
200         {
201             // We need to increase the buffer size. We allow for a bit
202
// of future expansion (if possible).
203
int factor8 = 32;
204             for (int pass = 1; pass <= 7; pass++, factor8 /= 2)
205             {
206                 // We try factors 5, 3, 2, 1.5, 1.25, 1.125, 1.
207
try
208                 {
209                     byte[] newBuffer = new byte[needLength + factor8 * needLength / 8];
210                     // Copy old buffer contents into new buffer.
211
System.arraycopy(_buffer, 0, newBuffer, 0, _offset);
212                     pool(_buffer);
213                     _buffer = newBuffer;
214                     _length = newBuffer.length;
215                     break;
216                 }
217                 catch (OutOfMemoryError ignore)
218                 {
219                     if (pass == 7)
220                     {
221                         throw new org.omg.CORBA.NO_MEMORY(needLength + " bytes");
222                     }
223                 }
224             }
225         }
226         int mask = alignment - 1;
227         for (int i = (alignment - (_offset & mask)) & mask; i > 0; i--)
228         {
229             _buffer[_offset++] = 0;
230         }
231     }
232
233     /**
234      ** Convenience method needed in many places.
235      **/

236     public void write_octet_sequence(byte[] bytes)
237     {
238         if (bytes == null)
239         {
240             bytes = ArrayUtil.EMPTY_BYTE_ARRAY;
241         }
242         int n = bytes.length;
243         write_long(n);
244         write_octet_array(bytes, 0, n);
245     }
246
247     public void write_message(int messageType)
248     {
249         MessageHeader_1_1 header = new MessageHeader_1_1();
250         header.magic = GIOP_MAGIC;
251         switch (_giopVersion)
252         {
253           case GiopVersion.VERSION_1_0:
254             header.GIOP_version = GIOP_VERSION_1_2;
255             break;
256           case GiopVersion.VERSION_1_1:
257             header.GIOP_version = GIOP_VERSION_1_1;
258             break;
259           case GiopVersion.VERSION_1_2:
260             header.GIOP_version = GIOP_VERSION_1_2;
261             break;
262           default:
263             throw new IllegalStateException();
264         }
265         header.flags = 0;
266         header.message_type = (byte)messageType;
267         header.message_size = 0;
268         // header.message_size is rewritten later
269
MessageHeader_1_1Helper.write(this, header);
270     }
271
272     public void write_message_size()
273     {
274         int messageSize = _offset - 12;
275         int saveOffset = _offset;
276         _offset = 8;
277         write_long(messageSize);
278         _offset = saveOffset;
279     }
280
281     public void write_request(RequestHeader_1_2 request, CdrOutputStream parameters)
282     {
283         if (request.service_context == null)
284         {
285             // Avoid allocation of empty array by Helper.
286
request.service_context = EMPTY_SERVICE_CONTEXT_LIST;
287         }
288         write_message(MsgType_1_1._Request);
289         switch (_giopVersion)
290         {
291           case GiopVersion.VERSION_1_0:
292             RequestHeader_1_0 req10 = new RequestHeader_1_0();
293             req10.service_context = request.service_context;
294             req10.request_id = request.request_id;
295             req10.response_expected = (request.response_flags & 1) != 0;
296             req10.operation = request.operation;
297             req10.object_key = request.target.object_key();
298             RequestHeader_1_0Helper.write(this, req10);
299             break;
300           case GiopVersion.VERSION_1_1:
301             RequestHeader_1_1 req11 = new RequestHeader_1_1();
302             req11.service_context = request.service_context;
303             req11.request_id = request.request_id;
304             req11.response_expected = (request.response_flags & 1) != 0;
305             req11.operation = request.operation;
306             req11.object_key = request.target.object_key();
307             RequestHeader_1_1Helper.write(this, req11);
308             break;
309           case GiopVersion.VERSION_1_2:
310             RequestHeader_1_2Helper.write(this, request);
311             break;
312           default:
313             throw new IllegalStateException();
314         }
315         byte[] parametersBuffer = parameters.getBuffer();
316         int parametersLength = parameters.getOffset();
317         if (parametersLength > 0)
318         {
319             if (_giopVersion >= GiopVersion.VERSION_1_2)
320             {
321                 write_align(8, 0); // parameters are 8-byte aligned
322
}
323             else
324             {
325                 // TODO: should have padded service context earlier
326
}
327             write_octet_array(parametersBuffer, 0, parametersLength);
328         }
329         write_message_size();
330     }
331
332     public void write_reply(ReplyHeader_1_2 reply, CdrOutputStream results)
333     {
334         if (reply.service_context == null)
335         {
336             // Avoid allocation of empty array by Helper.
337
reply.service_context = EMPTY_SERVICE_CONTEXT_LIST;
338         }
339         write_message(MsgType_1_1._Reply);
340         switch (_giopVersion)
341         {
342           case GiopVersion.VERSION_1_0:
343           case GiopVersion.VERSION_1_1:
344             ReplyHeader_1_0 rep10 = new ReplyHeader_1_0();
345             rep10.service_context = reply.service_context;
346             rep10.request_id = reply.request_id;
347             rep10.reply_status = reply.reply_status;
348             ReplyHeader_1_0Helper.write(this, rep10);
349             break;
350           case GiopVersion.VERSION_1_2:
351             ReplyHeader_1_2Helper.write(this, reply);
352             break;
353           default:
354             throw new IllegalStateException();
355         }
356         byte[] resultsBuffer = results.getBuffer();
357         int resultsLength = results.getOffset();
358         if (resultsLength > 0)
359         {
360             if (_giopVersion >= GiopVersion.VERSION_1_2)
361             {
362                 write_align(8, 0); // results are 8-byte aligned
363
}
364             else
365             {
366                 // TODO: should have padded service context earlier
367
}
368             write_octet_array(resultsBuffer, 0, resultsLength);
369         }
370         write_message_size();
371     }
372
373     public void write_reply(LocateReplyHeader_1_2 reply)
374     {
375         write_message(MsgType_1_1._LocateReply);
376         LocateReplyHeader_1_2Helper.write(this, reply);
377         write_message_size();
378     }
379
380     public void write_SystemException(Exception ex, boolean withStackTrace)
381     {
382         String type = "UNKNOWN";
383         if (ex instanceof org.omg.CORBA.SystemException)
384         {
385             type = JavaClass.getNameSuffix(ex.getClass().getName());
386         }
387         else if (ex instanceof SecurityException)
388         {
389             type = "NO_PERMISSION";
390         }
391         else if (ex instanceof UnsupportedOperationException)
392         {
393             type = "BAD_OPERATION";
394         }
395         String id = "IDL:omg.org/CORBA/" + type + ":1.0";
396         write_string(id);
397         write_long(0); // minor (TODO: other values?)
398
write_long(0); // completed (TODO: other values?)
399
if (withStackTrace)
400         {
401             write_string(ExceptionUtil.getStackTrace(ex));
402         }
403     }
404
405     public void send_message(java.io.OutputStream output, String host)
406     {
407         if (RMI_TRACE)
408         {
409             byte[] data = new byte[_offset];
410             System.arraycopy(_buffer, 0, data, 0, _offset);
411             RmiTrace.send(host, data);
412         }
413         try
414         {
415             output.write(_buffer, 0, _offset);
416             output.flush();
417         }
418         catch (java.io.IOException ex)
419         {
420             throw new org.omg.CORBA.COMM_FAILURE(ex.toString());
421         }
422     }
423
424     // -----------------------------------------------------------------------
425
// public methods from org.omg.CORBA.portable.OutputStream
426
// -----------------------------------------------------------------------
427

428     public void write_boolean(boolean value)
429     {
430         write_align(1, 1);
431         if (value)
432         {
433             _buffer[_offset++] = 1;
434         }
435         else
436         {
437             _buffer[_offset++] = 0;
438         }
439     }
440
441     public void write_char(char value)
442     {
443         write_align(1, 1);
444         if ((int)value > 255)
445         {
446             throw new org.omg.CORBA.MARSHAL("write_char: value = " + value);
447         }
448         _buffer[_offset++] = (byte)value;
449     }
450
451     public void write_wchar(char value)
452     {
453         write_octet((byte)2); // size of wchar is 2 bytes
454
write_align(1, 2);
455         write_ushort_no_align_big_endian((int)value);
456     }
457
458     public void write_octet(byte value)
459     {
460         write_align(1, 1);
461         _buffer[_offset++] = value;
462     }
463
464     public void write_short(short value)
465     {
466         write_align(2, 2);
467         int oldOffset = _offset;
468         _offset += 2;
469         BigEndian.setShort(_buffer, oldOffset, value);
470     }
471
472     public void write_ushort(short value)
473     {
474         write_short(value);
475     }
476
477     public void write_long(int value)
478     {
479         write_align(4, 4);
480         int oldOffset = _offset;
481         _offset += 4;
482         BigEndian.setInt(_buffer, oldOffset, value);
483     }
484
485     public void write_ulong(int value)
486     {
487         write_long(value);
488     }
489
490     public void write_longlong(long value)
491     {
492         write_align(8, 8);
493         int oldOffset = _offset;
494         _offset += 8;
495         BigEndian.setLong(_buffer, oldOffset, value);
496     }
497
498     public void write_ulonglong(long value)
499     {
500         write_longlong(value);
501     }
502
503     public void write_float(float value)
504     {
505         write_long(Float.floatToIntBits(value));
506     }
507
508     public void write_double(double value)
509     {
510         write_longlong(Double.doubleToLongBits(value));
511     }
512
513     public void write_string(String value)
514     {
515         if (value == null)
516         {
517             value = "";
518         }
519         write_align(4, 4);
520         int size = UTF8.fromString(value, _buffer, _offset + 4, _length - 1);
521         if (size == -1)
522         {
523             // No room to convert in-place, ok to allocate new byte array.
524
byte[] bytes = UTF8.fromString(value);
525             size = bytes.length;
526             write_ulong(size + 1);
527             write_octet_array(bytes, 0, size);
528         }
529         else
530         {
531             // Already converted already into _buffer.
532
write_ulong(size + 1);
533             _offset += size;
534         }
535         write_octet((byte)0);
536     }
537
538     public void write_wstring(String value)
539     {
540         if (value == null)
541         {
542             value = "";
543         }
544         int size = value.length();
545         int numBytes = 2 * size;
546         write_ulong(numBytes); // No terminating NUL
547
write_align(1, numBytes);
548         for (int i = 0; i < size; i++)
549         {
550             char c = value.charAt(i);
551             BigEndian.setShort(_buffer, _offset, (short)c);
552             _offset += 2;
553         }
554     }
555
556     public void write_boolean_array(boolean[] value, int offset, int length)
557     {
558         for (int i = 0; i < length; i++)
559         {
560             write_boolean(value[offset + i]);
561         }
562     }
563
564     public void write_char_array(char[] value, int offset, int length)
565     {
566         for (int i = 0; i < length; i++)
567         {
568             write_char(value[offset + i]);
569         }
570     }
571
572     public void write_wchar_array(char[] value, int offset, int length)
573     {
574         for (int i = 0; i < length; i++)
575         {
576             write_wchar(value[offset + i]);
577         }
578     }
579
580     public void write_octet_array(byte[] value, int offset, int length)
581     {
582         write_align(1, length);
583         System.arraycopy(value, offset, _buffer, _offset, length);
584         _offset += length;
585     }
586
587     public void write_short_array(short[] value, int offset, int length)
588     {
589         for (int i = 0; i < length; i++)
590         {
591             write_short(value[offset + i]);
592         }
593     }
594
595     public void write_ushort_array(short[] value, int offset, int length)
596     {
597         for (int i = 0; i < length; i++)
598         {
599             write_ushort(value[offset + i]);
600         }
601     }
602
603     public void write_long_array(int[] value, int offset, int length)
604     {
605         for (int i = 0; i < length; i++)
606         {
607             write_long(value[offset + i]);
608         }
609     }
610
611     public void write_ulong_array(int[] value, int offset, int length)
612     {
613         for (int i = 0; i < length; i++)
614         {
615             write_ulong(value[offset + i]);
616         }
617     }
618
619     public void write_longlong_array(long[] value, int offset, int length)
620     {
621         for (int i = 0; i < length; i++)
622         {
623             write_longlong(value[offset + i]);
624         }
625     }
626
627     public void write_ulonglong_array(long[] value, int offset, int length)
628     {
629         for (int i = 0; i < length; i++)
630         {
631             write_ulonglong(value[offset + i]);
632         }
633     }
634
635     public void write_float_array(float[] value, int offset, int length)
636     {
637         for (int i = 0; i < length; i++)
638         {
639             write_float(value[offset + i]);
640         }
641     }
642
643     public void write_double_array(double[] value, int offset, int length)
644     {
645         for (int i = 0; i < length; i++)
646         {
647             write_double(value[offset + i]);
648         }
649     }
650
651     public void write_Object(org.omg.CORBA.Object value)
652     {
653         if (value == null)
654         {
655             write_IOR(null);
656         }
657         else if (value instanceof ObjectRef)
658         {
659             ObjectRef ref = (ObjectRef)value;
660             IOR ior = ref.$getIOR();
661             write_IOR(ior);
662         }
663         else
664         {
665             throw new org.omg.CORBA.MARSHAL(value.getClass().getName());
666         }
667     }
668
669     public void write_TypeCode(org.omg.CORBA.TypeCode tc)
670     {
671         write_TypeCode(tc, new java.util.HashMap());
672     }
673
674     public void write_Any(org.omg.CORBA.Any value)
675     {
676         org.omg.CORBA.TypeCode tc = value.type();
677         write_TypeCode(tc);
678         write_Any(value.create_input_stream(), tc);
679     }
680
681     // Sybase-internal
682
public void write_Any(org.omg.CORBA.portable.InputStream is, org.omg.CORBA.TypeCode tc)
683     {
684         try
685         {
686             int tk = tc.kind().value();
687             switch (tk)
688             {
689                 case TCKind._tk_null:
690                 case TCKind._tk_void:
691                     break;
692                 case TCKind._tk_TypeCode:
693                     write_TypeCode(is.read_TypeCode());
694                     break;
695                 case TCKind._tk_any:
696                     write_Any(is.read_any());
697                     break;
698                 case TCKind._tk_boolean:
699                     write_boolean(is.read_boolean());
700                     break;
701                 case TCKind._tk_char:
702                     write_char(is.read_char());
703                     break;
704                 case TCKind._tk_wchar:
705                     write_wchar(is.read_wchar());
706                     break;
707                 case TCKind._tk_octet:
708                     write_octet(is.read_octet());
709                     break;
710                 case TCKind._tk_short:
711                     write_short(is.read_short());
712                     break;
713                 case TCKind._tk_ushort:
714                     write_ushort(is.read_ushort());
715                     break;
716                 case TCKind._tk_long:
717                     write_long(is.read_long());
718                     break;
719                 case TCKind._tk_ulong:
720                 case TCKind._tk_enum:
721                     write_ulong(is.read_ulong());
722                     break;
723                 case TCKind._tk_longlong:
724                     write_longlong(is.read_longlong());
725                     break;
726                 case TCKind._tk_ulonglong:
727                     write_ulonglong(is.read_ulonglong());
728                     break;
729                 case TCKind._tk_float:
730                     write_float(is.read_float());
731                     break;
732                 case TCKind._tk_double:
733                     write_double(is.read_double());
734                     break;
735                 case TCKind._tk_string:
736                     write_string(is.read_string());
737                     break;
738                 case TCKind._tk_wstring:
739                     write_wstring(is.read_wstring());
740                     break;
741                 case TCKind._tk_objref:
742                     write_Object(is.read_Object());
743                     break;
744                 case TCKind._tk_alias:
745                     write_Any(is, tc.content_type());
746                     break;
747                 case TCKind._tk_array:
748                     {
749                         int n = tc.length();
750                         org.omg.CORBA.TypeCode c = tc.content_type();
751                         for (int i = 0; i < n; i++)
752                         {
753                             write_Any(is, c);
754                         }
755                     }
756                     break;
757                 case TCKind._tk_sequence:
758                     {
759                         int n = is.read_ulong();
760                         write_ulong(n);
761                         org.omg.CORBA.TypeCode c = tc.content_type();
762                         for (int i = 0; i < n; i++)
763                         {
764                             write_Any(is, c);
765                         }
766                     }
767                     break;
768                 case TCKind._tk_struct:
769                 case TCKind._tk_except:
770                     {
771                         int n = tc.member_count();
772                         for (int i = 0; i < n; i++)
773                         {
774                             write_Any(is, tc.member_type(i));
775                         }
776                     }
777                     break;
778                 case TCKind._tk_union:
779                     {
780                         org.omg.CORBA.TypeCode dt = tc.discriminator_type();
781                         org.omg.CORBA.Any disc = read_disc(is, dt);
782                         write_Any(disc.create_input_stream(), dt);
783                         int di = tc.default_index();
784                         int i, n = tc.member_count();
785                         for (i = 0; i < n; i++)
786                         {
787                             org.omg.CORBA.Any label = tc.member_label(i);
788                             if (label.equal(disc))
789                             {
790                                 write_Any(is, tc.member_type(i));
791                             }
792                         }
793                         if (i == n && di >= 0)
794                         {
795                             write_Any(is, tc.member_type(di));
796                         }
797                     }
798                     break;
799                 case TCKind._tk_fixed: // TODO
800
case TCKind._tk_value: // TODO
801
default:
802                     throw new org.omg.CORBA.MARSHAL("write_Any: type = " + tc);
803             }
804         }
805         catch (org.omg.CORBA.TypeCodePackage.BadKind ex)
806         {
807             throw new org.omg.CORBA.MARSHAL("write_Any: " + ex.toString());
808         }
809         catch (org.omg.CORBA.TypeCodePackage.Bounds ex)
810         {
811             throw new org.omg.CORBA.MARSHAL("write_Any: " + ex.toString());
812         }
813     }
814
815     public void write_any(org.omg.CORBA.Any value)
816     {
817         throw new org.omg.CORBA.NO_IMPLEMENT( "write_any: NOT IMPLMENTED YET" );
818     }
819
820     public void write_Principal(org.omg.CORBA.Principal value)
821     {
822         throw new org.omg.CORBA.NO_IMPLEMENT( "write_Principal: NOT IMPLMENTED YET" );
823     }
824
825     public void write(int value) throws java.io.IOException
826     {
827         throw new org.omg.CORBA.NO_IMPLEMENT( "write: NOT IMPLMENTED YET" );
828     }
829
830     public void write_fixed(java.math.BigDecimal value)
831     {
832         throw new org.omg.CORBA.NO_IMPLEMENT( "write_fixed: NOT IMPLMENTED YET" );
833     }
834
835     public void write_Context(org.omg.CORBA.Context context, org.omg.CORBA.ContextList list)
836     {
837         throw new org.omg.CORBA.NO_IMPLEMENT( "write_Context: NOT IMPLMENTED YET" );
838     }
839
840     public org.omg.CORBA.ORB orb()
841     {
842         throw new org.omg.CORBA.NO_IMPLEMENT( "orb: NOT IMPLMENTED YET" );
843     }
844
845
846     // -----------------------------------------------------------------------
847
// public methods from org.omg.CORBA_2_3.portable.OutputStream
848
// -----------------------------------------------------------------------
849

850     public void write_value(java.io.Serializable value)
851     {
852         throw new org.omg.CORBA.NO_IMPLEMENT( "write_value: NOT IMPLMENTED YET" );
853     }
854
855     public void write_value(java.io.Serializable value, Class _class)
856     {
857         throw new org.omg.CORBA.NO_IMPLEMENT( "write_value: NOT IMPLMENTED YET" );
858     }
859
860     public void write_value(java.io.Serializable value, String id)
861     {
862         throw new org.omg.CORBA.NO_IMPLEMENT( "write_value: NOT IMPLMENTED YET" );
863     }
864
865     public void write_value(java.io.Serializable value, org.omg.CORBA.portable.BoxedValueHelper helper)
866     {
867         throw new org.omg.CORBA.NO_IMPLEMENT( "write_value: NOT IMPLMENTED YET" );
868     }
869
870     public void write_abstract_interface(java.lang.Object value)
871     {
872         throw new org.omg.CORBA.NO_IMPLEMENT( "write_abstract_interface: NOT IMPLMENTED YET" );
873     }
874
875
876     // doing this specifically to handle Any. This implementation
877
// could be worng but will work for us
878
public org.omg.CORBA.portable.InputStream create_input_stream()
879     {
880         CdrInputStream is = CdrInputStream.getInstance();
881         is._buffer = new byte[_buffer.length];
882         System.arraycopy(_buffer,0,is._buffer,0,_buffer.length);
883         is._length = _buffer.length;
884         is._offset = 0;
885         return is;
886     }
887
888     // -----------------------------------------------------------------------
889
// protected methods
890
// -----------------------------------------------------------------------
891

892     protected void pool(byte[] oldBuffer)
893     {
894         if (oldBuffer.length <= MAXIMUM_POOLED_BUFFER_LENGTH)
895         {
896             _pooledBuffer = oldBuffer;
897         }
898     }
899
900     protected final void write_ushort_no_align_big_endian(int value)
901     {
902         int oldOffset = _offset;
903         _offset += 2;
904         BigEndian.setShort(_buffer, oldOffset, (short)value);
905     }
906
907     protected void write_IOR(IOR ior)
908     {
909         if (ior == null)
910         {
911             ior = NULL_IOR;
912         }
913         IORHelper.write(this, ior);
914     }
915
916     public int begin()
917     {
918         write_ulong(0);
919         int saveOffset = _offset;
920         write_boolean(false);
921         return saveOffset;
922     }
923
924     public void end(int saveOffset)
925     {
926         int endOffset = _offset;
927         _offset = saveOffset - 4;
928         write_ulong(endOffset - saveOffset);
929         _offset = endOffset;
930     }
931
932     private void write_TypeCode(org.omg.CORBA.TypeCode tc, java.util.HashMap table)
933     {
934         try
935         {
936             int tk = tc.kind().value();
937             // Check if we need to write an indirection
938
switch (tk)
939             {
940                 case TCKind._tk_struct:
941                 case TCKind._tk_union:
942                 case TCKind._tk_value:
943                     String id = tc.id();
944                     if (! id.equals(""))
945                     {
946                         Integer key = (Integer)table.get(id);
947                         if (key != null)
948                         {
949                             write_ulong(0xffffffff);
950                             write_long(key.intValue() - _offset);
951                             return;
952                         }
953                         table.put(id, new Integer(_offset));
954                     }
955             }
956             write_ulong(tk);
957             switch (tk)
958             {
959                 case TCKind._tk_null:
960                 case TCKind._tk_void:
961                 case TCKind._tk_TypeCode:
962                 case TCKind._tk_any:
963                 case TCKind._tk_boolean:
964                 case TCKind._tk_char:
965                 case TCKind._tk_wchar:
966                 case TCKind._tk_octet:
967                 case TCKind._tk_short:
968                 case TCKind._tk_ushort:
969                 case TCKind._tk_long:
970                 case TCKind._tk_ulong:
971                 case TCKind._tk_longlong:
972                 case TCKind._tk_ulonglong:
973                 case TCKind._tk_float:
974                 case TCKind._tk_double:
975                 case TCKind._tk_longdouble:
976                     // All these require only a TCKind.
977
break;
978                 case TCKind._tk_fixed:
979                     write_ushort(tc.fixed_digits());
980                     write_short(tc.fixed_scale());
981                     break;
982                 case TCKind._tk_string:
983                 case TCKind._tk_wstring:
984                     write_ulong(tc.length());
985                     break;
986                 case TCKind._tk_objref:
987                 case TCKind._tk_native:
988                 case TCKind._tk_abstract_interface:
989                     {
990                         int saveOffset = begin();
991                         write_string(tc.id());
992                         write_string(tc.name());
993                         end(saveOffset);
994                     }
995                     break;
996                 case TCKind._tk_alias:
997                 case TCKind._tk_value_box:
998                     {
999                         int saveOffset = begin();
1000                        write_string(tc.id());
1001                        write_string(tc.name());
1002                        write_TypeCode(tc.content_type(), table);
1003                        end(saveOffset);
1004                    }
1005                    break;
1006                case TCKind._tk_sequence:
1007                case TCKind._tk_array:
1008                    {
1009                        int saveOffset = begin();
1010                        write_TypeCode(tc.content_type(), table);
1011                        write_ulong(tc.length());
1012                        end(saveOffset);
1013                    }
1014                    break;
1015                case TCKind._tk_enum:
1016                    {
1017                        int saveOffset = begin();
1018                        write_string(tc.id());
1019                        write_string(tc.name());
1020                        int count = tc.member_count();
1021                        write_ulong(count);
1022                        for (int i = 0; i < count; i++)
1023                        {
1024                            write_string(tc.member_name(i));
1025                        }
1026                        end(saveOffset);
1027                    }
1028                    break;
1029                case TCKind._tk_struct:
1030                case TCKind._tk_except:
1031                    {
1032                        int saveOffset = begin();
1033                        write_string(tc.id());
1034                        write_string(tc.name());
1035                        int count = tc.member_count();
1036                        write_ulong(count);
1037                        for (int i = 0; i < count; i++)
1038                        {
1039                            write_string(tc.member_name(i));
1040                            write_TypeCode(tc.member_type(i), table);
1041                        }
1042                        end(saveOffset);
1043                    }
1044                    break;
1045                case TCKind._tk_union:
1046                    {
1047                        int saveOffset = begin();
1048                        write_string(tc.id());
1049                        write_string(tc.name());
1050                        org.omg.CORBA.TypeCode dt = tc.discriminator_type();
1051                        write_TypeCode(dt, table);
1052                        int di = tc.default_index();
1053                        write_ulong(di);
1054                        int count = tc.member_count();
1055                        write_ulong(count);
1056                        for (int i = 0; i < count; i++)
1057                        {
1058                            write_Any(tc.member_label(i).create_input_stream(), dt);
1059                            write_string(tc.member_name(i));
1060                            write_TypeCode(tc.member_type(i), table);
1061                        }
1062                        end(saveOffset);
1063                    }
1064                    break;
1065                case TCKind._tk_value:
1066                    {
1067                        int saveOffset = begin();
1068                        write_string(tc.id());
1069                        write_string(tc.name());
1070                        write_short(tc.type_modifier());
1071                        write_TypeCode(tc.concrete_base_type(), table);
1072                        int count = tc.member_count();
1073                        write_ulong(count);
1074                        for (int i = 0; i < count; i++)
1075                        {
1076                            write_string(tc.member_name(i));
1077                            write_TypeCode(tc.member_type(i), table);
1078                            write_short(tc.member_visibility(i));
1079                        }
1080                        end(saveOffset);
1081                    }
1082                    break;
1083                default:
1084                    throw new org.omg.CORBA.MARSHAL("write_TypeCode: kind = " + tk);
1085            }
1086        }
1087        catch (org.omg.CORBA.TypeCodePackage.BadKind ex)
1088        {
1089            throw new org.omg.CORBA.MARSHAL(ex.toString());
1090        }
1091        catch (org.omg.CORBA.TypeCodePackage.Bounds ex)
1092        {
1093            throw new org.omg.CORBA.MARSHAL(ex.toString());
1094        }
1095    }
1096
1097    private org.omg.CORBA.Any read_disc(org.omg.CORBA.portable.InputStream is, org.omg.CORBA.TypeCode dt)
1098    {
1099        int tk = dt.kind().value();
1100        if (tk == TCKind._tk_alias)
1101        {
1102            try
1103            {
1104                return read_disc(is, dt.content_type());
1105            }
1106            catch (org.omg.CORBA.TypeCodePackage.BadKind ex)
1107            {
1108                throw new org.omg.CORBA.MARSHAL("read_disc: " + ex.toString());
1109            }
1110        }
1111        org.omg.CORBA.Any disc = new gcc.rmi.iiop.Any();
1112        switch (tk)
1113        {
1114            case TCKind._tk_boolean:
1115                disc.insert_boolean(is.read_boolean());
1116                break;
1117            case TCKind._tk_octet:
1118                disc.insert_octet(is.read_octet());
1119                break;
1120            case TCKind._tk_short:
1121                disc.insert_short(is.read_short());
1122                break;
1123            case TCKind._tk_ushort:
1124                disc.insert_ushort(is.read_ushort());
1125                break;
1126            case TCKind._tk_long:
1127                disc.insert_long(is.read_long());
1128                break;
1129            case TCKind._tk_ulong:
1130            case TCKind._tk_enum:
1131                disc.insert_ulong(is.read_ulong());
1132                break;
1133            case TCKind._tk_longlong:
1134                disc.insert_longlong(is.read_longlong());
1135                break;
1136            case TCKind._tk_ulonglong:
1137                disc.insert_ulonglong(is.read_ulonglong());
1138                break;
1139            default:
1140                throw new org.omg.CORBA.MARSHAL("read_disc: type = " + dt);
1141        }
1142        return disc;
1143    }
1144}
1145
Popular Tags