gnuAny.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /* gnuAny.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA;
  32. import gnu.CORBA.CDR.Vio;
  33. import gnu.CORBA.CDR.BufferredCdrInput;
  34. import gnu.CORBA.CDR.BufferedCdrOutput;
  35. import gnu.CORBA.typecodes.PrimitiveTypeCode;
  36. import gnu.CORBA.typecodes.StringTypeCode;
  37. import org.omg.CORBA.Any;
  38. import org.omg.CORBA.AnyHolder;
  39. import org.omg.CORBA.BAD_OPERATION;
  40. import org.omg.CORBA.BooleanHolder;
  41. import org.omg.CORBA.CharHolder;
  42. import org.omg.CORBA.DoubleHolder;
  43. import org.omg.CORBA.FixedHolder;
  44. import org.omg.CORBA.FloatHolder;
  45. import org.omg.CORBA.IntHolder;
  46. import org.omg.CORBA.LongHolder;
  47. import org.omg.CORBA.MARSHAL;
  48. import org.omg.CORBA.ORB;
  49. import org.omg.CORBA.ObjectHolder;
  50. import org.omg.CORBA.Principal;
  51. import org.omg.CORBA.PrincipalHolder;
  52. import org.omg.CORBA.ShortHolder;
  53. import org.omg.CORBA.StringHolder;
  54. import org.omg.CORBA.TCKind;
  55. import org.omg.CORBA.TypeCode;
  56. import org.omg.CORBA.TypeCodeHolder;
  57. import org.omg.CORBA.ValueBaseHolder;
  58. import org.omg.CORBA.portable.Streamable;
  59. import java.io.Serializable;
  60. import java.lang.reflect.Field;
  61. import java.math.BigDecimal;
  62. import java.util.Arrays;
  63. import java.util.zip.Adler32;
  64. /**
  65. * The implementation of {@link Any}.
  66. *
  67. * For performance reasonse, the inserted values are not cloned.
  68. * If the value object allows modifications (like {@link Streamable}),
  69. * these subsequent alterations are reflected by the instance of
  70. * this gnuAny, and the gnuAny alterations are reflected by the
  71. * returned value. If it is required to have the uncoupled value,
  72. * it must be requested from the copy of the current instance.
  73. * The {@link gnuAny} can be simply cloned by the provided
  74. * {@link Clone()} method.
  75. *
  76. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  77. */
  78. public class gnuAny
  79. extends Any
  80. {
  81. /**
  82. * Use serialVersionUID for interoperability.
  83. */
  84. private static final long serialVersionUID = 1;
  85. /**
  86. * The value, returned by {@link #type()} if the value has been
  87. * not intialized.
  88. */
  89. protected static final TypeCode nullType =
  90. new PrimitiveTypeCode(TCKind.tk_null);
  91. /**
  92. * The Streamable, representing the value, held by this gnuAny.
  93. */
  94. protected Streamable has;
  95. /**
  96. * The complete typecode of the Streamable, if explicitly set.
  97. */
  98. protected TypeCode typecode;
  99. /**
  100. * The typecode kind of the Streamable, if explicitly set.
  101. */
  102. protected int xKind = -1;
  103. /**
  104. * The associated ORB.
  105. */
  106. private ORB orb;
  107. /**
  108. * Set the associated orb.
  109. */
  110. public void setOrb(ORB an_orb)
  111. {
  112. orb = an_orb;
  113. }
  114. /**
  115. * Creates a deep copy of this gnuAny, writing to and subsequently
  116. * reading from from the byte buffer.
  117. *
  118. * @return the uncoupled gnuAny with all fields set to identical
  119. * values.
  120. */
  121. public gnuAny Clone()
  122. {
  123. BufferedCdrOutput out = new BufferedCdrOutput();
  124. out.setOrb(orb);
  125. out.write_any(this);
  126. BufferredCdrInput in = new BufferredCdrInput(out.buffer.toByteArray());
  127. in.setOrb(orb);
  128. return (gnuAny) in.read_any();
  129. }
  130. /**
  131. * Create the buffered CDR input stream, containing the
  132. * value, stored inside of this {@link Any}.
  133. */
  134. public org.omg.CORBA.portable.InputStream create_input_stream()
  135. {
  136. if (has instanceof GeneralHolder)
  137. {
  138. GeneralHolder u = (GeneralHolder) has;
  139. return u.getInputStream();
  140. }
  141. else
  142. {
  143. BufferedCdrOutput out = new BufferedCdrOutput();
  144. out.setOrb(orb);
  145. write_value(out);
  146. BufferredCdrInput in = new BufferredCdrInput(out.buffer.toByteArray());
  147. in.setOrb(orb);
  148. return in;
  149. }
  150. }
  151. /**
  152. * Create the buffered CDR output stream (empty).
  153. */
  154. public org.omg.CORBA.portable.OutputStream create_output_stream()
  155. {
  156. BufferedCdrOutput stream = new BufferedCdrOutput();
  157. stream.setOrb(orb);
  158. return stream;
  159. }
  160. /**
  161. * Compare two Any's for equality.
  162. * @param other the other Any to compare.
  163. */
  164. public boolean equal(Any other)
  165. {
  166. if (other == this)
  167. return true;
  168. if (type().kind() != other.type().kind())
  169. return false;
  170. if (has != null && other instanceof gnuAny)
  171. if (has.equals(((gnuAny) other).has))
  172. return true;
  173. BufferedCdrOutput a = new BufferedCdrOutput();
  174. a.setOrb(orb);
  175. write_value(a);
  176. BufferedCdrOutput b = new BufferedCdrOutput();
  177. b.setOrb(orb);
  178. other.write_value(b);
  179. byte[] ba = a.buffer.toByteArray();
  180. byte[] bb = b.buffer.toByteArray();
  181. return Arrays.equals(ba, bb);
  182. }
  183. /**
  184. * Get the content - dependent hashcode.
  185. */
  186. public int hashCode()
  187. {
  188. if (has == null)
  189. return type().kind().value();
  190. else
  191. {
  192. Adler32 adler = new Adler32();
  193. BufferedCdrOutput a = new BufferedCdrOutput();
  194. a.setOrb(orb);
  195. write_value(a);
  196. adler.update(a.buffer.toByteArray());
  197. adler.update(type().kind().value());
  198. return (int) adler.getValue() & Integer.MAX_VALUE;
  199. }
  200. }
  201. /**
  202. * Delegates functionality to {@link #equal(Any)}.
  203. */
  204. public boolean equals(java.lang.Object other)
  205. {
  206. if (other == this)
  207. return true;
  208. if (!(other instanceof Any))
  209. return false;
  210. return equal((Any) other);
  211. }
  212. /**
  213. * Extract the previously stored object.
  214. */
  215. public org.omg.CORBA.Object extract_Object()
  216. {
  217. try
  218. {
  219. return ((ObjectHolder) has).value;
  220. }
  221. catch (ClassCastException ex)
  222. {
  223. BAD_OPERATION bad = new BAD_OPERATION();
  224. bad.initCause(ex);
  225. bad.minor = Minor.Any;
  226. throw bad;
  227. }
  228. }
  229. /**
  230. * Extract the previously inserted CORBA <code>Principal</code>/
  231. * @return the previously inserted value.
  232. *
  233. * @throws org.omg.CORBA.BAD_OPERATION if the holder contains something
  234. * else than Principal.
  235. *
  236. * @deprecated by CORBA 2.2.
  237. */
  238. public Principal extract_Principal()
  239. {
  240. check(TCKind._tk_Principal);
  241. return ((PrincipalHolder) has).value;
  242. }
  243. /**
  244. * Return the value, encapsulated in a suitable holder.
  245. * This implementation returns the direct reference,
  246. * so the alterations on the returned streamable are
  247. * directly reflected to the content of this {@link Any}.
  248. */
  249. public Streamable extract_Streamable()
  250. {
  251. return has;
  252. }
  253. public TypeCode extract_TypeCode()
  254. throws BAD_OPERATION
  255. {
  256. check(TCKind._tk_TypeCode);
  257. return ((TypeCodeHolder) has).value;
  258. }
  259. /**
  260. * Extract the stored value type.
  261. *
  262. * @return the previously stored value type.
  263. *
  264. * @throws BAD_OPERATION if the Any contains something different.
  265. *
  266. * @see org.omg.CORBA.portable.ValueBase
  267. */
  268. public Serializable extract_Value()
  269. throws BAD_OPERATION
  270. {
  271. try
  272. {
  273. if (has instanceof ValueBaseHolder)
  274. return ((ValueBaseHolder) has).value;
  275. else
  276. {
  277. // Normally, ValueBase holder must be an instance of the
  278. // ValueBaseHolder. However some IDL compilers probably
  279. // have a bug, do not deriving this way. The the only
  280. // way to access the wrapped value is via reflection.
  281. Field f = has.getClass().getField("value");
  282. return (Serializable) f.get(has);
  283. }
  284. }
  285. catch (Exception ex)
  286. {
  287. BAD_OPERATION bad = new BAD_OPERATION("Value type expected");
  288. bad.minor = Minor.Any;
  289. bad.initCause(ex);
  290. throw bad;
  291. }
  292. }
  293. /** {@inheritDoc} */
  294. public Any extract_any()
  295. throws BAD_OPERATION
  296. {
  297. check(TCKind._tk_any);
  298. return ((AnyHolder) has).value;
  299. }
  300. /** {@inheritDoc} */
  301. public boolean extract_boolean()
  302. throws BAD_OPERATION
  303. {
  304. check(TCKind._tk_boolean);
  305. return ((BooleanHolder) has).value;
  306. }
  307. /** {@inheritDoc} */
  308. public char extract_char()
  309. throws BAD_OPERATION
  310. {
  311. check(TCKind._tk_char);
  312. return ((CharHolder) has).value;
  313. }
  314. /** {@inheritDoc} */
  315. public double extract_double()
  316. throws BAD_OPERATION
  317. {
  318. check(TCKind._tk_double);
  319. return ((DoubleHolder) has).value;
  320. }
  321. /**
  322. * Extract the previously inserted CORBA <code>fixed</code>/
  323. * @return the previously inserted value.
  324. *
  325. * @throws org.omg.CORBA.BAD_OPERATION if the holder contains something
  326. * else than BigDecimal.
  327. */
  328. public BigDecimal extract_fixed()
  329. throws org.omg.CORBA.BAD_OPERATION
  330. {
  331. check(TCKind._tk_fixed);
  332. return ((FixedHolder) has).value;
  333. }
  334. /** {@inheritDoc} */
  335. public float extract_float()
  336. throws BAD_OPERATION
  337. {
  338. check(TCKind._tk_float);
  339. return ((FloatHolder) has).value;
  340. }
  341. /** {@inheritDoc} */
  342. public int extract_long()
  343. throws BAD_OPERATION
  344. {
  345. // CORBA long = java int.
  346. check(TCKind._tk_long);
  347. return ((IntHolder) has).value;
  348. }
  349. /** {@inheritDoc} */
  350. public long extract_longlong()
  351. throws BAD_OPERATION
  352. {
  353. check(TCKind._tk_longlong);
  354. return ((LongHolder) has).value;
  355. }
  356. /** {@inheritDoc} */
  357. public byte extract_octet()
  358. throws BAD_OPERATION
  359. {
  360. // ShortHolder holds also octets.
  361. check(TCKind._tk_octet);
  362. return (byte) ((OctetHolder) has).value;
  363. }
  364. /** {@inheritDoc} */
  365. public short extract_short()
  366. throws BAD_OPERATION
  367. {
  368. check(TCKind._tk_short);
  369. return ((ShortHolder) has).value;
  370. }
  371. /** {@inheritDoc} */
  372. public String extract_string()
  373. throws BAD_OPERATION
  374. {
  375. check(TCKind._tk_string);
  376. return ((StringHolder) has).value;
  377. }
  378. /** {@inheritDoc} */
  379. public int extract_ulong()
  380. throws BAD_OPERATION
  381. {
  382. // IntHolder also holds ulongs.
  383. check(TCKind._tk_ulong);
  384. return ((IntHolder) has).value;
  385. }
  386. /** {@inheritDoc} */
  387. public long extract_ulonglong()
  388. throws BAD_OPERATION
  389. {
  390. // LongHolder also holds ulonglong
  391. check(TCKind._tk_ulonglong);
  392. return ((LongHolder) has).value;
  393. }
  394. /** {@inheritDoc} */
  395. public short extract_ushort()
  396. throws BAD_OPERATION
  397. {
  398. // ShortHolder also holds ushorts.
  399. check(TCKind._tk_ushort);
  400. return ((ShortHolder) has).value;
  401. }
  402. /** {@inheritDoc} */
  403. public char extract_wchar()
  404. throws BAD_OPERATION
  405. {
  406. check(TCKind._tk_wchar);
  407. return ((WCharHolder) has).value;
  408. }
  409. /** {@inheritDoc} */
  410. public String extract_wstring()
  411. throws BAD_OPERATION
  412. {
  413. // StringHolder also holds wstrings.
  414. check(TCKind._tk_wstring);
  415. return ((WStringHolder) has).value;
  416. }
  417. /**
  418. * Inserts the CORBA object and sets the typecode to the given type.
  419. */
  420. public void insert_Object(org.omg.CORBA.Object x, TypeCode typecode)
  421. {
  422. has = new ObjectHolder(x);
  423. type(typecode);
  424. }
  425. /**
  426. * Inserts the CORBA object.
  427. */
  428. public void insert_Object(org.omg.CORBA.Object x)
  429. {
  430. has = new ObjectHolder(x);
  431. }
  432. /**
  433. * Insert the CORBA Principal.
  434. * This implementation uses direct assignment, so the later
  435. * alterations of that BigDecimal are reflected on the
  436. * content of this {@link Any}.
  437. *
  438. * @deprecated by CORBA 2.2.
  439. */
  440. public void insert_Principal(Principal x)
  441. {
  442. resetTypes();
  443. if (has instanceof PrincipalHolder)
  444. ((PrincipalHolder) has).value = x;
  445. else
  446. has = new PrincipalHolder(x);
  447. }
  448. /**
  449. * Sets the value to the value, encapsulated in this holder.
  450. * This implementation uses direct assignment, so the later
  451. * alterations of that streamable are reflected on the
  452. * content of this {@link Any}.
  453. */
  454. public void insert_Streamable(Streamable x)
  455. {
  456. resetTypes();
  457. has = x;
  458. }
  459. /**
  460. * Insert the typecode into this Any
  461. * @param typecode the typecode to insert.
  462. */
  463. public void insert_TypeCode(TypeCode typecode)
  464. {
  465. resetTypes();
  466. if (has instanceof TypeCodeHolder)
  467. ((TypeCodeHolder) has).value = typecode;
  468. else
  469. has = new TypeCodeHolder(typecode);
  470. }
  471. /** {@inheritDoc} */
  472. public void insert_Value(Serializable x, TypeCode c_typecode)
  473. {
  474. if (typecode != null && typecode.kind() == TCKind.tk_value_box)
  475. {
  476. has = new gnuValueHolder(x, typecode);
  477. }
  478. else
  479. {
  480. type(typecode);
  481. insert_Value(x);
  482. }
  483. }
  484. /** {@inheritDoc} */
  485. public void insert_Value(Serializable x)
  486. {
  487. if (typecode != null && typecode.kind() == TCKind.tk_value_box)
  488. {
  489. has = new gnuValueHolder(x, typecode);
  490. }
  491. else
  492. {
  493. if (has instanceof ValueBaseHolder)
  494. ((ValueBaseHolder) has).value = x;
  495. else
  496. has = new ValueBaseHolder(x);
  497. }
  498. }
  499. /**
  500. * Insert another {@link Any} into this {@link Any}.
  501. * This implementation uses direct assignment, so the later
  502. * alterations of that {@link Any} are reflected on the
  503. * content of this {@link Any}.
  504. */
  505. public void insert_any(Any an_any)
  506. {
  507. resetTypes();
  508. if (has instanceof AnyHolder)
  509. ((AnyHolder) has).value = an_any;
  510. else
  511. has = new AnyHolder(an_any);
  512. }
  513. /** {@inheritDoc} */
  514. public void insert_boolean(boolean x)
  515. {
  516. resetTypes();
  517. if (has instanceof BooleanHolder)
  518. ((BooleanHolder) has).value = x;
  519. else
  520. has = new BooleanHolder(x);
  521. }
  522. /** {@inheritDoc} */
  523. public void insert_char(char x)
  524. {
  525. resetTypes();
  526. if (has instanceof CharHolder)
  527. ((CharHolder) has).value = x;
  528. else
  529. has = new CharHolder(x);
  530. }
  531. /** {@inheritDoc} */
  532. public void insert_double(double x)
  533. {
  534. resetTypes();
  535. if (has instanceof DoubleHolder)
  536. ((DoubleHolder) has).value = x;
  537. else
  538. has = new DoubleHolder(x);
  539. }
  540. /**
  541. * Inserts the CORBA <code>fixed</code>, setting the typecode
  542. * explicitly.
  543. * This implementation uses direct assignment, so the later
  544. * alterations of that BigDecimal are reflected on the
  545. * content of this {@link Any}.
  546. */
  547. public void insert_fixed(BigDecimal x, TypeCode x_typecode)
  548. {
  549. resetTypes();
  550. insert_fixed(x);
  551. typecode = x_typecode;
  552. }
  553. /**
  554. * Inserts the CORBA <code>fixed</code>, setting the typecode
  555. * by example of the currently passed value.
  556. * This implementation uses direct assignment, so the later
  557. * alterations of that BigDecimal are reflected on the
  558. * content of this {@link Any}, including the typecode.
  559. */
  560. public void insert_fixed(BigDecimal x)
  561. {
  562. resetTypes();
  563. if (has instanceof FixedHolder)
  564. ((FixedHolder) has).value = x;
  565. else
  566. has = new FixedHolder(x);
  567. }
  568. /** {@inheritDoc} */
  569. public void insert_float(float x)
  570. {
  571. resetTypes();
  572. if (has instanceof FloatHolder)
  573. ((FloatHolder) has).value = x;
  574. else
  575. has = new FloatHolder(x);
  576. }
  577. /** {@inheritDoc} */
  578. public void insert_long(int x)
  579. {
  580. resetTypes();
  581. if (has instanceof IntHolder)
  582. ((IntHolder) has).value = x;
  583. else
  584. has = new IntHolder(x);
  585. }
  586. /** {@inheritDoc} */
  587. public void insert_longlong(long x)
  588. {
  589. resetTypes();
  590. if (has instanceof LongHolder)
  591. ((LongHolder) has).value = x;
  592. else
  593. has = new LongHolder(x);
  594. }
  595. /** {@inheritDoc} */
  596. public void insert_octet(byte x)
  597. {
  598. resetTypes();
  599. if (has instanceof OctetHolder)
  600. ((OctetHolder) has).value = x;
  601. else
  602. has = new OctetHolder(x);
  603. }
  604. /** {@inheritDoc} */
  605. public void insert_short(short x)
  606. {
  607. resetTypes();
  608. if (has instanceof ShortHolder)
  609. ((ShortHolder) has).value = x;
  610. else
  611. has = new ShortHolder(x);
  612. }
  613. /** {@inheritDoc} */
  614. public void insert_string(String x)
  615. {
  616. resetTypes();
  617. if (has instanceof StringHolder)
  618. ((StringHolder) has).value = x;
  619. else
  620. has = new StringHolder(x);
  621. typecode = new StringTypeCode(TCKind.tk_string);
  622. }
  623. /** {@inheritDoc} */
  624. public void insert_ulong(int x)
  625. {
  626. resetTypes();
  627. if (has instanceof IntHolder)
  628. ((IntHolder) has).value = x;
  629. else
  630. has = new IntHolder(x);
  631. xKind = TCKind._tk_ulong;
  632. }
  633. /** {@inheritDoc} */
  634. public void insert_ulonglong(long x)
  635. {
  636. resetTypes();
  637. if (has instanceof LongHolder)
  638. ((LongHolder) has).value = x;
  639. else
  640. has = new LongHolder(x);
  641. xKind = TCKind._tk_ulonglong;
  642. }
  643. /** {@inheritDoc} */
  644. public void insert_ushort(short x)
  645. {
  646. resetTypes();
  647. if (has instanceof ShortHolder)
  648. ((ShortHolder) has).value = x;
  649. else
  650. has = new ShortHolder(x);
  651. xKind = TCKind._tk_ushort;
  652. }
  653. /** {@inheritDoc} */
  654. public void insert_wchar(char x)
  655. {
  656. resetTypes();
  657. if (has instanceof WCharHolder)
  658. ((WCharHolder) has).value = x;
  659. else
  660. has = new WCharHolder(x);
  661. }
  662. /** {@inheritDoc} */
  663. public void insert_wstring(String x)
  664. {
  665. resetTypes();
  666. if (has instanceof WStringHolder)
  667. ((WStringHolder) has).value = x;
  668. else
  669. has = new WStringHolder(x);
  670. }
  671. /**
  672. * Return the associated orb.
  673. */
  674. public ORB orb()
  675. {
  676. return orb;
  677. }
  678. /**
  679. * Read the value of the given type from the given stream.
  680. *
  681. * @param input a stream to read from.
  682. * @param a_type a typecode of the value to read.
  683. */
  684. public void read_value(org.omg.CORBA.portable.InputStream input,
  685. TypeCode a_type
  686. )
  687. throws MARSHAL
  688. {
  689. try
  690. {
  691. int kind = a_type.kind().value();
  692. // Fixed needs special handling.
  693. if (kind == TCKind._tk_fixed)
  694. {
  695. BigDecimal dec = BigDecimalHelper.read(input, a_type.fixed_scale());
  696. has = new FixedHolder(dec);
  697. }
  698. else
  699. {
  700. has = HolderLocator.createHolder(a_type);
  701. if (has == null)
  702. {
  703. // Use the Universal Holder that reads till the end of stream.
  704. // This works with the extract/insert pair of the typical
  705. // Helper.
  706. BufferedCdrOutput buffer = new BufferedCdrOutput();
  707. buffer.setOrb(orb);
  708. has = new GeneralHolder(buffer);
  709. }
  710. }
  711. type(a_type);
  712. if (!(has instanceof GeneralHolder) &&
  713. (kind == TCKind._tk_value_box))
  714. {
  715. // The streamable only contains operations for
  716. // reading the value, not the value header.
  717. Field vField = has.getClass().getField("value");
  718. Object content = Vio.read(input, a_type.id());
  719. vField.set(has, content);
  720. }
  721. else
  722. has._read(input);
  723. }
  724. catch (Exception ex)
  725. {
  726. MARSHAL m = new MARSHAL();
  727. m.minor = Minor.Any;
  728. m.initCause(ex);
  729. throw m;
  730. }
  731. }
  732. /** {@inheritDoc} */
  733. public TypeCode type()
  734. {
  735. if (typecode != null)
  736. return typecode;
  737. else if (xKind >= 0)
  738. {
  739. typecode = new PrimitiveTypeCode(TCKind.from_int(xKind));
  740. return typecode;
  741. }
  742. else
  743. return has != null ? has._type() : nullType;
  744. }
  745. /**
  746. * Explicitly set the typecode of the value to the given type.
  747. *
  748. * @param valueTypeCode the typecode of the value.
  749. */
  750. public void type(TypeCode valueTypeCode)
  751. {
  752. xKind = valueTypeCode.kind().value();
  753. typecode = valueTypeCode;
  754. }
  755. /** {@inheritDoc} */
  756. public void write_value(org.omg.CORBA.portable.OutputStream output)
  757. {
  758. if (has != null)
  759. has._write(output);
  760. else
  761. // These kinds support null.
  762. if (xKind == TCKind._tk_null || xKind == TCKind._tk_objref ||
  763. xKind == TCKind._tk_value || xKind == TCKind._tk_value_box
  764. )
  765. output.write_long(0);
  766. }
  767. /**
  768. * Check if the current value if the value of the given kind.
  769. *
  770. * @param kind a kind to check.
  771. * @throws BAD_OPERATION if the value is not set of is different kind.
  772. */
  773. protected void check(int kind)
  774. throws BAD_OPERATION
  775. {
  776. if (has == null)
  777. {
  778. BAD_OPERATION bad = new BAD_OPERATION("value not set");
  779. bad.minor = Minor.Any;
  780. throw bad;
  781. }
  782. if (xKind >= 0)
  783. {
  784. if (xKind != kind)
  785. if (!(xKind == TCKind._tk_alias && has._type().kind().value() == kind))
  786. {
  787. BAD_OPERATION bad = new BAD_OPERATION("Extracting "
  788. + TypeKindNamer.nameIt(kind) + " when stored "
  789. + TypeKindNamer.nameIt(xKind));
  790. bad.minor = Minor.Any;
  791. throw bad;
  792. }
  793. }
  794. else
  795. {
  796. if (type().kind().value() != kind)
  797. if (!(type().kind().value() == TCKind._tk_alias && has._type().kind().value() == kind))
  798. {
  799. BAD_OPERATION bad = new BAD_OPERATION("Extracting "
  800. + TypeKindNamer.nameIt(kind) + " stored "
  801. + TypeKindNamer.nameIt(type()));
  802. bad.minor = Minor.Any;
  803. throw bad;
  804. }
  805. }
  806. }
  807. /**
  808. * Clear the additional type information before reusing this instance.
  809. */
  810. private final void resetTypes()
  811. {
  812. typecode = null;
  813. xKind = -1;
  814. }
  815. }