1. /*
  2. * @(#)DynArrayImpl.java 1.5 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. package com.sun.corba.se.internal.DynamicAny;
  8. import org.omg.CORBA.ORB;
  9. import org.omg.CORBA.TypeCode;
  10. import org.omg.CORBA.Any;
  11. import org.omg.CORBA.NO_IMPLEMENT;
  12. import org.omg.CORBA.OBJECT_NOT_EXIST;
  13. import org.omg.CORBA.BAD_OPERATION;
  14. import org.omg.CORBA.TypeCodePackage.BadKind;
  15. import org.omg.CORBA.TypeCodePackage.Bounds;
  16. import org.omg.CORBA.portable.InputStream;
  17. import org.omg.DynamicAny.*;
  18. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  19. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  20. import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  21. public class DynArrayImpl extends DynAnyCollectionImpl implements DynArray
  22. {
  23. //
  24. // Constructors
  25. //
  26. private DynArrayImpl() {
  27. this(null, (Any)null, false);
  28. }
  29. protected DynArrayImpl(ORB orb, Any any, boolean copyValue) {
  30. super(orb, any, copyValue);
  31. }
  32. protected DynArrayImpl(ORB orb, TypeCode typeCode) {
  33. super(orb, typeCode);
  34. }
  35. // Initializes components and anys representation
  36. // from the Any representation
  37. protected boolean initializeComponentsFromAny() {
  38. // This typeCode is of kind tk_array.
  39. TypeCode typeCode = any.type();
  40. int length = getBound();
  41. TypeCode contentType = getContentType();
  42. InputStream input;
  43. try {
  44. input = any.create_input_stream();
  45. } catch (BAD_OPERATION e) {
  46. return false;
  47. }
  48. components = new DynAny[length];
  49. anys = new Any[length];
  50. for (int i=0; i<length; i++) {
  51. // _REVISIT_ Could use read_xxx_array() methods on InputStream for efficiency
  52. // but only for primitive types
  53. anys[i] = DynAnyUtil.extractAnyFromStream(contentType, input, orb);
  54. try {
  55. // Creates the appropriate subtype without copying the Any
  56. components[i] = DynAnyUtil.createMostDerivedDynAny(anys[i], orb, false);
  57. } catch (InconsistentTypeCode itc) { // impossible
  58. }
  59. }
  60. return true;
  61. }
  62. // Initializes components and anys representation
  63. // from the internal TypeCode information with default values.
  64. // This is not done recursively, only one level.
  65. // More levels are initialized lazily, on demand.
  66. protected boolean initializeComponentsFromTypeCode() {
  67. // This typeCode is of kind tk_array.
  68. TypeCode typeCode = any.type();
  69. int length = getBound();
  70. TypeCode contentType = getContentType();
  71. components = new DynAny[length];
  72. anys = new Any[length];
  73. for (int i=0; i<length; i++) {
  74. createDefaultComponentAt(i, contentType);
  75. }
  76. return true;
  77. }
  78. //
  79. // DynArray interface methods
  80. //
  81. // Initializes the elements of the array.
  82. // If value does not contain the same number of elements as the array dimension,
  83. // the operation raises InvalidValue.
  84. // If one or more elements have a type that is inconsistent with the DynArrays TypeCode,
  85. // the operation raises TypeMismatch.
  86. // This operation does not change the current position.
  87. /*
  88. public void set_elements (org.omg.CORBA.Any[] value)
  89. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  90. org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  91. */
  92. //
  93. // Utility methods
  94. //
  95. protected void checkValue(Object[] value)
  96. throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
  97. {
  98. if (value == null || value.length != getBound()) {
  99. throw new InvalidValue();
  100. }
  101. }
  102. }