1 package org.jacorb.util; 2 3 22 23 import java.util.*; 24 25 29 30 public class ObjectUtil 31 { 32 private static Class identityHashMapClass = null; 33 private static final char[] lookup = 35 new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 36 40 public static final String readURL( String url ) 41 throws java.io.IOException 42 { 43 String token = "file://"; 44 String line = ""; 45 java.io.InputStreamReader isr = null; 46 if (url.startsWith (token)) 47 try 48 { 49 isr = new java.io.FileReader (url.substring(token.length())); 50 } 51 catch (Exception e) 52 { 53 System.out.println ("Tried and failed to open file: " + 54 url.substring(token.length())); 55 } 57 if (isr == null) 58 { 59 java.net.URL u = new java.net.URL (url); 60 isr = new java.io.InputStreamReader (u.openStream()); 61 } 62 63 java.io.BufferedReader in = new java.io.BufferedReader (isr); 64 line = in.readLine(); 65 66 in.close(); 67 return line; 68 } 69 70 113 114 public static Class classForName(String name) 115 throws ClassNotFoundException , IllegalArgumentException 116 { 117 if (name == null) 118 throw new IllegalArgumentException ("Class name must not be null!"); 119 try 120 { 121 return Thread.currentThread().getContextClassLoader().loadClass(name); 126 } 127 catch (Exception e) 128 { 129 return Class.forName(name); 134 } 135 } 136 137 143 public static Map createIdentityHashMap() 144 { 145 if (identityHashMapClass == null) 146 { 147 try 148 { 149 identityHashMapClass = 150 ObjectUtil.classForName("java.util.IdentityHashMap"); 151 } 152 catch (ClassNotFoundException ex) 153 { 154 try 155 { 156 identityHashMapClass = 157 ObjectUtil.classForName("org.jacorb.util.IdentityHashMap"); 158 } 159 catch (ClassNotFoundException e) 160 { 161 throw new RuntimeException (e.toString()); 162 } 163 } 164 } 165 try 166 { 167 return (Map)identityHashMapClass.newInstance(); 168 } 169 catch (Exception exc) 170 { 171 throw new RuntimeException (exc.toString()); 172 } 173 } 174 175 public static synchronized String bufToString( byte bs[], 176 int start, 177 int len) 178 { 179 StringBuffer result = new StringBuffer (); 180 StringBuffer chars = new StringBuffer (); 181 182 for ( int i = start; i < (start + len); i++ ) 183 { 184 if ((i % 16 ) == 0) 185 { 186 result.append( chars.toString() ); 187 chars = new StringBuffer (); 188 } 189 190 chars.append( toAscii( bs[i] )); 191 result.append( toHex( bs[i] )); 192 193 if ( (i % 4) == 3 ) 194 { 195 chars.append( ' ' ); 196 result.append( ' ' ); 197 } 198 } 199 200 if ( len % 16 != 0 ) 201 { 202 int pad = 0; 203 int delta_bytes = 16 - (len % 16); 204 205 pad = delta_bytes * 3; 208 209 pad += (delta_bytes / 4); 211 212 for ( int i = 0; i < pad; i++ ) 213 { 214 chars.insert( 0, ' ' ); 215 } 216 } 217 218 result.append( chars.toString()); 219 return result.toString(); 220 } 221 222 223 229 230 public static final String toHex(byte b) 231 { 232 StringBuffer sb = new StringBuffer (); 233 234 int upper = (b >> 4) & 0x0F; 235 sb.append( lookup[upper] ); 236 237 int lower = b & 0x0F; 238 sb.append( lookup[lower] ); 239 240 sb.append( ' ' ); 241 242 return sb.toString(); 243 } 244 245 246 public static final char toAscii(byte b) 247 { 248 if ( b > (byte) 31 && b < (byte) 127) 249 { 250 return (char) b; 251 } 252 else 253 { 254 return '.'; 255 } 256 } 257 258 263 264 public static java.util.Properties argsToProps(String [] args) 265 { 266 java.util.Properties props = new java.util.Properties (); 267 268 for( int i = 0; i < args.length; i++ ) 269 { 270 if (args[i].startsWith("-D")) 271 { 272 int idx = args[i].indexOf('='); 273 if (idx < 3 ) 274 continue; 275 String key = args[i].substring(2,idx); 276 277 System.out.println("putting: " + key + "," + args[i].substring(idx+1)); 278 props.put(key, args[i].substring(idx+1)); 279 } 280 } 281 return props; 282 } 283 284 285 286 287 } 288 | Popular Tags |