DynSequenceImpl.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.corba.se.impl.dynamicany; 27 28import org.omg.CORBA.TypeCode; 29import org.omg.CORBA.Any; 30import org.omg.CORBA.BAD_OPERATION; 31import org.omg.CORBA.TypeCodePackage.BadKind; 32import org.omg.CORBA.TypeCodePackage.Bounds; 33import org.omg.CORBA.portable.InputStream; 34import org.omg.CORBA.portable.OutputStream; 35import org.omg.DynamicAny.*; 36import org.omg.DynamicAny.DynAnyPackage.TypeMismatch; 37import org.omg.DynamicAny.DynAnyPackage.InvalidValue; 38import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode; 39 40import com.sun.corba.se.spi.orb.ORB ; 41import com.sun.corba.se.spi.logging.CORBALogDomains ; 42import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 43 44// _REVIST_ Could make this a subclass of DynArrayImpl 45// But that would mean that an object that implements DynSequence also implements DynArray 46// which the spec doesn't mention (it also doesn't forbid it). 47public class DynSequenceImpl extends DynAnyCollectionImpl implements DynSequence 48{ 49 // 50 // Constructors 51 // 52 53 private DynSequenceImpl() { 54 this(null, (Any)null, false); 55 } 56 57 protected DynSequenceImpl(ORB orb, Any any, boolean copyValue) { 58 super(orb, any, copyValue); 59 } 60 61 // Sets the current position to -1 and creates an empty sequence. 62 protected DynSequenceImpl(ORB orb, TypeCode typeCode) { 63 super(orb, typeCode); 64 } 65 66 // Initializes components and anys representation 67 // from the Any representation 68 protected boolean initializeComponentsFromAny() { 69 // This typeCode is of kind tk_sequence. 70 TypeCode typeCode = any.type(); 71 int length; 72 TypeCode contentType = getContentType(); 73 InputStream input; 74 75 try { 76 input = any.create_input_stream(); 77 } catch (BAD_OPERATION e) { 78 return false; 79 } 80 81 length = input.read_long(); 82 components = new DynAny[length]; 83 anys = new Any[length]; 84 85 for (int i=0; i<length; i++) { 86 // _REVISIT_ Could use read_xxx_array() methods on InputStream for efficiency 87 // but only for primitive types 88 anys[i] = DynAnyUtil.extractAnyFromStream(contentType, input, orb); 89 try { 90 // Creates the appropriate subtype without copying the Any 91 components[i] = DynAnyUtil.createMostDerivedDynAny(anys[i], orb, false); 92 } catch (InconsistentTypeCode itc) { // impossible 93 } 94 } 95 return true; 96 } 97 98 // Sets the current position to -1 and creates an empty sequence. 99 protected boolean initializeComponentsFromTypeCode() { 100 // already done in the type code constructor 101 components = new DynAny[0]; 102 anys = new Any[0]; 103 return true; 104 } 105 106 // Collapses the whole DynAny hierarchys values into one single streamed Any 107 protected boolean initializeAnyFromComponents() { 108 OutputStream out = any.create_output_stream(); 109 // Writing the length first is the only difference to supers implementation 110 out.write_long(components.length); 111 for (int i=0; i<components.length; i++) { 112 if (components[i] instanceof DynAnyImpl) { 113 ((DynAnyImpl)components[i]).writeAny(out); 114 } else { 115 // Not our implementation. Nothing we can do to prevent copying. 116 components[i].to_any().write_value(out); 117 } 118 } 119 any.read_value(out.create_input_stream(), any.type()); 120 return true; 121 } 122 123 124 // 125 // DynSequence interface methods 126 // 127 128 // Returns the current length of the sequence 129 public int get_length() { 130 if (status == STATUS_DESTROYED) { 131 throw wrapper.dynAnyDestroyed() ; 132 } 133 return (checkInitComponents() ? components.length : 0); 134 } 135 136 // Sets the length of the sequence. Increasing the length of a sequence 137 // adds new elements at the tail without affecting the values of already 138 // existing elements. Newly added elements are default-initialized. 139 // 140 // Increasing the length of a sequence sets the current position to the first 141 // newly-added element if the previous current position was -1. 142 // Otherwise, if the previous current position was not -1, 143 // the current position is not affected. 144 // 145 // Increasing the length of a bounded sequence to a value larger than the bound 146 // raises InvalidValue. 147 // 148 // Decreasing the length of a sequence removes elements from the tail 149 // without affecting the value of those elements that remain. 150 // The new current position after decreasing the length of a sequence is determined 151 // as follows: 152 // ?f the length of the sequence is set to zero, the current position is set to -1. 153 // ?f the current position is -1 before decreasing the length, it remains at -1. 154 // ?f the current position indicates a valid element and that element is not removed 155 // when the length is decreased, the current position remains unaffected. 156 // ?f the current position indicates a valid element and that element is removed, the 157 // current position is set to -1. 158 public void set_length(int len) 159 throws org.omg.DynamicAny.DynAnyPackage.InvalidValue 160 { 161 if (status == STATUS_DESTROYED) { 162 throw wrapper.dynAnyDestroyed() ; 163 } 164 int bound = getBound(); 165 if (bound > 0 && len > bound) { 166 throw new InvalidValue(); 167 } 168 169 checkInitComponents(); 170 171 int oldLength = components.length; 172 if (len > oldLength) { 173 // Increase length 174 DynAny[] newComponents = new DynAny[len]; 175 Any[] newAnys = new Any[len]; 176 System.arraycopy(components, 0, newComponents, 0, oldLength); 177 System.arraycopy(anys, 0, newAnys, 0, oldLength); 178 components = newComponents; 179 anys = newAnys; 180 181 // Newly added elements are default-initialized 182 TypeCode contentType = getContentType(); 183 for (int i=oldLength; i<len; i++) { 184 createDefaultComponentAt(i, contentType); 185 } 186 187 // Increasing the length of a sequence sets the current position to the first 188 // newly-added element if the previous current position was -1. 189 if (index == NO_INDEX) 190 index = oldLength; 191 } else if (len < oldLength) { 192 // Decrease length 193 DynAny[] newComponents = new DynAny[len]; 194 Any[] newAnys = new Any[len]; 195 System.arraycopy(components, 0, newComponents, 0, len); 196 System.arraycopy(anys, 0, newAnys, 0, len); 197 // It is probably right not to destroy the released component DynAnys. 198 // Some other DynAny or a user variable might still hold onto them 199 // and if not then the garbage collector will take care of it. 200 //for (int i=len; i<oldLength; i++) { 201 // components[i].destroy(); 202 //} 203 components = newComponents; 204 anys = newAnys; 205 206 // ?f the length of the sequence is set to zero, the current position is set to -1. 207 // ?f the current position is -1 before decreasing the length, it remains at -1. 208 // ?f the current position indicates a valid element and that element is not removed 209 // when the length is decreased, the current position remains unaffected. 210 // ?f the current position indicates a valid element and that element is removed, 211 // the current position is set to -1. 212 if (len == 0 || index >= len) { 213 index = NO_INDEX; 214 } 215 } else { 216 // Length unchanged 217 // Maybe components is now default initialized from type code 218 if (index == NO_INDEX && len > 0) { 219 index = 0; 220 } 221 } 222 } 223 224 // Initializes the elements of the sequence. 225 // The length of the DynSequence is set to the length of value. 226 // The current position is set to zero if value has non-zero length 227 // and to -1 if value is a zero-length sequence. 228 // If the length of value exceeds the bound of a bounded sequence, 229 // the operation raises InvalidValue. 230 // If value contains one or more elements whose TypeCode is not equivalent 231 // to the element TypeCode of the DynSequence, the operation raises TypeMismatch. 232/* 233 public void set_elements(org.omg.CORBA.Any[] value) 234 throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch, 235 org.omg.DynamicAny.DynAnyPackage.InvalidValue; 236*/ 237 238 // 239 // Utility methods 240 // 241 242 protected void checkValue(Object[] value) 243 throws org.omg.DynamicAny.DynAnyPackage.InvalidValue 244 { 245 if (value == null || value.length == 0) { 246 clearData(); 247 index = NO_INDEX; 248 return; 249 } else { 250 index = 0; 251 } 252 int bound = getBound(); 253 if (bound > 0 && value.length > bound) { 254 throw new InvalidValue(); 255 } 256 } 257} 258