1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66:
67: import ;
68: import ;
69:
70:
75: public abstract class Vio
76: {
77:
87: public static boolean USE_CHUNKING = false;
88:
89:
94: public static final int vt_VALUE_TAG = 0x7fffff00;
95:
96:
100: public static final int vf_CODEBASE = 0x1;
101:
102:
106: public static final int vf_ID = 0x2;
107:
108:
113: public static final int vf_MULTIPLE_IDS = 0x4;
114:
115:
120: public static final int vf_CHUNKING = 0x8;
121:
122:
129: public static final int vt_INDIRECTION = 0xffffffff;
130:
131:
135: public static final int vt_NULL = 0x0;
136:
137:
140: static final int INT_SIZE = 4;
141:
142:
145: public static final WStringValueHelper m_StringValueHelper = new WStringValueHelper();
146:
147:
150: static ValueHandler handler = Util.createValueHandler();
151:
152:
165: public static Serializable read(InputStream input)
166: {
167: return read(input, (String) null);
168: }
169:
170:
184: public static Serializable read(InputStream input, String repository_id)
185: {
186: try
187: {
188: final int position = getCurrentPosition(input);
189:
190: input.mark(512);
191:
192: int value_tag = input.read_long();
193: checkTag(value_tag);
194:
195: String codebase = null;
196: String[] ids = null;
197: String id = repository_id;
198:
199:
200: if (value_tag == vt_NULL)
201: return null;
202: else if (value_tag == vt_INDIRECTION)
203: return readIndirection(input);
204: else
205: {
206:
207: if ((value_tag & vf_CODEBASE) != 0)
208: {
209:
210:
211:
212: codebase = read_string(input);
213: }
214:
215: if ((value_tag & vf_MULTIPLE_IDS) != 0)
216: {
217:
218: ids = read_string_array(input);
219: }
220: else if ((value_tag & vf_ID) != 0)
221: {
222:
223: id = read_string(input);
224: }
225: }
226:
227: BoxedValueHelper helper = getHelper(null, id);
228:
229: java.lang.Object ox = null;
230:
231: if (helper != null)
232: ox = null;
233: else if (id.equals(WStringValueHelper.id()))
234: helper = m_StringValueHelper;
235: else
236: ox = createInstance(id, ids, codebase);
237: return (Serializable) read_instance(input, position, ox, value_tag,
238: helper, id, ids, codebase);
239: }
240: catch (Exception ex)
241: {
242: MARSHAL m = new MARSHAL();
243: m.minor = Minor.Value;
244: m.initCause(ex);
245: throw m;
246: }
247: }
248:
249:
260: public static Serializable read(InputStream input, Class value_class)
261: {
262: final int position = getCurrentPosition(input);
263:
264: String id = null;
265: String[] ids = null;
266: String codebase = null;
267:
268: try
269: {
270: int value_tag = input.read_long();
271: checkTag(value_tag);
272:
273:
274: if (value_tag == vt_NULL)
275: return null;
276: else if (value_tag == vt_INDIRECTION)
277: return readIndirection(input);
278: else
279: {
280:
281: if ((value_tag & vf_CODEBASE) != 0)
282: {
283:
284: codebase = read_string(input);
285: }
286:
287: if ((value_tag & vf_MULTIPLE_IDS) != 0)
288: {
289:
290: ids = read_string_array(input);
291: }
292: else if ((value_tag & vf_ID) != 0)
293: {
294:
295: id = read_string(input);
296: }
297: }
298:
299: BoxedValueHelper vHelper = id != null ? getHelper(value_class, id)
300: : getHelper(value_class, ids);
301:
302: java.lang.Object ox;
303:
304: if (vHelper == null)
305: {
306: try
307: {
308: ox = createInstance(id, ids, codebase);
309: }
310: catch (Exception e)
311: {
312: ox = null;
313: }
314:
315: if (ox != null)
316: {
317: if (value_class != null
318: && !value_class.isAssignableFrom(ox.getClass()))
319: {
320: MARSHAL m = new MARSHAL(ox.getClass() + " is not a "
321: + value_class.getName());
322: m.minor = Minor.ClassCast;
323: throw m;
324: }
325: }
326: }
327: else
328: ox = null;
329:
330: ox = read_instance(input, position, ox, value_tag, vHelper, id, ids,
331: codebase);
332: return (Serializable) ox;
333: }
334: catch (MARSHAL m)
335: {
336: throw m;
337: }
338: catch (SystemException sysEx)
339: {
340:
341: throw sysEx;
342: }
343: catch (Exception ex)
344: {
345: MARSHAL m = new MARSHAL("Cant read " + value_class);
346: m.minor = Minor.Value;
347: m.initCause(ex);
348: throw m;
349: }
350: }
351:
352:
370: public static Object read(InputStream input, Object value_instance,
371: BoxedValueHelper helper)
372: {
373: final int position = getCurrentPosition(input);
374:
375: String id = null;
376: String[] ids = null;
377: String codebase = null;
378:
379: try
380: {
381: int value_tag = input.read_long();
382: checkTag(value_tag);
383:
384:
385: if (value_tag == vt_NULL)
386: return null;
387: else if (value_tag == vt_INDIRECTION)
388: return readIndirection(input);
389: else
390: {
391:
392: if ((value_tag & vf_CODEBASE) != 0)
393: {
394:
395: codebase = read_string(input);
396: }
397:
398: if ((value_tag & vf_MULTIPLE_IDS) != 0)
399: {
400:
401: ids = read_string_array(input);
402: }
403: else if ((value_tag & vf_ID) != 0)
404: {
405:
406: id = read_string(input);
407: }
408: }
409:
410: Class value_class = value_instance == null ? null
411: : value_instance.getClass();
412:
413: if (helper == null)
414: helper = id != null ? getHelper(value_class, id) : getHelper(
415: value_class, ids);
416:
417: value_instance = read_instance(input, position, value_instance,
418: value_tag, helper, id, ids, codebase);
419: return value_instance;
420: }
421: catch (Exception ex)
422: {
423: MARSHAL m = new MARSHAL();
424: m.minor = Minor.Value;
425: m.initCause(ex);
426: throw m;
427: }
428: }
429:
430:
441: public static Serializable read(InputStream input, BoxedValueHelper helper)
442: {
443: return (Serializable) read(input, null, helper);
444: }
445:
446:
463: static Object read_instance(InputStream input, final int position,
464: Object value, int value_tag, BoxedValueHelper helper, String id,
465: String[] ids, String codebase)
466: {
467: if (helper != m_StringValueHelper && id != null)
468: if (id.equals(StringValueHelper.id()))
469: {
470: value = null;
471: helper = m_StringValueHelper;
472: }
473:
474: try
475: {
476: if ((value_tag & vf_CHUNKING) != 0)
477: {
478: BufferedCdrOutput output = createBuffer(input, 1024);
479:
480: readNestedValue(value_tag, input, output, -1);
481: BufferredCdrInput ci = new BufferredCdrInput(output.buffer.getBuffer());
482: ci.setRunTime(output.getRunTime());
483:
484: input = new HeadlessInput(ci, input);
485: }
486: else
487: {
488: if (input instanceof BufferredCdrInput)
489: {
490:
491: input = new HeadlessInput((BufferredCdrInput) input, null);
492: }
493: else if (input instanceof HeadlessInput)
494: {
495:
496:
497: ((HeadlessInput) input).subsequentCalls = false;
498: }
499: else
500: {
501: BufferedCdrOutput bout = new BufferedCdrOutput();
502: int c;
503: while ((c = input.read()) >= 0)
504: bout.write((byte) c);
505: input = new HeadlessInput(
506: (BufferredCdrInput) bout.create_input_stream(), input);
507: }
508: }
509: }
510: catch (IOException ex)
511: {
512: MARSHAL m = new MARSHAL("Unable to read chunks");
513: m.minor = Minor.Value;
514: m.initCause(ex);
515: throw m;
516: }
517:
518: return readValue(input, position, value, helper, id, ids, codebase);
519: }
520:
521:
524: private static BufferedCdrOutput createBuffer(InputStream input, int proposed_size)
525: {
526: BufferedCdrOutput bout;
527: bout = new BufferedCdrOutput(2 * proposed_size + 256);
528:
529: if (input instanceof BufferredCdrInput)
530: {
531: BufferredCdrInput in = (BufferredCdrInput) input;
532: bout.setBigEndian(in.isBigEndian());
533: }
534:
535: if (input instanceof gnuValueStream)
536: bout.setRunTime(((gnuValueStream) input).getRunTime());
537: else
538: bout.setRunTime(new gnuRuntime(null, null));
539: return bout;
540: }
541:
542:
554: public static int readNestedValue(int value_tag, InputStream input,
555: BufferedCdrOutput output, int level)
556: throws IOException
557: {
558: String id = null;
559: if (level < -1)
560: {
561:
562: output.write_long(value_tag - vf_CHUNKING);
563:
564:
565: if ((value_tag & vf_CHUNKING) == 0)
566: {
567: MARSHAL m = new MARSHAL("readNestedValue: must be chunked");
568: m.minor = Minor.Chunks;
569: throw m;
570: }
571: else if (value_tag == vt_NULL)
572: {
573: MARSHAL m = new MARSHAL("readNestedValue: nul");
574: m.minor = Minor.Chunks;
575: throw m;
576: }
577: else if (value_tag == vt_INDIRECTION)
578: {
579: MARSHAL m = new MARSHAL("readNestedValue: indirection");
580: m.minor = Minor.Chunks;
581: throw m;
582: }
583: else
584: {
585:
586: if ((value_tag & vf_CODEBASE) != 0)
587: {
588: String codebase = read_string(input);
589: write_string(output, codebase);
590: }
591:
592: if ((value_tag & vf_MULTIPLE_IDS) != 0)
593: {
594:
595: String[] ids = read_string_array(input);
596: id = ids[0];
597: write_string_array(output, ids);
598: }
599: else if ((value_tag & vf_ID) != 0)
600: {
601: id = read_string(input);
602: write_string(output, id);
603: }
604: }
605: }
606:
607: int n = -1;
608:
609:
610: int chunk_size;
611:
612: byte[] r = null;
613:
614: while (true)
615: {
616:
617:
618: chunk_size = input.read_long();
619:
620:
621: if (chunk_size < 0 && chunk_size >= level)
622: return chunk_size;
623: else if (chunk_size >= 0x7FFFFF00)
624: {
625: int onInput = getCurrentPosition(input) - 4;
626: int onOutput = output.getPosition();
627: output.getRunTime().redirect(onInput, onOutput);
628:
629:
630:
631: chunk_size = readNestedValue(chunk_size, input, output, level - 1);
632: if (chunk_size < 0 && chunk_size >= level)
633: return chunk_size;
634: }
635: else
636: {
637:
638: if (r == null || r.length < chunk_size)
639: r = new byte[chunk_size + 256];
640:
641: n = 0;
642: while (n < chunk_size)
643: n += input.read(r, n, chunk_size - n);
644: output.write(r, 0, n);
645: }
646: }
647: }
648:
649:
652: public static Serializable readValue(InputStream input, final int position,
653: Object value, BoxedValueHelper helper, String id, String[] ids,
654: String codebase)
655: {
656: gnuRuntime g;
657: gnuValueStream c = ((gnuValueStream) input);
658: if (c.getRunTime() == null)
659: {
660: g = new gnuRuntime(codebase, value);
661: c.setRunTime(g);
662: }
663: else
664: {
665: g = c.getRunTime();
666: g.addCodeBase(codebase);
667: g.target = (Serializable) value;
668: }
669: if (value != null)
670: g.objectWritten(value, position);
671:
672: if (input instanceof HeadlessInput)
673: ((HeadlessInput) input).subsequentCalls = false;
674:
675: boolean ok = true;
676:
677:
678: if (value instanceof CustomMarshal)
679: {
680: CustomMarshal marsh = (CustomMarshal) value;
681: marsh.unmarshal((DataInputStream) input);
682: }
683: else
684:
685: if (value instanceof Streamable)
686: {
687: ((Streamable) value)._read(input);
688: }
689: else if (helper != null)
690: {
691:
692: value = helper.read_value(input);
693: g.objectWritten(value, position);
694: }
695: else
696: {
697: ok = false;
698: ValueFactory factory = null;
699: org.omg.CORBA_2_3.ORB orb = (org.omg.CORBA_2_3.ORB) input.orb();
700:
701: if (id != null)
702: factory = orb.lookup_value_factory(id);
703:
704: if (factory == null && ids != null)
705: {
706: for (int i = 0; i < ids.length && factory == null; i++)
707: {
708: factory = orb.lookup_value_factory(ids[i]);
709: }
710: }
711:
712: if (factory != null)
713: {
714: value = factory.read_value((org.omg.CORBA_2_3.portable.InputStream) input);
715: ok = true;
716: }
717: }
718:
719: if (!ok && value instanceof Serializable)
720:
721: {
722: if (ids != null && ids.length > 0)
723: id = ids[0];
724:
725: value = handler.readValue(input, position, value.getClass(), id, g);
726: ok = true;
727: }
728:
729: if (!ok)
730: {
731: if (value != null)
732: {
733: MARSHAL m = new MARSHAL(value.getClass().getName()
734: + " must be Streamable, CustomMarshal or Serializable");
735: m.minor = Minor.UnsupportedValue;
736: throw m;
737: }
738: else
739: {
740: MARSHAL m = new MARSHAL("Unable to instantiate " + id + ":" + list(ids)
741: + " helper " + helper);
742: m.minor = Minor.UnsupportedValue;
743: throw m;
744: }
745: }
746: else
747: return (Serializable) value;
748: }
749:
750:
753: static String list(String[] s)
754: {
755: if (s == null)
756: return "null";
757: else
758: {
759: CPStringBuilder b = new CPStringBuilder("{");
760: for (int i = 0; i < s.length; i++)
761: {
762: b.append(s[i]);
763: b.append(" ");
764: }
765: b.append("}");
766: return b.toString();
767: }
768: }
769:
770:
780: public static void write(OutputStream output, Serializable value)
781: {
782:
783: if (value == null)
784: output.write_long(vt_NULL);
785: else if (value instanceof String)
786: write(output, value, m_StringValueHelper);
787: else
788: write(output, value, value.getClass());
789: }
790:
791:
801: public static void write(OutputStream output, Serializable value,
802: Class substitute)
803: {
804:
805: if (value == null)
806: output.write_long(vt_NULL);
807: else if (value instanceof String || substitute == String.class)
808: writeString(output, value);
809: else
810: {
811: String vId = ObjectCreator.getRepositoryId(value.getClass());
812: if (substitute == null || value.getClass().equals(substitute))
813: write_instance(output, value, vId, getHelper(value.getClass(), vId));
814: else
815: {
816: String vC = ObjectCreator.getRepositoryId(substitute);
817: String[] ids = new String[] { vId, vC };
818: BoxedValueHelper h = getHelper(substitute.getClass(), ids);
819:
820:
821:
822: if (h == null)
823: write_instance(output, value, ids, null);
824: else
825: write_instance(output, value, h.get_id(), null);
826: }
827: }
828: }
829:
830:
841: public static void write(OutputStream output, Serializable value,
842: String[] multiple_ids)
843: {
844:
845: if (value == null)
846: output.write_long(vt_NULL);
847: else
848: {
849: String[] ids = new String[multiple_ids.length + 1];
850: ids[0] = ObjectCreator.getRepositoryId(value.getClass());
851: System.arraycopy(multiple_ids, 0, ids, 1, multiple_ids.length);
852: BoxedValueHelper h = getHelper(value.getClass(), ids);
853: write_instance(output, value, ids, h);
854: }
855: }
856:
857:
865: public static void write(OutputStream output, Serializable value, String id)
866: {
867: if (value == null)
868: output.write_long(vt_NULL);
869: else
870: write_instance(output, value, id, getHelper(value.getClass(), id));
871: }
872:
873:
882: public static void write(OutputStream output, Serializable value,
883: BoxedValueHelper helper)
884: {
885: if (helper == null)
886: throw new AssertionError("Helper must be provided");
887: if (value == null)
888: output.write_long(vt_NULL);
889: else
890: write_instance(output, value, helper.get_id(), helper);
891: }
892:
893:
896: private static void writeString(OutputStream output, Serializable string)
897: {
898: write_instance(output, string, m_StringValueHelper.get_id(),
899: m_StringValueHelper);
900: }
901:
902:
913: static void write_instance(OutputStream output, Serializable value,
914: Object ids, BoxedValueHelper helper)
915: {
916: gnuValueStream rout = null;
917: gnuRuntime runtime = null;
918:
919: try
920: {
921: if (output instanceof gnuValueStream)
922: {
923: int position;
924: rout = (gnuValueStream) output;
925: runtime = rout.getRunTime();
926:
927: if (runtime == null)
928: {
929: runtime = new gnuRuntime(null, value);
930: rout.setRunTime(runtime);
931: rout.getRunTime().objectWritten(value,
932: position = rout.getPosition());
933: }
934: else if (runtime.target == value)
935: {
936: if (!writeSelf(output, value))
937: throw new InternalError("Recursive helper call for "
938: + value.getClass().getName());
939: return;
940: }
941: else
942: {
943: position = runtime.isWrittenAt(value);
944: if (position >= 0)
945: {
946:
947: output.write_long(vt_INDIRECTION);
948: output.write_long(position - rout.getPosition());
949:
950: return;
951: }
952: else
953: {
954: runtime.objectWritten(value, position = rout.getPosition());
955: }
956: }
957: }
958:
959: int value_tag = vt_VALUE_TAG;
960:
961: if (ids instanceof String)
962: value_tag |= vf_ID;
963: else if (ids instanceof String[])
964:
965: value_tag |= vf_MULTIPLE_IDS | vf_ID;
966:
967: int chunkSizeLocation;
968:
969: OutputStream outObj;
970:
971: if (USE_CHUNKING)
972: {
973:
974:
975: outObj = output;
976: value_tag |= vf_CHUNKING;
977: }
978: else
979: outObj = output;
980:
981: output.write_long(value_tag);
982:
983: if ((value_tag & vf_MULTIPLE_IDS) != 0)
984: write_string_array(output, (String[]) ids);
985: else if ((value_tag & vf_ID) != 0)
986: write_string(output, (String) ids);
987:
988: if (USE_CHUNKING)
989: {
990:
991:
992: output.write_long(0x55555555);
993:
994: chunkSizeLocation = rout.getPosition() - INT_SIZE;
995: }
996: else
997:
998: chunkSizeLocation = -1;
999:
1000: writeValue(outObj, value, helper);
1001:
1002: if (USE_CHUNKING)
1003: {
1004:
1005: int chunkSize = rout.getPosition() - chunkSizeLocation - INT_SIZE;
1006: int current = rout.getPosition();
1007: rout.seek(chunkSizeLocation);
1008: output.write_long(chunkSize);
1009: rout.seek(current);
1010:
1011:
1012: output.write_long(-1);
1013: }
1014: }
1015: finally
1016: {
1017: if (runtime != null)
1018: runtime.target = null;
1019: }
1020: }
1021:
1022:
1025: static void writeValue(OutputStream output, Serializable value,
1026: BoxedValueHelper helper)
1027: {
1028: ((gnuValueStream) output).getRunTime().target = value;
1029: if (helper != null)
1030: helper.write_value(output, value);
1031: else if (!writeSelf(output, value))
1032: {
1033:
1034: boolean ok = false;
1035:
1036: if (!ok)
1037: {
1038: if (output instanceof BufferedCdrOutput)
1039: {
1040: BufferedCdrOutput b = (BufferedCdrOutput) output;
1041: if (b.runtime == null)
1042: b.runtime = new gnuRuntime(null, value);
1043: }
1044:
1045: handler.writeValue(output, value);
1046: }
1047: }
1048: }
1049:
1050:
1054: static boolean writeSelf(OutputStream output, Serializable value)
1055: {
1056:
1057: if (value instanceof CustomMarshal)
1058: {
1059: ((CustomMarshal) value).marshal((DataOutputStream) output);
1060: return true;
1061: }
1062: else if (value instanceof Streamable)
1063: {
1064: ((Streamable) value)._write(output);
1065: return true;
1066: }
1067: return false;
1068: }
1069:
1070:
1076: static Serializable readIndirection(InputStream an_input)
1077: {
1078: if (!(an_input instanceof gnuValueStream))
1079: throw new NO_IMPLEMENT(gnuValueStream.class.getName()
1080: + " expected as parameter");
1081:
1082: gnuValueStream in = (gnuValueStream) an_input;
1083:
1084: int current_pos = in.getPosition();
1085:
1086: int offset = an_input.read_long();
1087: if (offset > -INT_SIZE)
1088: {
1089: MARSHAL m = new MARSHAL("Indirection tag refers to " + offset
1090: + " (must be less than -" + INT_SIZE + ")");
1091: m.minor = Minor.Offset;
1092: throw m;
1093: }
1094:
1095: int stored_at = current_pos + offset;
1096:
1097: if (in.getRunTime() == null)
1098: {
1099: MARSHAL m = new MARSHAL(stored_at + " offset " + offset + ": not written");
1100: m.minor = Minor.Value;
1101: throw m;
1102: }
1103:
1104: return (Serializable) in.getRunTime().isObjectWrittenAt(stored_at, offset);
1105: }
1106:
1107:
1114: static void checkTag(int value_tag)
1115: {
1116: if ((value_tag < 0x7fffff00 || value_tag > 0x7fffffff)
1117: && value_tag != vt_NULL && value_tag != vt_INDIRECTION)
1118: {
1119: MARSHAL m = new MARSHAL("Invalid value record, unsupported header tag: "
1120: + value_tag + " (0x" + Integer.toHexString(value_tag) + ")");
1121: m.minor = Minor.ValueHeaderTag;
1122: throw m;
1123: }
1124:
1125: if ((value_tag & vf_MULTIPLE_IDS) != 0 && (value_tag & vf_ID) == 0)
1126: {
1127: MARSHAL m = new MARSHAL("Invalid value record header flag combination (0x"
1128: + Integer.toHexString(value_tag) + ")");
1129: m.minor = Minor.ValueHeaderFlags;
1130: throw m;
1131: }
1132: }
1133:
1134:
1137: static void throwIt(String msg, String id1, String id2, Throwable e)
1138: throws MARSHAL
1139: {
1140: MARSHAL m = new MARSHAL(msg + ":'" + id1 + "' versus '" + id2 + "'");
1141: if (e != null)
1142: m.initCause(e);
1143: m.minor = Minor.Value;
1144: throw m;
1145: }
1146:
1147:
1150: static Object createInstance(String id, String[] ids, String codebase)
1151: {
1152: Object o = null;
1153:
1154: if (id != null)
1155: o = _createInstance(id, codebase);
1156:
1157: if (ids != null)
1158: for (int i = 0; i < ids.length && o == null; i++)
1159: o = _createInstance(ids[i], codebase);
1160: return o;
1161: }
1162:
1163: static Object _createInstance(String id, String codebase)
1164: {
1165: if (id == null)
1166: return null;
1167: if (id.equals(StringValueHelper.id()))
1168: return "";
1169: StringTokenizer st = new StringTokenizer(id, ":");
1170:
1171: String prefix = st.nextToken();
1172: if (prefix.equalsIgnoreCase("IDL"))
1173: return ObjectCreator.Idl2Object(id);
1174: else if (prefix.equalsIgnoreCase("RMI"))
1175: {
1176: String className = st.nextToken();
1177: String hashCode = st.nextToken();
1178: String sid = null;
1179: if (st.hasMoreElements())
1180: sid = st.nextToken();
1181:
1182: try
1183: {
1184: Class objectClass = Util.loadClass(className, codebase,
1185: Vio.class.getClassLoader());
1186:
1187: String rid = ObjectCreator.getRepositoryId(objectClass);
1188:
1189: if (!rid.equals(id))
1190: {
1191:
1192: StringTokenizer st2 = new StringTokenizer(rid, ":");
1193: if (!st2.nextToken().equals("RMI"))
1194: throw new InternalError("RMI format expected: '" + rid + "'");
1195: if (!st2.nextToken().equals(className))
1196: throwIt("Class name mismatch", id, rid, null);
1197:
1198: try
1199: {
1200: long h1 = Long.parseLong(hashCode, 16);
1201: long h2 = Long.parseLong(st2.nextToken(), 16);
1202: if (h1 != h2)
1203: throwIt("Hashcode mismatch", id, rid, null);
1204:
1205: if (sid != null && st2.hasMoreTokens())
1206: {
1207: long s1 = Long.parseLong(hashCode, 16);
1208: long s2 = Long.parseLong(st2.nextToken(), 16);
1209: if (s1 != s2)
1210: throwIt("serialVersionUID mismatch", id, rid, null);
1211: }
1212: }
1213: catch (NumberFormatException e)
1214: {
1215: throwIt("Invalid hashcode or svuid format: ", id, rid, e);
1216: }
1217: }
1218:
1219:
1220: return instantiateAnyWay(objectClass);
1221: }
1222: catch (Exception ex)
1223: {
1224: MARSHAL m = new MARSHAL("Unable to instantiate " + id);
1225: m.minor = Minor.Instantiation;
1226: m.initCause(ex);
1227: throw m;
1228: }
1229: }
1230: else
1231: throw new NO_IMPLEMENT("Unsupported prefix " + prefix + ":");
1232: }
1233:
1234:
1237: static String read_string(InputStream input)
1238: {
1239: gnuValueStream g = (gnuValueStream) input;
1240: int previous = g.getPosition();
1241: int l = input.read_long();
1242: if (l != vt_INDIRECTION)
1243: {
1244: g.seek(previous);
1245: String s = input.read_string();
1246: if (g.getRunTime() == null)
1247: g.setRunTime(new gnuRuntime(null, null));
1248: g.getRunTime().singleIdWritten(s, previous);
1249: return s;
1250: }
1251: else
1252: {
1253: gnuRuntime r = g.getRunTime();
1254: int base = g.getPosition();
1255: int delta = input.read_long();
1256: if (r == null)
1257: {
1258: previous = g.getPosition();
1259: g.seek(base + delta);
1260: String indir = input.read_string();
1261: g.seek(previous);
1262: return indir;
1263: }
1264: else
1265: {
1266: return (String) r.isObjectWrittenAt(base + delta, delta);
1267: }
1268: }
1269: }
1270:
1271:
1274: static String[] read_string_array(InputStream input)
1275: {
1276: gnuValueStream g = (gnuValueStream) input;
1277: int previous = g.getPosition();
1278: int l = input.read_long();
1279: if (l != vt_INDIRECTION)
1280: {
1281: g.seek(previous);
1282: String[] s = StringSeqHelper.read(input);
1283: if (g.getRunTime() == null)
1284: g.setRunTime(new gnuRuntime(null, null));
1285: g.getRunTime().objectWritten(s, previous);
1286: return s;
1287: }
1288: else
1289: {
1290: gnuRuntime r = g.getRunTime();
1291: int base = g.getPosition();
1292: int delta = input.read_long();
1293: if (r == null)
1294: {
1295: previous = g.getPosition();
1296: g.seek(base + delta);
1297: String[] indir = StringSeqHelper.read(input);
1298: g.seek(previous);
1299: return indir;
1300: }
1301: else
1302: {
1303: return (String[]) r.isObjectWrittenAt(base + delta, delta);
1304: }
1305: }
1306: }
1307:
1308:
1311: static void write_string(OutputStream output, String id)
1312: {
1313: if (output instanceof gnuValueStream)
1314: {
1315: gnuValueStream b = (gnuValueStream) output;
1316: if (b != null)
1317: {
1318: int written = b.getRunTime().idWrittenAt(id);
1319: if (written >= 0)
1320: {
1321:
1322: output.write_long(vt_INDIRECTION);
1323: int p = b.getPosition();
1324: output.write_long(written - p);
1325: }
1326: else
1327: {
1328: b.getRunTime().singleIdWritten(id, b.getPosition());
1329: output.write_string(id);
1330: }
1331: }
1332: }
1333: else
1334: output.write_string(id);
1335: }
1336:
1337:
1340: static void write_string_array(OutputStream output, String[] ids)
1341: {
1342: if (output instanceof gnuValueStream)
1343: {
1344: gnuValueStream b = (gnuValueStream) output;
1345: if (b != null)
1346: {
1347: int written = b.getRunTime().idWrittenAt(ids);
1348: if (written >= 0)
1349: {
1350:
1351: output.write_long(vt_INDIRECTION);
1352: int p = b.getPosition();
1353: output.write_long(written - p);
1354: }
1355: else
1356: {
1357: b.getRunTime().multipleIdsWritten(ids, b.getPosition());
1358: StringSeqHelper.write(output, ids);
1359: }
1360: }
1361: }
1362: else
1363: StringSeqHelper.write(output, ids);
1364: }
1365:
1366:
1370: public static BoxedValueHelper getHelper(Class x, Object ids)
1371: {
1372: if (x != null && x.equals(String.class))
1373: return m_StringValueHelper;
1374: else if (x != null && x.isArray())
1375: return new ArrayValueHelper(x);
1376: else if (ids instanceof String)
1377: return locateHelper((String) ids);
1378: else if (ids instanceof String[])
1379: {
1380: String[] ia = (String[]) ids;
1381: BoxedValueHelper h;
1382: for (int i = 0; i < ia.length; i++)
1383: {
1384: h = locateHelper(ia[i]);
1385: if (h != null)
1386: return h;
1387: }
1388: return null;
1389: }
1390: else
1391: return null;
1392: }
1393:
1394:
1398: public static BoxedValueHelper getHelper(Class x, String id)
1399: {
1400: if (x != null && x.equals(String.class))
1401: return m_StringValueHelper;
1402: else if (x != null && x.isArray())
1403: return new ArrayValueHelper(x);
1404: else
1405: return locateHelper(id);
1406: }
1407:
1408:
1411: static BoxedValueHelper locateHelper(String id)
1412: {
1413: if (id != null)
1414: {
1415: if (id.equals(m_StringValueHelper.get_id()))
1416: return m_StringValueHelper;
1417: else
1418:
1419: if (id.startsWith("IDL:"))
1420: {
1421: try
1422: {
1423: Class helperClass = ObjectCreator.findHelper(id);
1424: if (BoxedValueHelper.class.isAssignableFrom(helperClass))
1425: return (BoxedValueHelper) helperClass.newInstance();
1426: else if (helperClass != null)
1427: return new IDLTypeHelper(helperClass);
1428: else
1429: return null;
1430: }
1431: catch (Exception ex)
1432: {
1433: return null;
1434: }
1435: }
1436: }
1437: return null;
1438: }
1439:
1440:
1443: static int getCurrentPosition(InputStream x)
1444: {
1445: if (x instanceof gnuValueStream)
1446: return ((gnuValueStream) x).getPosition();
1447: else
1448: return 0;
1449: }
1450:
1451:
1458: public static Object instantiateAnyWay(Class clazz)
1459: throws Exception
1460: {
1461: Class first_nonserial = clazz;
1462:
1463: while (Serializable.class.isAssignableFrom(first_nonserial)
1464: || Modifier.isAbstract(first_nonserial.getModifiers()))
1465: first_nonserial = first_nonserial.getSuperclass();
1466:
1467: final Class local_constructor_class = first_nonserial;
1468:
1469: Constructor constructor = local_constructor_class.getDeclaredConstructor(new Class[0]);
1470:
1471: return VMVio.allocateObject(clazz, constructor.getDeclaringClass(),
1472: constructor);
1473: }
1474: }