1. /*
  2. * @(#)AnyImpl.java 1.61 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.internal.corba;
  16. import org.omg.CORBA.*;
  17. import org.omg.CORBA.portable.Streamable;
  18. import org.omg.CORBA.portable.ObjectImpl;
  19. import org.omg.CORBA.portable.InputStream;
  20. import org.omg.CORBA.portable.OutputStream;
  21. import org.omg.CORBA.TypeCodePackage.BadKind;
  22. import org.omg.CORBA.TypeCodePackage.Bounds;
  23. import com.sun.corba.se.internal.core.*;
  24. import com.sun.corba.se.internal.iiop.CDRInputStream;
  25. import com.sun.corba.se.internal.orbutil.RepositoryIdFactory;
  26. import com.sun.corba.se.internal.orbutil.RepositoryIdStrings;
  27. import com.sun.corba.se.internal.orbutil.MinorCodes;
  28. import com.sun.corba.se.internal.orbutil.ORBUtility;
  29. import com.sun.corba.se.internal.io.ValueUtility;
  30. import java.io.Serializable;
  31. import java.math.BigDecimal;
  32. //import test.Debug;
  33. final class AnyInputStream extends EncapsInputStream {
  34. AnyInputStream(EncapsInputStream theStream ) {
  35. super( theStream );
  36. }
  37. }
  38. final class AnyOutputStream extends EncapsOutputStream {
  39. public AnyOutputStream(org.omg.CORBA.ORB orb) {
  40. super(orb);
  41. }
  42. public AnyOutputStream(org.omg.CORBA.ORB orb, int size) {
  43. super(orb, size);
  44. }
  45. public org.omg.CORBA.portable.InputStream create_input_stream() {
  46. return new AnyInputStream((com.sun.corba.se.internal.corba.EncapsInputStream)super.create_input_stream());
  47. }
  48. }
  49. // subclasses must provide a matching helper class
  50. public class AnyImpl extends Any
  51. {
  52. //
  53. // Always valid.
  54. //
  55. private TypeCodeImpl typeCode;
  56. protected org.omg.CORBA.ORB orb;
  57. //
  58. // Validity depends upon typecode. The 'value' and 'object' instance
  59. // members are used to hold immutable types as defined by the
  60. // isStreamed[] table below. Otherwise, 'stream' is non-null and
  61. // holds the value in CDR marshaled format. As an optimization, the
  62. // stream type is an Any extension of CDR stream that is used to
  63. // detect an optimization in read_value().
  64. //
  65. private CDRInputStream stream;
  66. private long value;
  67. private java.lang.Object object;
  68. // Setting the typecode via the type() accessor wipes out the value.
  69. // An attempt to extract before the value is set will result
  70. // in a BAD_OPERATION exception being raised.
  71. private boolean isInitialized = false;
  72. private static final int DEFAULT_BUFFER_SIZE = 32;
  73. /*
  74. * This boolean array tells us if a given typecode must be
  75. * streamed. Objects that are immutable don't have to be streamed.
  76. */
  77. static boolean isStreamed[] = {
  78. false, // null
  79. false, // void
  80. false, // short
  81. false, // long
  82. false, // ushort
  83. false, // ulong
  84. false, // float
  85. false, // double
  86. false, // boolean
  87. false, // char
  88. false, // octet
  89. false, // any
  90. false, // TypeCode
  91. true, // Principal
  92. false, // objref
  93. true, // struct
  94. true, // union
  95. false, // enum
  96. false, // string
  97. true, // sequence
  98. true, // array
  99. true, // alias
  100. true, // except
  101. false, // longlong
  102. false, // ulonglong
  103. false, // longdouble
  104. false, // wchar
  105. false, // wstring
  106. false, // fixed
  107. false, // value
  108. false, // value_box (used to be true)
  109. false, // native
  110. false // abstract interface
  111. };
  112. static AnyImpl convertToNative(org.omg.CORBA.ORB orb, Any any) {
  113. if (any instanceof AnyImpl) {
  114. return (AnyImpl)any;
  115. } else {
  116. AnyImpl anyImpl = new AnyImpl(orb, any);
  117. anyImpl.typeCode = TypeCodeImpl.convertToNative(orb, anyImpl.typeCode);
  118. return anyImpl;
  119. }
  120. }
  121. ///////////////////////////////////////////////////////////////////////////
  122. // Constructors
  123. /**
  124. * A constructor that sets the Any to contain a null. It also marks
  125. * the value as being invalid so that extractions throw an exception
  126. * until an insertion has been performed.
  127. */
  128. //private Debug debug = new Debug ("AnyImpl ");
  129. public AnyImpl(org.omg.CORBA.ORB orb)
  130. {
  131. this.orb = orb;
  132. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_null);
  133. stream = null;
  134. object = null;
  135. value = 0;
  136. // null is a valid value
  137. isInitialized = true;
  138. }
  139. //
  140. // Create a new AnyImpl which is a copy of obj.
  141. //
  142. public AnyImpl(org.omg.CORBA.ORB orb, Any obj) {
  143. this(orb);
  144. if ((obj instanceof AnyImpl)) {
  145. AnyImpl objImpl = (AnyImpl)obj;
  146. typeCode = objImpl.typeCode;
  147. value = objImpl.value;
  148. object = objImpl.object;
  149. isInitialized = objImpl.isInitialized;
  150. if (objImpl.stream != null)
  151. stream = objImpl.stream.dup();
  152. } else {
  153. read_value(obj.create_input_stream(), obj.type());
  154. }
  155. }
  156. ///////////////////////////////////////////////////////////////////////////
  157. // basic accessors
  158. /**
  159. * returns the type of the element contained in the Any.
  160. *
  161. * @result the TypeCode for the element in the Any
  162. */
  163. public TypeCode type() {
  164. return typeCode;
  165. }
  166. private TypeCode realType() {
  167. return realType(typeCode);
  168. }
  169. private TypeCode realType(TypeCode aType) {
  170. TypeCode realType = aType;
  171. try {
  172. // Note: Indirect types are handled in kind() method
  173. while (realType.kind().value() == TCKind._tk_alias) {
  174. realType = realType.content_type();
  175. }
  176. } catch (BadKind bad) { // impossible
  177. }
  178. return realType;
  179. }
  180. /**
  181. * sets the type of the element to be contained in the Any.
  182. *
  183. * @param tc the TypeCode for the element in the Any
  184. */
  185. public void type(TypeCode tc)
  186. {
  187. //debug.log ("type2");
  188. // set the typecode
  189. typeCode = TypeCodeImpl.convertToNative(orb, tc);
  190. stream = null;
  191. value = 0;
  192. object = null;
  193. // null is the only legal value this Any can have after resetting the type code
  194. isInitialized = (tc.kind().value() == TCKind._tk_null);
  195. }
  196. /**
  197. * checks for equality between Anys.
  198. *
  199. * @param otherAny the Any to be compared with.
  200. * @result true if the Anys are equal, false otherwise.
  201. */
  202. public boolean equal(Any otherAny)
  203. {
  204. //debug.log ("equal");
  205. if (otherAny == this)
  206. return true;
  207. // first check for typecode equality.
  208. // note that this will take aliases into account
  209. if (!typeCode.equal(otherAny.type()))
  210. return false;
  211. // Resolve aliases here
  212. TypeCode realType = realType();
  213. // _REVISIT_ Possible optimization for the case where
  214. // otherAny is a AnyImpl and the endianesses match.
  215. // Need implementation of CDRInputStream.equals()
  216. // For now we disable this to encourage testing the generic,
  217. // unoptimized code below.
  218. // Unfortunately this generic code needs to copy the whole stream
  219. // at least once.
  220. // if (AnyImpl.isStreamed[realType.kind().value()]) {
  221. // if (otherAny instanceof AnyImpl) {
  222. // return ((AnyImpl)otherAny).stream.equals(stream);
  223. // }
  224. // }
  225. switch (realType.kind().value()) {
  226. // handle primitive types
  227. case TCKind._tk_null:
  228. case TCKind._tk_void:
  229. return true;
  230. case TCKind._tk_short:
  231. return (extract_short() == otherAny.extract_short());
  232. case TCKind._tk_long:
  233. return (extract_long() == otherAny.extract_long());
  234. case TCKind._tk_ushort:
  235. return (extract_ushort() == otherAny.extract_ushort());
  236. case TCKind._tk_ulong:
  237. return (extract_ulong() == otherAny.extract_ulong());
  238. case TCKind._tk_float:
  239. return (extract_float() == otherAny.extract_float());
  240. case TCKind._tk_double:
  241. return (extract_double() == otherAny.extract_double());
  242. case TCKind._tk_boolean:
  243. return (extract_boolean() == otherAny.extract_boolean());
  244. case TCKind._tk_char:
  245. return (extract_char() == otherAny.extract_char());
  246. case TCKind._tk_wchar:
  247. return (extract_wchar() == otherAny.extract_wchar());
  248. case TCKind._tk_octet:
  249. return (extract_octet() == otherAny.extract_octet());
  250. case TCKind._tk_any:
  251. return extract_any().equal(otherAny.extract_any());
  252. case TCKind._tk_TypeCode:
  253. return extract_TypeCode().equal(otherAny.extract_TypeCode());
  254. case TCKind._tk_string:
  255. return extract_string().equals(otherAny.extract_string());
  256. case TCKind._tk_wstring:
  257. return (extract_wstring().equals(otherAny.extract_wstring()));
  258. case TCKind._tk_longlong:
  259. return (extract_longlong() == otherAny.extract_longlong());
  260. case TCKind._tk_ulonglong:
  261. return (extract_ulonglong() == otherAny.extract_ulonglong());
  262. case TCKind._tk_objref:
  263. return (extract_Object().equals(otherAny.extract_Object()));
  264. case TCKind._tk_Principal:
  265. return (extract_Principal().equals(otherAny.extract_Principal()));
  266. case TCKind._tk_enum:
  267. return (extract_long() == otherAny.extract_long());
  268. case TCKind._tk_fixed:
  269. return (extract_fixed().compareTo(otherAny.extract_fixed()) == 0);
  270. case TCKind._tk_except:
  271. case TCKind._tk_struct:
  272. case TCKind._tk_union:
  273. case TCKind._tk_sequence:
  274. case TCKind._tk_array:
  275. InputStream copyOfMyStream = this.create_input_stream();
  276. InputStream copyOfOtherStream = otherAny.create_input_stream();
  277. return equalMember(realType, copyOfMyStream, copyOfOtherStream);
  278. // Too complicated to handle value types the way we handle
  279. // other complex types above. Don't try to decompose it here
  280. // for faster comparison, just use Object.equals().
  281. case TCKind._tk_value:
  282. case TCKind._tk_value_box:
  283. return extract_Value().equals(otherAny.extract_Value());
  284. case TCKind._tk_alias:
  285. // error resolving alias above
  286. throw new org.omg.CORBA.INTERNAL();
  287. case TCKind._tk_longdouble:
  288. // Unspecified for Java
  289. throw new org.omg.CORBA.NO_IMPLEMENT();
  290. default:
  291. throw new org.omg.CORBA.NO_IMPLEMENT();
  292. }
  293. }
  294. // Needed for equal() in order to achieve linear performance for complex types.
  295. // Uses up (recursively) copies of the InputStream in both Anys that got created in equal().
  296. private boolean equalMember(TypeCode memberType, InputStream myStream, InputStream otherStream) {
  297. // Resolve aliases here
  298. TypeCode realType = realType(memberType);
  299. try {
  300. switch (realType.kind().value()) {
  301. // handle primitive types
  302. case TCKind._tk_null:
  303. case TCKind._tk_void:
  304. return true;
  305. case TCKind._tk_short:
  306. return (myStream.read_short() == otherStream.read_short());
  307. case TCKind._tk_long:
  308. return (myStream.read_long() == otherStream.read_long());
  309. case TCKind._tk_ushort:
  310. return (myStream.read_ushort() == otherStream.read_ushort());
  311. case TCKind._tk_ulong:
  312. return (myStream.read_ulong() == otherStream.read_ulong());
  313. case TCKind._tk_float:
  314. return (myStream.read_float() == otherStream.read_float());
  315. case TCKind._tk_double:
  316. return (myStream.read_double() == otherStream.read_double());
  317. case TCKind._tk_boolean:
  318. return (myStream.read_boolean() == otherStream.read_boolean());
  319. case TCKind._tk_char:
  320. return (myStream.read_char() == otherStream.read_char());
  321. case TCKind._tk_wchar:
  322. return (myStream.read_wchar() == otherStream.read_wchar());
  323. case TCKind._tk_octet:
  324. return (myStream.read_octet() == otherStream.read_octet());
  325. case TCKind._tk_any:
  326. return myStream.read_any().equal(otherStream.read_any());
  327. case TCKind._tk_TypeCode:
  328. return myStream.read_TypeCode().equal(otherStream.read_TypeCode());
  329. case TCKind._tk_string:
  330. return myStream.read_string().equals(otherStream.read_string());
  331. case TCKind._tk_wstring:
  332. return (myStream.read_wstring().equals(otherStream.read_wstring()));
  333. case TCKind._tk_longlong:
  334. return (myStream.read_longlong() == otherStream.read_longlong());
  335. case TCKind._tk_ulonglong:
  336. return (myStream.read_ulonglong() == otherStream.read_ulonglong());
  337. case TCKind._tk_objref:
  338. return (myStream.read_Object().equals(otherStream.read_Object()));
  339. case TCKind._tk_Principal:
  340. return (myStream.read_Principal().equals(otherStream.read_Principal()));
  341. case TCKind._tk_enum:
  342. return (myStream.read_long() == otherStream.read_long());
  343. case TCKind._tk_fixed:
  344. return (myStream.read_fixed().compareTo(otherStream.read_fixed()) == 0);
  345. case TCKind._tk_except:
  346. case TCKind._tk_struct: {
  347. int length = realType.member_count();
  348. for (int i=0; i<length; i++) {
  349. if ( ! equalMember(realType.member_type(i), myStream, otherStream)) {
  350. return false;
  351. }
  352. }
  353. return true;
  354. }
  355. case TCKind._tk_union: {
  356. Any myDiscriminator = orb.create_any();
  357. Any otherDiscriminator = orb.create_any();
  358. myDiscriminator.read_value(myStream, realType.discriminator_type());
  359. otherDiscriminator.read_value(otherStream, realType.discriminator_type());
  360. if ( ! myDiscriminator.equal(otherDiscriminator)) {
  361. return false;
  362. }
  363. TypeCodeImpl realTypeCodeImpl = TypeCodeImpl.convertToNative(orb, realType);
  364. int memberIndex = realTypeCodeImpl.currentUnionMemberIndex(myDiscriminator);
  365. if (memberIndex == -1)
  366. throw new MARSHAL();
  367. if ( ! equalMember(realType.member_type(memberIndex), myStream, otherStream)) {
  368. return false;
  369. }
  370. return true;
  371. }
  372. case TCKind._tk_sequence: {
  373. int length = myStream.read_long();
  374. otherStream.read_long(); // just so that the two stream are in sync
  375. for (int i=0; i<length; i++) {
  376. if ( ! equalMember(realType.content_type(), myStream, otherStream)) {
  377. return false;
  378. }
  379. }
  380. return true;
  381. }
  382. case TCKind._tk_array: {
  383. int length = realType.member_count();
  384. for (int i=0; i<length; i++) {
  385. if ( ! equalMember(realType.content_type(), myStream, otherStream)) {
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. // Too complicated to handle value types the way we handle
  392. // other complex types above. Don't try to decompose it here
  393. // for faster comparison, just use Object.equals().
  394. case TCKind._tk_value:
  395. case TCKind._tk_value_box:
  396. org.omg.CORBA_2_3.portable.InputStream mine =
  397. (org.omg.CORBA_2_3.portable.InputStream)myStream;
  398. org.omg.CORBA_2_3.portable.InputStream other =
  399. (org.omg.CORBA_2_3.portable.InputStream)otherStream;
  400. return mine.read_value().equals(other.read_value());
  401. case TCKind._tk_alias:
  402. // error resolving alias above
  403. throw new org.omg.CORBA.INTERNAL();
  404. case TCKind._tk_longdouble:
  405. // Unspecified for Java
  406. throw new org.omg.CORBA.NO_IMPLEMENT();
  407. default:
  408. throw new org.omg.CORBA.NO_IMPLEMENT();
  409. }
  410. } catch (BadKind badKind) { // impossible
  411. } catch (Bounds bounds) { // impossible
  412. }
  413. return false;
  414. }
  415. ///////////////////////////////////////////////////////////////////////////
  416. // accessors for marshaling/unmarshaling
  417. /**
  418. * returns an output stream that an Any value can be marshaled into.
  419. *
  420. * @result the OutputStream to marshal value of Any into
  421. */
  422. public org.omg.CORBA.portable.OutputStream create_output_stream()
  423. {
  424. //debug.log ("create_output_stream");
  425. return new AnyOutputStream(orb, DEFAULT_BUFFER_SIZE);
  426. }
  427. /**
  428. * returns an input stream that an Any value can be marshaled out of.
  429. *
  430. * @result the InputStream to marshal value of Any out of.
  431. */
  432. //
  433. // We create a new InputStream so that multiple threads can call here
  434. // and read the streams in parallel without thread safety problems.
  435. //
  436. public org.omg.CORBA.portable.InputStream create_input_stream()
  437. {
  438. //debug.log ("create_input_stream");
  439. if (AnyImpl.isStreamed[realType().kind().value()]) {
  440. return stream.dup();
  441. } else {
  442. OutputStream os = (OutputStream)orb.create_output_stream();
  443. TCUtility.marshalIn(os, realType(), value, object);
  444. return os.create_input_stream();
  445. }
  446. }
  447. ///////////////////////////////////////////////////////////////////////////
  448. // marshaling/unmarshaling routines
  449. //
  450. // If the InputStream is a CDRInputStream then we can copy the bytes
  451. // since it is in our format and does not have alignment issues.
  452. //
  453. public void read_value(org.omg.CORBA.portable.InputStream in, TypeCode tc)
  454. {
  455. //debug.log ("read_value");
  456. //
  457. // Assume that someone isn't going to think they can keep reading
  458. // from this stream after calling us. That would be likely for
  459. // an IIOPInputStream but if it is an AnyInputStream then they
  460. // presumably obtained it via our create_output_stream() so they could
  461. // write the contents of an IDL data type to it and then call
  462. // create_input_stream() for us to read it. This is how Helper classes
  463. // typically implement the insert() method.
  464. // We should probably document this behavior in the 1.1 revision
  465. // task force.
  466. //
  467. typeCode = TypeCodeImpl.convertToNative(orb, tc);
  468. int kind = realType().kind().value();
  469. if (kind >= isStreamed.length) {
  470. throw new INTERNAL("Unknown isStreamed kind value: " + kind,
  471. MinorCodes.INVALID_ISSTREAMED_TCKIND,
  472. CompletionStatus.COMPLETED_MAYBE);
  473. }
  474. if (AnyImpl.isStreamed[kind]) {
  475. if ( in instanceof AnyInputStream ) {
  476. // could only have been created here
  477. stream = (CDRInputStream)in;
  478. } else {
  479. org.omg.CORBA_2_3.portable.OutputStream out =
  480. (org.omg.CORBA_2_3.portable.OutputStream)orb.create_output_stream();
  481. typeCode.copy((org.omg.CORBA_2_3.portable.InputStream)in, out);
  482. stream = (CDRInputStream)out.create_input_stream();
  483. }
  484. } else {
  485. java.lang.Object[] objholder = new java.lang.Object[1];
  486. objholder[0] = object;
  487. long[] longholder = new long[1];
  488. TCUtility.unmarshalIn(in, typeCode, longholder, objholder);
  489. value = longholder[0];
  490. object = objholder[0];
  491. stream = null;
  492. }
  493. isInitialized = true;
  494. }
  495. //
  496. // We could optimize this by noticing whether the target stream
  497. // has ever had anything marshaled on it that required an
  498. // alignment of greater than 4 (was write_double() ever called on it).
  499. // If not, then we can just do a byte array copy without having to
  500. // drive the remarshaling through typecode interpretation.
  501. //
  502. public void write_value(OutputStream out)
  503. {
  504. //debug.log ("write_value");
  505. if (AnyImpl.isStreamed[realType().kind().value()]) {
  506. typeCode.copy(stream.dup(), out);
  507. } else {
  508. // _REVISIT_ check isInitialized whether all we write is TypeCode!
  509. TCUtility.marshalIn(out, realType(), value, object);
  510. }
  511. }
  512. /**
  513. * takes a streamable and inserts its reference into the any
  514. *
  515. * @param s the streamable to insert
  516. */
  517. public void insert_Streamable(Streamable s)
  518. {
  519. //debug.log ("insert_Streamable");
  520. typeCode = TypeCodeImpl.convertToNative(orb, s._type());
  521. object = s;
  522. isInitialized = true;
  523. }
  524. public Streamable extract_Streamable()
  525. {
  526. //debug.log( "extract_Streamable" ) ;
  527. return (Streamable)object;
  528. }
  529. ///////////////////////////////////////////////////////////////////////////
  530. // insertion/extraction/replacement for all basic types
  531. /**
  532. * See the description of the <a href="#anyOps">general Any operations.</a>
  533. */
  534. public void insert_short(short s)
  535. {
  536. //debug.log ("insert_short");
  537. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_short);
  538. value = s;
  539. isInitialized = true;
  540. }
  541. /**
  542. * See the description of the <a href="#anyOps">general Any operations.</a>
  543. */
  544. public short extract_short()
  545. {
  546. //debug.log ("extract_short");
  547. if (!isInitialized || !(realType().kind().value() == TCKind._tk_short))
  548. throw new BAD_OPERATION();
  549. return (short)value;
  550. }
  551. /**
  552. * See the description of the <a href="#anyOps">general Any operations.</a>
  553. */
  554. public void insert_long(int l)
  555. {
  556. //debug.log ("insert_long");
  557. // A long value is applicable to enums as well, so don't erase the enum type code
  558. // in case it was initialized that way before.
  559. int kind = realType().kind().value();
  560. if (kind != TCKind._tk_long && kind != TCKind._tk_enum) {
  561. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_long);
  562. }
  563. value = l;
  564. isInitialized = true;
  565. }
  566. /**
  567. * See the description of the <a href="#anyOps">general Any operations.</a>
  568. */
  569. public int extract_long()
  570. {
  571. //debug.log ("extract_long");
  572. int kind = realType().kind().value();
  573. if ( ! isInitialized) {
  574. throw new BAD_OPERATION();
  575. }
  576. if ( ! (kind == TCKind._tk_long || kind == TCKind._tk_enum))
  577. throw new BAD_OPERATION();
  578. return (int)value;
  579. }
  580. /**
  581. * See the description of the <a href="#anyOps">general Any operations.</a>
  582. */
  583. public void insert_ushort(short s)
  584. {
  585. //debug.log ("insert_ushort");
  586. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_ushort);
  587. value = s;
  588. isInitialized = true;
  589. }
  590. /**
  591. * See the description of the <a href="#anyOps">general Any operations.</a>
  592. */
  593. public short extract_ushort()
  594. {
  595. //debug.log ("extract_ushort");
  596. if (!isInitialized || !(realType().kind().value() == TCKind._tk_ushort))
  597. throw new BAD_OPERATION();
  598. return (short)value;
  599. }
  600. /**
  601. * See the description of the <a href="#anyOps">general Any operations.</a>
  602. */
  603. public void insert_ulong(int l)
  604. {
  605. //debug.log ("insert_ulong");
  606. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_ulong);
  607. value = l;
  608. isInitialized = true;
  609. }
  610. /**
  611. * See the description of the <a href="#anyOps">general Any operations.</a>
  612. */
  613. public int extract_ulong()
  614. {
  615. //debug.log ("extract_ulong");
  616. if (!isInitialized || !(realType().kind().value() == TCKind._tk_ulong))
  617. throw new BAD_OPERATION();
  618. return (int)value;
  619. }
  620. /**
  621. * See the description of the <a href="#anyOps">general Any operations.</a>
  622. */
  623. public void insert_float(float f)
  624. {
  625. //debug.log ("insert_float");
  626. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_float);
  627. value = Float.floatToIntBits(f);
  628. isInitialized = true;
  629. }
  630. /**
  631. * See the description of the <a href="#anyOps">general Any operations.</a>
  632. */
  633. public float extract_float()
  634. {
  635. //debug.log ("extract_float");
  636. if (!isInitialized || !(realType().kind().value() == TCKind._tk_float))
  637. throw new BAD_OPERATION();
  638. return Float.intBitsToFloat((int)value);
  639. }
  640. /**
  641. * See the description of the <a href="#anyOps">general Any operations.</a>
  642. */
  643. public void insert_double(double d)
  644. {
  645. //debug.log ("insert_double");
  646. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_double);
  647. value = Double.doubleToLongBits(d);
  648. isInitialized = true;
  649. }
  650. /**
  651. * See the description of the <a href="#anyOps">general Any operations.</a>
  652. */
  653. public double extract_double()
  654. {
  655. //debug.log ("extract_double");
  656. if (!isInitialized || !(realType().kind().value() == TCKind._tk_double))
  657. throw new BAD_OPERATION();
  658. return Double.longBitsToDouble(value);
  659. }
  660. /**
  661. * See the description of the <a href="#anyOps">general Any operations.</a>
  662. */
  663. public void insert_longlong(long l)
  664. {
  665. //debug.log ("insert_longlong");
  666. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_longlong);
  667. value = l;
  668. isInitialized = true;
  669. }
  670. /**
  671. * See the description of the <a href="#anyOps">general Any operations.</a>
  672. */
  673. public long extract_longlong()
  674. {
  675. //debug.log ("extract_longlong");
  676. if (!isInitialized || !(realType().kind().value() == TCKind._tk_longlong))
  677. throw new BAD_OPERATION();
  678. return value;
  679. }
  680. /**
  681. * See the description of the <a href="#anyOps">general Any operations.</a>
  682. */
  683. public void insert_ulonglong(long l)
  684. {
  685. //debug.log ("insert_ulonglong");
  686. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_ulonglong);
  687. value = l;
  688. isInitialized = true;
  689. }
  690. /**
  691. * See the description of the <a href="#anyOps">general Any operations.</a>
  692. */
  693. public long extract_ulonglong()
  694. {
  695. //debug.log ("extract_ulonglong");
  696. if (!isInitialized || !(realType().kind().value() == TCKind._tk_ulonglong))
  697. throw new BAD_OPERATION();
  698. return value;
  699. }
  700. /**
  701. * See the description of the <a href="#anyOps">general Any operations.</a>
  702. */
  703. public void insert_boolean(boolean b)
  704. {
  705. //debug.log ("insert_boolean");
  706. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_boolean);
  707. value = (b)? 1:0;
  708. isInitialized = true;
  709. }
  710. /**
  711. * See the description of the <a href="#anyOps">general Any operations.</a>
  712. */
  713. public boolean extract_boolean()
  714. {
  715. //debug.log ("extract_boolean");
  716. if (!isInitialized || !(realType().kind().value() == TCKind._tk_boolean))
  717. throw new BAD_OPERATION();
  718. return (value == 0)? false: true;
  719. }
  720. /**
  721. * See the description of the <a href="#anyOps">general Any operations.</a>
  722. */
  723. public void insert_char(char c)
  724. {
  725. //debug.log ("insert_char");
  726. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_char);
  727. value = c;
  728. isInitialized = true;
  729. }
  730. /**
  731. * See the description of the <a href="#anyOps">general Any operations.</a>
  732. */
  733. public char extract_char()
  734. {
  735. //debug.log ("extract_char");
  736. if (!isInitialized || !(realType().kind().value() == TCKind._tk_char))
  737. throw new BAD_OPERATION();
  738. return (char)value;
  739. }
  740. /**
  741. * See the description of the <a href="#anyOps">general Any operations.</a>
  742. */
  743. public void insert_wchar(char c)
  744. {
  745. //debug.log ("insert_wchar");
  746. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_wchar);
  747. value = c;
  748. isInitialized = true;
  749. }
  750. /**
  751. * See the description of the <a href="#anyOps">general Any operations.</a>
  752. */
  753. public char extract_wchar()
  754. {
  755. //debug.log ("extract_wchar");
  756. if (!isInitialized || !(realType().kind().value() == TCKind._tk_wchar))
  757. throw new BAD_OPERATION();
  758. return (char)value;
  759. }
  760. /**
  761. * See the description of the <a href="#anyOps">general Any operations.</a>
  762. */
  763. public void insert_octet(byte b)
  764. {
  765. //debug.log ("insert_octet");
  766. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_octet);
  767. value = b;
  768. isInitialized = true;
  769. }
  770. /**
  771. * See the description of the <a href="#anyOps">general Any operations.</a>
  772. */
  773. public byte extract_octet()
  774. {
  775. //debug.log ("extract_octet");
  776. if (!isInitialized || !(realType().kind().value() == TCKind._tk_octet))
  777. throw new BAD_OPERATION();
  778. return (byte)value;
  779. }
  780. /**
  781. * See the description of the <a href="#anyOps">general Any operations.</a>
  782. */
  783. public void insert_string(String s)
  784. {
  785. //debug.log ("insert_string");
  786. // Make sure type code information for bounded strings is not erased
  787. if (typeCode.kind() == TCKind.tk_string) {
  788. int length = 0;
  789. try { length = typeCode.length(); } catch (BadKind bad) {}
  790. // Check if bounded strings length is not exceeded
  791. if (length != 0 && s != null && s.length() > length) {
  792. throw new DATA_CONVERSION();
  793. }
  794. } else {
  795. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_string);
  796. }
  797. object = s;
  798. isInitialized = true;
  799. }
  800. /**
  801. * See the description of the <a href="#anyOps">general Any operations.</a>
  802. */
  803. public String extract_string()
  804. {
  805. //debug.log ("extract_string");
  806. if (!isInitialized || !(realType().kind().value() == TCKind._tk_string))
  807. throw new BAD_OPERATION();
  808. return (String)object;
  809. }
  810. /**
  811. * See the description of the <a href="#anyOps">general Any operations.</a>
  812. */
  813. public void insert_wstring(String s)
  814. {
  815. //debug.log ("insert_wstring");
  816. // Make sure type code information for bounded strings is not erased
  817. if (typeCode.kind() == TCKind.tk_wstring) {
  818. int length = 0;
  819. try { length = typeCode.length(); } catch (BadKind bad) {}
  820. // Check if bounded strings length is not exceeded
  821. if (length != 0 && s != null && s.length() > length) {
  822. throw new DATA_CONVERSION();
  823. }
  824. } else {
  825. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_wstring);
  826. }
  827. object = s;
  828. isInitialized = true;
  829. }
  830. /**
  831. * See the description of the <a href="#anyOps">general Any operations.</a>
  832. */
  833. public String extract_wstring()
  834. {
  835. //debug.log ("extract_wstring");
  836. if (!isInitialized || !(realType().kind().value() == TCKind._tk_wstring))
  837. throw new BAD_OPERATION();
  838. return (String)object;
  839. }
  840. /**
  841. * See the description of the <a href="#anyOps">general Any operations.</a>
  842. */
  843. public void insert_any(Any a)
  844. {
  845. //debug.log ("insert_any");
  846. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_any);
  847. object = a;
  848. stream = null;
  849. isInitialized = true;
  850. }
  851. /**
  852. * See the description of the <a href="#anyOps">general Any operations.</a>
  853. */
  854. public Any extract_any()
  855. {
  856. //debug.log ("extract_any");
  857. if (!isInitialized || !(realType().kind().value() == TCKind._tk_any))
  858. throw new BAD_OPERATION();
  859. return (Any)object;
  860. }
  861. /**
  862. * See the description of the <a href="#anyOps">general Any operations.</a>
  863. */
  864. public void insert_Object(org.omg.CORBA.Object o)
  865. {
  866. //debug.log ("insert_Object");
  867. if ( o == null ) {
  868. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_objref);
  869. } else {
  870. if (o instanceof ObjectImpl) {
  871. ObjectImpl objImpl = (ObjectImpl)o;
  872. String[] ids = objImpl._ids();
  873. typeCode = new TypeCodeImpl(orb, TCKind._tk_objref, ids[0], "");
  874. } else {
  875. throw new BAD_PARAM("Attempted to insert non-ObjectImpl "
  876. + o.getClass().getName()
  877. + " into an Any",
  878. MinorCodes.BAD_INSERTOBJ_PARAM,
  879. CompletionStatus.COMPLETED_MAYBE);
  880. }
  881. }
  882. object = o;
  883. isInitialized = true;
  884. }
  885. /**
  886. * A variant of the insertion operation that takes a typecode
  887. * argument as well.
  888. */
  889. public void insert_Object(org.omg.CORBA.Object o, TypeCode tc)
  890. {
  891. //debug.log ("insert_Object2");
  892. try {
  893. if ( tc.id().equals("IDL:omg.org/CORBA/Object:1.0") || o._is_a(tc.id()) )
  894. {
  895. typeCode = TypeCodeImpl.convertToNative(orb, tc);
  896. object = o;
  897. }
  898. else {
  899. throw new BAD_OPERATION();
  900. }
  901. } catch ( Exception ex ) {
  902. throw new BAD_OPERATION();
  903. }
  904. isInitialized = true;
  905. }
  906. /**
  907. * See the description of the <a href="#anyOps">general Any operations.</a>
  908. */
  909. public org.omg.CORBA.Object extract_Object()
  910. {
  911. //debug.log ("extract_Object");
  912. if (!isInitialized)
  913. throw new BAD_OPERATION("Invalid operation on uninitialized Any");
  914. // Check if the object contained here is of the type in typeCode
  915. org.omg.CORBA.Object obj = null;
  916. try {
  917. obj = (org.omg.CORBA.Object) object;
  918. if (typeCode.id().equals("IDL:omg.org/CORBA/Object:1.0") || obj._is_a(typeCode.id())) {
  919. return obj;
  920. } else {
  921. throw new BAD_OPERATION();
  922. }
  923. } catch ( Exception ex ) {
  924. throw new BAD_OPERATION();
  925. }
  926. }
  927. /**
  928. * See the description of the <a href="#anyOps">general Any operations.</a>
  929. */
  930. public void insert_TypeCode(TypeCode tc)
  931. {
  932. //debug.log ("insert_TypeCode");
  933. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_TypeCode);
  934. object = tc;
  935. isInitialized = true;
  936. }
  937. /**
  938. * See the description of the <a href="#anyOps">general Any operations.</a>
  939. */
  940. public TypeCode extract_TypeCode()
  941. {
  942. //debug.log ("extract_TypeCode");
  943. if (!isInitialized || !(realType().kind().value() == TCKind._tk_TypeCode))
  944. throw new BAD_OPERATION();
  945. return (TypeCode)object;
  946. }
  947. /**
  948. * @deprecated
  949. */
  950. public void insert_Principal(Principal p)
  951. {
  952. typeCode = TypeCodeImpl.get_primitive_tc(TCKind.tk_Principal);
  953. object = p;
  954. isInitialized = true;
  955. }
  956. /**
  957. * @deprecated
  958. */
  959. public Principal extract_Principal()
  960. {
  961. if (!isInitialized || !(realType().kind().value() == TCKind._tk_Principal))
  962. throw new BAD_OPERATION();
  963. return (Principal)object;
  964. }
  965. /**
  966. * Note that the Serializable really should be an IDLEntity of
  967. * some kind. It shouldn't just be an RMI-IIOP type. Currently,
  968. * we accept and will produce RMI repIds with the latest
  969. * calculations if given a non-IDLEntity Serializable.
  970. */
  971. public Serializable extract_Value() throws org.omg.CORBA.BAD_OPERATION
  972. {
  973. //debug.log ("extract_Value");
  974. if (!isInitialized)
  975. throw new BAD_OPERATION("Invalid operation on uninitialized Any");
  976. int kind = realType().kind().value();
  977. if (kind != TCKind._tk_value && kind != TCKind._tk_value_box && kind != TCKind._tk_abstract_interface)
  978. throw new BAD_OPERATION();
  979. return (Serializable)object;
  980. }
  981. public void insert_Value(Serializable v)
  982. {
  983. //debug.log ("insert_Value");
  984. object = v;
  985. TypeCode tc;
  986. if ( v == null ) {
  987. tc = orb.get_primitive_tc (TCKind.tk_value);
  988. } else {
  989. // See note in getPrimitiveTypeCodeForClass. We
  990. // have to use the latest type code fixes in this
  991. // case since there is no way to know what ORB will
  992. // actually send this Any. In RMI-IIOP, when using
  993. // Util.writeAny, we can do the versioning correctly,
  994. // and use the insert_Value(Serializable, TypeCode)
  995. // method.
  996. //
  997. // The ORB singleton uses the latest version.
  998. tc = createTypeCodeForClass (v.getClass(),
  999. (com.sun.corba.se.internal.corba.ORB)ORB.init());
  1000. }
  1001. typeCode = TypeCodeImpl.convertToNative(orb, tc);
  1002. isInitialized = true;
  1003. }
  1004. public void insert_Value(Serializable v, org.omg.CORBA.TypeCode t)
  1005. throws org.omg.CORBA.MARSHAL
  1006. {
  1007. //debug.log ("insert_Value2");
  1008. object = v;
  1009. typeCode = TypeCodeImpl.convertToNative(orb, t);
  1010. isInitialized = true;
  1011. }
  1012. public void insert_fixed(java.math.BigDecimal value) {
  1013. typeCode = TypeCodeImpl.convertToNative(orb,
  1014. orb.create_fixed_tc(TypeCodeImpl.digits(value), TypeCodeImpl.scale(value)));
  1015. object = value;
  1016. isInitialized = true;
  1017. }
  1018. public void insert_fixed(java.math.BigDecimal value, org.omg.CORBA.TypeCode type)
  1019. throws org.omg.CORBA.BAD_INV_ORDER
  1020. {
  1021. try {
  1022. if (TypeCodeImpl.digits(value) > type.fixed_digits() ||
  1023. TypeCodeImpl.scale(value) > type.fixed_scale())
  1024. {
  1025. // type and value don't match
  1026. throw new BAD_INV_ORDER();
  1027. }
  1028. } catch (org.omg.CORBA.TypeCodePackage.BadKind bk) {
  1029. // type isn't even of kind fixed
  1030. throw new BAD_INV_ORDER();
  1031. }
  1032. typeCode = TypeCodeImpl.convertToNative(orb, type);
  1033. object = value;
  1034. isInitialized = true;
  1035. }
  1036. public java.math.BigDecimal extract_fixed() {
  1037. if (!isInitialized || !(realType().kind().value() == TCKind._tk_fixed))
  1038. throw new BAD_OPERATION();
  1039. return (BigDecimal)object;
  1040. }
  1041. /**
  1042. * Utility method for insert_Value and Util.writeAny.
  1043. *
  1044. * The ORB passed in should have the desired ORBVersion. It
  1045. * is used to generate the type codes.
  1046. */
  1047. public TypeCode createTypeCodeForClass (java.lang.Class c,
  1048. com.sun.corba.se.internal.corba.ORB tcORB)
  1049. {
  1050. // Look in the cache first
  1051. TypeCodeImpl classTC = tcORB.getTypeCodeForClass(c);
  1052. if (classTC != null)
  1053. return classTC;
  1054. // All cases need to be able to create repository IDs.
  1055. //
  1056. // See bug 4391648 for more info about the tcORB in this
  1057. // case.
  1058. RepositoryIdStrings repStrs
  1059. = RepositoryIdFactory.getRepIdStringsFactory(tcORB);
  1060. // Assertion: c instanceof Serializable?
  1061. if ( c.isArray() ) {
  1062. // Arrays - may recurse for multi-dimensional arrays
  1063. Class componentClass = c.getComponentType();
  1064. TypeCode embeddedType;
  1065. if ( componentClass.isPrimitive() ) {
  1066. embeddedType = getPrimitiveTypeCodeForClass(componentClass,
  1067. tcORB);
  1068. } else {
  1069. embeddedType = createTypeCodeForClass (componentClass,
  1070. tcORB);
  1071. }
  1072. TypeCode t = tcORB.create_sequence_tc (0, embeddedType);
  1073. String id = repStrs.createForJavaType(c);
  1074. return tcORB.create_value_box_tc (id, "Sequence", t);
  1075. } else if ( c == java.lang.String.class ) {
  1076. // Strings
  1077. TypeCode t = tcORB.create_string_tc (0);
  1078. String id = repStrs.createForJavaType(c);
  1079. return tcORB.create_value_box_tc (id, "StringValue", t);
  1080. }
  1081. // Anything else
  1082. // We know that this is a TypeCodeImpl since it is our ORB
  1083. classTC = (TypeCodeImpl)ValueUtility.createTypeCodeForClass(
  1084. tcORB, c, ORBUtility.createValueHandler(tcORB));
  1085. // Intruct classTC to store its buffer
  1086. classTC.setCaching(true);
  1087. // Update the cache
  1088. tcORB.setTypeCodeForClass(c, classTC);
  1089. return classTC;
  1090. }
  1091. /**
  1092. * It looks like this was copied from io.ValueUtility at some
  1093. * point.
  1094. *
  1095. * It's used by createTypeCodeForClass. The tcORB passed in
  1096. * should have the desired ORB version, and is used to
  1097. * create the type codes.
  1098. */
  1099. private TypeCode getPrimitiveTypeCodeForClass (Class c,
  1100. com.sun.corba.se.internal.corba.ORB tcORB)
  1101. {
  1102. //debug.log ("getPrimitiveTypeCodeForClass");
  1103. if (c == Integer.TYPE) {
  1104. return tcORB.get_primitive_tc (TCKind.tk_long);
  1105. } else if (c == Byte.TYPE) {
  1106. return tcORB.get_primitive_tc (TCKind.tk_octet);
  1107. } else if (c == Long.TYPE) {
  1108. return tcORB.get_primitive_tc (TCKind.tk_longlong);
  1109. } else if (c == Float.TYPE) {
  1110. return tcORB.get_primitive_tc (TCKind.tk_float);
  1111. } else if (c == Double.TYPE) {
  1112. return tcORB.get_primitive_tc (TCKind.tk_double);
  1113. } else if (c == Short.TYPE) {
  1114. return tcORB.get_primitive_tc (TCKind.tk_short);
  1115. } else if (c == Character.TYPE) {
  1116. // For Merlin or later JDKs, or for foreign ORBs,
  1117. // we correctly say that a Java char maps to a
  1118. // CORBA wchar. For backwards compatibility
  1119. // with our older ORBs, we say it maps to a
  1120. // CORBA char. This is only used in RMI-IIOP
  1121. // in our javax.rmi.CORBA.Util delegate's
  1122. // writeAny method. In Java IDL, there's no way
  1123. // to know the ORB version that the Any will be
  1124. // sent out with -- it could be different than
  1125. // the one used to create the Any -- so we use the
  1126. // most recent version (see insert_Value).
  1127. if (ORBVersionImpl.FOREIGN.compareTo(tcORB.getORBVersion()) == 0 ||
  1128. ORBVersionImpl.NEWER.compareTo(tcORB.getORBVersion()) <= 0)
  1129. return tcORB.get_primitive_tc(TCKind.tk_wchar);
  1130. else
  1131. return tcORB.get_primitive_tc(TCKind.tk_char);
  1132. } else if (c == Boolean.TYPE) {
  1133. return tcORB.get_primitive_tc (TCKind.tk_boolean);
  1134. } else {
  1135. // _REVISIT_ Not sure if this is right.
  1136. return tcORB.get_primitive_tc (TCKind.tk_any);
  1137. }
  1138. }
  1139. /*
  1140. public java.lang.Object clone()
  1141. throws CloneNotSupportedException
  1142. {
  1143. // Do we want to depend on this constructor?
  1144. //return new AnyImpl(orb, this);
  1145. AnyImpl clone = (AnyImpl)super.clone();
  1146. if (stream != null) {
  1147. clone.stream = stream.dup();
  1148. }
  1149. return clone;
  1150. }
  1151. */
  1152. // Extracts a member value according to the given TypeCode from the given complex Any
  1153. // (at the Anys current internal stream position, consuming the anys stream on the way)
  1154. // and returns it wrapped into a new Any
  1155. public Any extractAny(TypeCode memberType, org.omg.CORBA.ORB orb) {
  1156. Any returnValue = orb.create_any();
  1157. OutputStream out = returnValue.create_output_stream();
  1158. TypeCodeImpl.convertToNative(orb, memberType).copy((InputStream)stream, out);
  1159. returnValue.read_value(out.create_input_stream(), memberType);
  1160. return returnValue;
  1161. }
  1162. /*
  1163. public Any extractAny(TypeCode memberType, org.omg.CORBA.ORB orb) {
  1164. // Resolve aliases here
  1165. TypeCode realType = realType(memberType);
  1166. Any returnValue = orb.create_any();
  1167. OutputStream out;
  1168. int length;
  1169. try {
  1170. switch (realType.kind().value()) {
  1171. case TCKind._tk_boolean:
  1172. returnValue.insert_boolean(stream.read_boolean());
  1173. break;
  1174. case TCKind._tk_char:
  1175. returnValue.insert_char(stream.read_char());
  1176. break;
  1177. case TCKind._tk_wchar:
  1178. returnValue.insert_wchar(stream.read_wchar());
  1179. break;
  1180. case TCKind._tk_octet:
  1181. returnValue.insert_octet(stream.read_octet());
  1182. break;
  1183. case TCKind._tk_short:
  1184. returnValue.insert_short(stream.read_short());
  1185. break;
  1186. case TCKind._tk_enum:
  1187. // Set the type first and then fall through to long
  1188. returnValue.type(realType());
  1189. case TCKind._tk_long:
  1190. returnValue.insert_long(stream.read_long());
  1191. break;
  1192. case TCKind._tk_ushort:
  1193. returnValue.insert_ushort(stream.read_ushort());
  1194. break;
  1195. case TCKind._tk_ulong:
  1196. returnValue.insert_ulong(stream.read_ulong());
  1197. break;
  1198. case TCKind._tk_longlong:
  1199. returnValue.insert_longlong(stream.read_longlong());
  1200. break;
  1201. case TCKind._tk_ulonglong:
  1202. returnValue.insert_ulonglong(stream.read_ulonglong());
  1203. break;
  1204. case TCKind._tk_float:
  1205. returnValue.insert_float(stream.read_float());
  1206. break;
  1207. case TCKind._tk_double:
  1208. returnValue.insert_double(stream.read_double());
  1209. break;
  1210. case TCKind._tk_string:
  1211. returnValue.insert_string(stream.read_string());
  1212. break;
  1213. case TCKind._tk_wstring:
  1214. returnValue.insert_wstring(stream.read_wstring());
  1215. break;
  1216. case TCKind._tk_objref:
  1217. returnValue.insert_Object(stream.read_Object());
  1218. break;
  1219. case TCKind._tk_any:
  1220. returnValue.insert_any(stream.read_any());
  1221. break;
  1222. case TCKind._tk_TypeCode:
  1223. returnValue.insert_TypeCode(stream.read_TypeCode());
  1224. break;
  1225. case TCKind._tk_Principal:
  1226. returnValue.insert_Principal(stream.read_Principal());
  1227. break;
  1228. case TCKind._tk_fixed:
  1229. returnValue.insert_fixed(stream.read_fixed());
  1230. break;
  1231. case TCKind._tk_value:
  1232. case TCKind._tk_value_box:
  1233. returnValue.insert_Value(stream.read_Value());
  1234. break;
  1235. // _REVISIT_ Room for optimization. Lots of intermediary Anys created
  1236. // for all complex types here.
  1237. case TCKind._tk_struct:
  1238. case TCKind._tk_array:
  1239. case TCKind._tk_except:
  1240. out = returnValue.create_output_stream();
  1241. // Copy all the members
  1242. length = realType.member_count();
  1243. for (int i=0; i<length; i++) {
  1244. extractAny(realType.member_type(i), orb).write_value(out);
  1245. }
  1246. returnValue.read_value(out.create_input_stream(), realType);
  1247. break;
  1248. case TCKind._tk_sequence:
  1249. out = returnValue.create_output_stream();
  1250. // Copy the length and all the members
  1251. length = stream.read_long();
  1252. for (int i=0; i<length; i++) {
  1253. extractAny(realType.member_type(i), orb).write_value(out);
  1254. }
  1255. returnValue.read_value(out.create_input_stream(), realType);
  1256. break;
  1257. case TCKind._tk_union:
  1258. out = returnValue.create_output_stream();
  1259. // Copy discriminator and value
  1260. Any discriminator = extractAny(realType.discriminator_type(), orb);
  1261. TypeCodeImpl realTypeCodeImpl = TypeCodeImpl.convertToNative(orb, realType);
  1262. int memberIndex = realTypeCodeImpl.currentUnionMemberIndex(discriminator);
  1263. if (memberIndex == -1)
  1264. throw new MARSHAL();
  1265. Any unionValue = extractAny(realType.member_type(memberIndex), orb);
  1266. unionValue.write_value(out);
  1267. discriminator.write_value(out);
  1268. returnValue.read_value(out.create_input_stream(), realType);
  1269. break;
  1270. case TCKind._tk_alias:
  1271. // error resolving alias above
  1272. //return extractAny(realType.content_type(), orb);
  1273. throw new org.omg.CORBA.INTERNAL();
  1274. case TCKind._tk_null:
  1275. case TCKind._tk_void:
  1276. case TCKind._tk_native:
  1277. case TCKind._tk_abstract_interface:
  1278. // These Anys don't have values, just a TypeCode
  1279. returnValue.type(realType);
  1280. break;
  1281. case TCKind._tk_longdouble:
  1282. // Unspecified for Java
  1283. throw new org.omg.CORBA.NO_IMPLEMENT();
  1284. default:
  1285. throw new org.omg.CORBA.BAD_PARAM();
  1286. }
  1287. } catch (BadKind badKind) {
  1288. } catch (Bounds bounds) {
  1289. }
  1290. return returnValue;
  1291. }
  1292. */
  1293. // This method could very well be moved into TypeCodeImpl or a common utility class,
  1294. // but is has to be in this package.
  1295. static public Any extractAnyFromStream(TypeCode memberType, InputStream input, org.omg.CORBA.ORB orb) {
  1296. Any returnValue = orb.create_any();
  1297. OutputStream out = returnValue.create_output_stream();
  1298. TypeCodeImpl.convertToNative(orb, memberType).copy(input, out);
  1299. returnValue.read_value(out.create_input_stream(), memberType);
  1300. return returnValue;
  1301. }
  1302. // There is no other way for DynAnys to find out whether the Any is initialized.
  1303. public boolean isInitialized() {
  1304. return isInitialized;
  1305. }
  1306. }