1 21 22 package com.coi.tools.os.win; 23 24 import java.io.Serializable ; 25 26 37 public class RegDataContainer implements Cloneable , Serializable , MSWinConstants 38 { 39 40 private static final long serialVersionUID = 3979265850388066865L; 41 42 private static final int[] VALID_TYPES = { 0, 1, 2, 3, 4, 6, 7}; 43 44 private long dwordData = 0; 45 46 private String stringData = null; 47 48 private String [] multiStringData = null; 49 50 private byte[] binData = null; 51 52 private int type = 0; 53 54 57 public RegDataContainer() 58 { 59 super(); 60 } 61 62 68 public RegDataContainer(int type) throws IllegalArgumentException 69 { 70 super(); 71 if (!isValidType(type)) throw new IllegalArgumentException ("Type is not valid"); 72 73 this.type = type; 74 } 75 76 81 public RegDataContainer(long data) 82 { 83 super(); 84 type = REG_DWORD; 85 dwordData = data; 86 } 87 88 93 public RegDataContainer(String data) 94 { 95 super(); 96 type = REG_SZ; 97 stringData = data; 98 } 99 100 105 public RegDataContainer(String [] data) 106 { 107 super(); 108 type = REG_MULTI_SZ; 109 multiStringData = data; 110 } 111 112 117 public RegDataContainer(byte[] data) 118 { 119 super(); 120 type = REG_BINARY; 121 binData = data; 122 } 123 124 130 public byte[] getBinData() 131 { 132 return binData; 133 } 134 135 141 public long getDwordData() 142 { 143 return dwordData; 144 } 145 146 152 public String [] getMultiStringData() 153 { 154 return multiStringData; 155 } 156 157 163 public String getStringData() 164 { 165 return stringData; 166 } 167 168 173 public int getType() 174 { 175 return type; 176 } 177 178 183 public void setBinData(byte[] bytes) 184 { 185 binData = bytes; 186 } 187 188 193 public void setDwordData(long i) 194 { 195 dwordData = i; 196 } 197 198 203 public void setMultiStringData(String [] strings) 204 { 205 multiStringData = strings; 206 } 207 208 213 public void setStringData(String string) 214 { 215 stringData = string; 216 } 217 218 223 public void setType(int i) 224 { 225 type = i; 226 } 227 228 234 public boolean isValidType(int type0) 235 { 236 for (int i = 0; i < VALID_TYPES.length; ++i) 237 if (type0 == VALID_TYPES[i]) return (true); 238 return (false); 239 240 } 241 242 248 public Object getDataAsObject() 249 { 250 switch (type) 251 { 252 case REG_SZ: 253 case REG_EXPAND_SZ: 254 return (getStringData()); 255 case REG_BINARY: 256 return (getBinData()); 257 case REG_DWORD: 258 return (new Long (getDwordData())); 259 case REG_MULTI_SZ: 260 return (getMultiStringData()); 261 default: 262 return (null); 263 } 264 } 265 266 public Object clone() throws CloneNotSupportedException 267 { 268 RegDataContainer retval = (RegDataContainer) super.clone(); 269 if (multiStringData != null) 270 { 271 retval.multiStringData = new String [multiStringData.length]; 272 System.arraycopy(multiStringData, 0, retval.multiStringData, 0, multiStringData.length); 273 } 274 if (binData != null) 275 { 276 retval.binData = new byte[binData.length]; 277 System.arraycopy(binData, 0, retval.binData, 0, binData.length); 278 } 279 return (retval); 280 } 281 282 public boolean equals(Object anObject) 283 { 284 if (this == anObject) return (true); 285 if (anObject instanceof RegDataContainer) 286 { 287 RegDataContainer other = (RegDataContainer) anObject; 288 if (other.type != type) return (false); 289 switch (type) 290 { 291 case REG_DWORD: 292 return (other.dwordData == dwordData); 293 case REG_SZ: 294 case REG_EXPAND_SZ: 295 if (stringData == null) return (other.stringData == null); 296 return (stringData.equals(other.stringData)); 297 case REG_BINARY: 298 if (binData == null) return (other.binData == null); 299 if (other.binData != null && binData.length == other.binData.length) 300 { 301 for (int i = 0; i < binData.length; ++i) 302 { 303 if (binData[i] != other.binData[i]) return (false); 304 } 305 return (true); 306 } 307 return (false); 308 case REG_MULTI_SZ: 309 if (multiStringData == null) return (other.multiStringData == null); 310 if (other.multiStringData != null 311 && multiStringData.length == other.multiStringData.length) 312 { 313 for (int i = 0; i < multiStringData.length; ++i) 314 { 315 if (multiStringData[i] != null) 316 { 317 if (!multiStringData[i].equals(other.multiStringData[i])) 318 return (false); 319 } 320 else if (other.multiStringData[i] == null) return (false); 321 } 322 return (true); 323 } 324 return (false); 325 } 326 } 327 return (false); 328 } 329 330 public int hashCode() 331 { 332 int result; 333 result = (int) (dwordData ^ (dwordData >>> 32)); 334 result = 29 * result + (stringData != null ? stringData.hashCode() : 0); 335 result = 29 * result + type; 336 return result; 337 } 338 339 } 340 | Popular Tags |