1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68:
69: import ;
70: import ;
71: import ;
72: import ;
73:
74:
88: public abstract class AbstractCdrOutput
89: extends org.omg.CORBA_2_3.portable.OutputStream
90: implements org.omg.CORBA.DataOutputStream
91: {
92:
96: public transient gnuRuntime runtime;
97:
98:
102: protected AbstractDataOutput b;
103:
104:
107: protected ORB orb;
108:
109:
112: protected Version giop = new Version(1, 2);
113:
114:
117: protected CodeSetServiceContext codeset;
118:
119:
122: private String narrow_charset;
123:
124:
128: private String wide_charset;
129:
130:
135: private boolean narrow_native;
136:
137:
142: private boolean wide_native;
143:
144:
148: private boolean little_endian;
149:
150:
153: private java.io.OutputStream actual_stream;
154:
155:
160: public AbstractCdrOutput(java.io.OutputStream writeTo)
161: {
162: setOutputStream(writeTo);
163: setCodeSet(CodeSetServiceContext.STANDARD);
164: }
165:
166:
170: public AbstractCdrOutput()
171: {
172: setCodeSet(CodeSetServiceContext.STANDARD);
173: }
174:
175:
179: public abstract void setOffset(int an_offset);
180:
181:
184: public void cloneSettings(AbstractCdrOutput stream)
185: {
186: stream.setBigEndian(!little_endian);
187: stream.setCodeSet(getCodeSet());
188: stream.setVersion(giop);
189: stream.setOrb(orb);
190: }
191:
192:
195: public void setCodeSet(CodeSetServiceContext a_codeset)
196: {
197: this.codeset = a_codeset;
198: narrow_charset = CharSets_OSF.getName(codeset.char_data);
199: wide_charset = CharSets_OSF.getName(codeset.wide_char_data);
200:
201: narrow_native = CharSets_OSF.NATIVE_CHARACTER == codeset.char_data;
202: wide_native = CharSets_OSF.NATIVE_WIDE_CHARACTER == codeset.wide_char_data;
203: }
204:
205:
208: public CodeSetServiceContext getCodeSet()
209: {
210: return codeset;
211: }
212:
213:
217: public void setOrb(ORB an_orb)
218: {
219: orb = an_orb;
220: }
221:
222:
227: public void setOutputStream(java.io.OutputStream writeTo)
228: {
229: if (little_endian)
230: b = new LittleEndianOutputStream(writeTo);
231: else
232: b = new BigEndianOutputStream(writeTo);
233:
234: actual_stream = writeTo;
235: }
236:
237:
241: public void setVersion(Version giop_version)
242: {
243: giop = giop_version;
244: }
245:
246:
253: public void setBigEndian(boolean use_big_endian)
254: {
255: little_endian = !use_big_endian;
256: setOutputStream(actual_stream);
257: }
258:
259:
262: public abstract void align(int boundary);
263:
264:
278: public AbstractCdrOutput createEncapsulation()
279: {
280: return new EncapsulationStream(this, !little_endian);
281: }
282:
283:
287: public ORB orb()
288: {
289: return orb;
290: }
291:
292:
296: public void write(int n)
297: {
298: try
299: {
300: b.write(n);
301: }
302: catch (IOException ex)
303: {
304: Unexpected.error(ex);
305: }
306: }
307:
308:
311: public void write(byte[] x)
312: throws java.io.IOException
313: {
314: b.write(x);
315: }
316:
317:
320: public void write(byte[] x, int ofs, int len)
321: throws java.io.IOException
322: {
323: b.write(x, ofs, len);
324: }
325:
326:
330: public void write_Context(Context context, ContextList contexts)
331: {
332: throw new NO_IMPLEMENT();
333: }
334:
335:
343: public void write_Object(org.omg.CORBA.Object x)
344: {
345: ORB w_orb = orb;
346: if (x instanceof IorProvider)
347: {
348: ((IorProvider) x).getIor()._write_no_endian(this);
349: return;
350: }
351: else if (x == null)
352: {
353: IOR.write_null(this);
354: return;
355: }
356: else if (x instanceof ObjectImpl)
357: {
358: Delegate d = ((ObjectImpl) x)._get_delegate();
359:
360: if (d instanceof IorProvider)
361: {
362: ((IorProvider) d).getIor()._write_no_endian(this);
363: return;
364: }
365: else
366: {
367: ORB d_orb = d.orb(x);
368: if (d_orb != null)
369: w_orb = d_orb;
370: }
371: }
372:
373:
374:
375:
376: if (w_orb != null)
377: {
378: IOR ior = IOR.parse(w_orb.object_to_string(x));
379: ior._write_no_endian(this);
380: return;
381: }
382: else
383: throw new BAD_OPERATION(
384: "Please set the ORB for this stream, cannot write "
385: + x.getClass().getName());
386: }
387:
388:
394: public void write_TypeCode(TypeCode x)
395: {
396: try
397: {
398: TypeCodeHelper.write(this, x);
399: }
400: catch (UserException ex)
401: {
402: Unexpected.error(ex);
403: }
404: }
405:
406:
415: public void write_any(Any x)
416: {
417: Streamable value = x.extract_Streamable();
418: if (value != null)
419: {
420: write_TypeCode(x.type());
421: value._write(this);
422: }
423: else
424: {
425: PrimitiveTypeCode p = new PrimitiveTypeCode(TCKind.tk_null);
426: write_TypeCode(p);
427: }
428: }
429:
430:
436: public void write_boolean(boolean x)
437: {
438: try
439: {
440: b.write(x ? 1 : 0);
441: }
442: catch (IOException ex)
443: {
444: Unexpected.error(ex);
445: }
446: }
447:
448:
455: public void write_boolean_array(boolean[] x, int ofs, int len)
456: {
457: try
458: {
459: for (int i = ofs; i < ofs + len; i++)
460: {
461: b.write(x [ i ] ? 1 : 0);
462: }
463: }
464: catch (IOException ex)
465: {
466: Unexpected.error(ex);
467: }
468: }
469:
470:
476: public void write_char(char x)
477: {
478: try
479: {
480: if (narrow_native)
481: b.write(x);
482: else
483: {
484: OutputStreamWriter ow =
485: new OutputStreamWriter((OutputStream) b, narrow_charset);
486: ow.write(x);
487: ow.flush();
488: }
489: }
490: catch (IOException ex)
491: {
492: Unexpected.error(ex);
493: }
494: }
495:
496:
503: public void write_char_array(char[] chars, int offset, int length)
504: {
505: try
506: {
507: if (narrow_native)
508: {
509: for (int i = offset; i < offset + length; i++)
510: {
511: b.write(chars [ i ]);
512: }
513: }
514: else
515: {
516: OutputStreamWriter ow =
517: new OutputStreamWriter((OutputStream) b, narrow_charset);
518: ow.write(chars, offset, length);
519: ow.flush();
520: }
521: }
522: catch (IOException ex)
523: {
524: Unexpected.error(ex);
525: }
526: }
527:
528:
531: public void write_double(double x)
532: {
533: try
534: {
535: align(8);
536: b.writeDouble(x);
537: }
538: catch (Exception ex)
539: {
540: Unexpected.error(ex);
541: }
542: }
543:
544:
547: public void write_double_array(double[] x, int ofs, int len)
548: {
549: try
550: {
551: align(8);
552: for (int i = ofs; i < ofs + len; i++)
553: {
554: b.writeDouble(x [ i ]);
555: }
556: }
557: catch (IOException ex)
558: {
559: Unexpected.error(ex);
560: }
561: }
562:
563:
568: public void write_fixed(BigDecimal fixed)
569: {
570: try
571: {
572: BigDecimalHelper.write(this, fixed);
573: }
574: catch (IOException ex)
575: {
576: Unexpected.error(ex);
577: }
578: catch (BadKind ex)
579: {
580: Unexpected.error(ex);
581: }
582: }
583:
584:
587: public void write_float(float x)
588: {
589: try
590: {
591: align(4);
592: b.writeFloat(x);
593: }
594: catch (IOException ex)
595: {
596: Unexpected.error(ex);
597: }
598: }
599:
600:
603: public void write_float_array(float[] x, int ofs, int len)
604: {
605: try
606: {
607: align(4);
608: for (int i = ofs; i < ofs + len; i++)
609: {
610: b.writeFloat(x [ i ]);
611: }
612: }
613: catch (IOException ex)
614: {
615: Unexpected.error(ex);
616: }
617: }
618:
619:
623: public void write_long(int x)
624: {
625: try
626: {
627: align(4);
628: b.writeInt(x);
629: }
630: catch (IOException ex)
631: {
632: Unexpected.error(ex);
633: }
634: }
635:
636:
643: public void write_long_array(int[] x, int ofs, int len)
644: {
645: try
646: {
647: align(4);
648: for (int i = ofs; i < ofs + len; i++)
649: {
650: b.writeInt(x [ i ]);
651: }
652: }
653: catch (IOException ex)
654: {
655: Unexpected.error(ex);
656: }
657: }
658:
659:
665: public void write_longlong(long x)
666: {
667: try
668: {
669: align(8);
670: b.writeLong(x);
671: }
672: catch (IOException ex)
673: {
674: Unexpected.error(ex);
675: }
676: }
677:
678:
685: public void write_longlong_array(long[] x, int ofs, int len)
686: {
687: try
688: {
689: align(8);
690: for (int i = ofs; i < ofs + len; i++)
691: {
692: b.writeLong(x [ i ]);
693: }
694: }
695: catch (IOException ex)
696: {
697: Unexpected.error(ex);
698: }
699: }
700:
701:
705: public void write_octet(byte x)
706: {
707: try
708: {
709: b.writeByte(x);
710: }
711: catch (IOException ex)
712: {
713: Unexpected.error(ex);
714: }
715: }
716:
717:
724: public void write_octet_array(byte[] x, int ofs, int len)
725: {
726: try
727: {
728: b.write(x, ofs, len);
729: }
730: catch (IOException ex)
731: {
732: Unexpected.error(ex);
733: }
734: }
735:
736:
742: public void write_sequence(byte[] buf)
743: {
744: try
745: {
746: write_long(buf.length);
747: write(buf);
748: }
749: catch (IOException ex)
750: {
751: MARSHAL t = new MARSHAL();
752: t.minor = Minor.CDR;
753: t.initCause(ex);
754: throw t;
755: }
756: }
757:
758:
764: public void write_sequence(BufferedCdrOutput from)
765: {
766: try
767: {
768: write_long(from.buffer.size());
769: from.buffer.writeTo(this);
770: }
771: catch (IOException ex)
772: {
773: MARSHAL t = new MARSHAL();
774: t.minor = Minor.CDR;
775: t.initCause(ex);
776: throw t;
777: }
778: }
779:
780:
785: public void write_short(short x)
786: {
787: try
788: {
789: align(2);
790: b.writeShort(x);
791: }
792: catch (IOException ex)
793: {
794: Unexpected.error(ex);
795: }
796: }
797:
798:
805: public void write_short_array(short[] x, int ofs, int len)
806: {
807: try
808: {
809: align(2);
810: for (int i = ofs; i < ofs + len; i++)
811: {
812: b.writeShort(x [ i ]);
813: }
814: }
815: catch (IOException ex)
816: {
817: Unexpected.error(ex);
818: }
819: }
820:
821:
831: public void write_string(String x)
832: {
833: try
834: {
835: byte[] ab = x.getBytes(narrow_charset);
836: write_long(ab.length + 1);
837: write(ab);
838:
839:
840: write(0);
841: }
842: catch (IOException ex)
843: {
844: Unexpected.error(ex);
845: }
846: }
847:
848:
851: public void write_ulong(int x)
852: {
853: write_long(x);
854: }
855:
856:
860: public void write_ulong_array(int[] x, int ofs, int len)
861: {
862: write_long_array(x, ofs, len);
863: }
864:
865:
870: public void write_ulonglong(long x)
871: {
872: write_longlong(x);
873: }
874:
875:
879: public void write_ulonglong_array(long[] x, int ofs, int len)
880: {
881: write_longlong_array(x, ofs, len);
882: }
883:
884:
887: public void write_ushort(short x)
888: {
889: write_short(x);
890: }
891:
892:
896: public void write_ushort_array(short[] x, int ofs, int len)
897: {
898: write_short_array(x, ofs, len);
899: }
900:
901:
908: public void write_wchar(char x)
909: {
910: try
911: {
912: if (giop.until_inclusive(1, 1))
913: {
914: align(2);
915:
916: if (wide_native)
917: b.writeShort(x);
918: else
919: {
920: OutputStreamWriter ow = new OutputStreamWriter(
921: (OutputStream) b, wide_charset);
922: ow.write(x);
923: ow.flush();
924: }
925: }
926: else if (wide_native)
927: {
928: b.writeByte(2);
929: b.writeChar(x);
930: }
931: else
932: {
933: String encoded = new String(new char[] { x });
934: byte[] bytes = encoded.getBytes(wide_charset);
935: b.write(bytes.length + 2);
936: b.write(bytes);
937: }
938: }
939: catch (IOException ex)
940: {
941: Unexpected.error(ex);
942: }
943: }
944:
945:
955: public void write_wchar_array(char[] chars, int offset, int length)
956: {
957: try
958: {
959: if (giop.until_inclusive(1, 1))
960: align(2);
961:
962: if (wide_native)
963: {
964: for (int i = offset; i < offset + length; i++)
965: {
966: b.writeShort(chars [ i ]);
967: }
968: }
969: else
970: {
971: OutputStreamWriter ow =
972: new OutputStreamWriter((OutputStream) b, wide_charset);
973: ow.write(chars, offset, length);
974: ow.flush();
975: }
976: }
977: catch (IOException ex)
978: {
979: Unexpected.error(ex);
980: }
981: }
982:
983:
991: public void write_wstring(String x)
992: {
993: try
994: {
995: if (giop.since_inclusive(1, 2))
996: {
997: byte[] bytes = x.getBytes(wide_charset);
998: write_sequence(bytes);
999: }
1000: else
1001: {
1002:
1003:
1004: write_long(2 * x.length() + 2);
1005:
1006: for (int i = 0; i < x.length(); i++)
1007: {
1008: b.writeShort(x.charAt(i));
1009: }
1010:
1011:
1012: b.writeShort(0);
1013: }
1014: }
1015: catch (IOException ex)
1016: {
1017: Unexpected.error(ex);
1018: }
1019: }
1020:
1021:
1022: public void write_any_array(Any[] anys, int offset, int length)
1023: {
1024: for (int i = offset; i < offset + length; i++)
1025: {
1026: write_any(anys [ i ]);
1027: }
1028: }
1029:
1030: public String[] _truncatable_ids()
1031: {
1032:
1033: throw new java.lang.UnsupportedOperationException("Method _truncatable_ids() not yet implemented.");
1034: }
1035:
1036:
1037: public void write_Abstract(java.lang.Object value)
1038: {
1039: write_abstract_interface(value);
1040: }
1041:
1042:
1043: public void write_Value(Serializable value)
1044: {
1045: write_value(value);
1046: }
1047: }