1 24 25 package org.objectweb.dream.util; 26 27 import java.io.DataInput ; 28 import java.io.DataOutput ; 29 import java.io.IOException ; 30 import java.io.ObjectInput ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutput ; 33 import java.io.ObjectOutputStream ; 34 35 import org.objectweb.fractal.api.Component; 36 import org.objectweb.fractal.api.NoSuchInterfaceException; 37 import org.objectweb.fractal.api.control.ContentController; 38 import org.objectweb.fractal.util.Fractal; 39 40 43 public final class Util 44 { 45 46 private Util() 47 { 48 } 49 50 59 public static Component getComponentByName(ContentController cc, String name) 60 { 61 String [] compNames = name.split("/"); 62 ContentController contentController = cc; 63 Component c = null; 65 for (int i = 0; i < compNames.length; i++) 66 { 67 c = null; 68 Component[] comps = cc.getFcSubComponents(); 69 for (int j = 0; j < comps.length; j++) 70 { 71 Component component = comps[j]; 72 try 73 { 74 String tempName = Fractal.getNameController(component).getFcName(); 75 if (tempName.equals(compNames[i])) 76 { 77 c = component; 78 cc = Fractal.getContentController(component); 79 break; 80 } 81 } 82 catch (NoSuchInterfaceException e) 83 { 84 } 86 } 87 if (c == null) 88 { 89 return null; 90 } 91 } 92 return c; 93 } 94 95 96 public static final Object NULL_OBJECT = new Object (); 97 98 108 public static void writeExternalByteArray(DataOutput out, byte[] array) 109 throws IOException 110 { 111 if (array == null) 112 { 113 out.writeShort(-1); 114 } 115 else 116 { 117 short length = (short) array.length; 118 out.writeShort(length); 119 out.write(array); 120 } 121 } 122 123 131 public static byte[] readExternalByteArray(DataInput in) throws IOException 132 { 133 short length = in.readShort(); 134 if (length == -1) 135 { 136 return null; 137 } 138 else 139 { 140 byte[] toReturn = new byte[length]; 141 in.readFully(toReturn); 142 return toReturn; 143 } 144 } 145 146 156 public static void writeExternalIntArray(DataOutput out, int[] array) 157 throws IOException 158 { 159 if (array == null) 160 { 161 out.writeShort(-1); 162 } 163 else 164 { 165 short length = (short) array.length; 166 out.writeShort(length); 167 for (int i = 0; i < length; i++) 168 { 169 out.writeInt(array[i]); 170 } 171 } 172 } 173 174 182 public static int[] readExternalIntArray(DataInput in) throws IOException 183 { 184 short length = in.readShort(); 185 if (length == -1) 186 { 187 return null; 188 } 189 else 190 { 191 int[] toReturn = new int[length]; 192 for (int i = 0; i < length; i++) 193 { 194 toReturn[i] = in.readInt(); 195 } 196 return toReturn; 197 } 198 } 199 200 210 public static void writeExternalLongArray(DataOutput out, long[] array) 211 throws IOException 212 { 213 if (array == null) 214 { 215 out.writeShort(-1); 216 } 217 else 218 { 219 short length = (short) array.length; 220 out.writeShort(length); 221 for (int i = 0; i < length; i++) 222 { 223 out.writeLong(array[i]); 224 } 225 } 226 } 227 228 235 public static long[] readExternalLongArray(DataInput in) throws IOException 236 { 237 short length = in.readShort(); 238 if (length == -1) 239 { 240 return null; 241 } 242 else 243 { 244 long[] toReturn = new long[length]; 245 for (int i = 0; i < length; i++) 246 { 247 toReturn[i] = in.readLong(); 248 } 249 return toReturn; 250 } 251 } 252 253 261 public static void writeObject(ObjectOutput output, Object o) 262 throws IOException 263 { 264 if (output instanceof ObjectOutputStream ) 265 { 266 ((ObjectOutputStream ) output).writeUnshared(o); 267 } 268 else 269 { 270 output.writeObject(o); 271 } 272 } 273 274 282 public static Object readObject(ObjectInput input) throws IOException , 283 ClassNotFoundException 284 { 285 if (input instanceof ObjectInputStream ) 286 { 287 return ((ObjectInputStream ) input).readUnshared(); 288 } 289 else 290 { 291 return input.readObject(); 292 } 293 } 294 } | Popular Tags |