1 29 30 31 package snmp; 32 33 import java.util.*; 34 import java.io.*; 35 36 40 41 public class SNMPObjectIdentifier extends SNMPObject 42 { 43 private long[] digits; 45 protected byte tag = SNMPBERCodec.SNMPOBJECTIDENTIFIER; 46 47 50 51 public SNMPObjectIdentifier() 52 { 53 digits = new long[0]; 54 } 55 56 57 58 59 64 65 public SNMPObjectIdentifier(String digitString) 66 throws SNMPBadValueException 67 { 68 convertDigitString(digitString); 69 } 70 71 72 73 74 79 80 public SNMPObjectIdentifier(int[] digits) 81 throws SNMPBadValueException 82 { 83 long[] longDigits = new long[digits.length]; 84 85 for (int i = 0; i < digits.length; i++) 86 { 87 if (digits[i] < 0) 88 throw new SNMPBadValueException("Negative value supplied for SNMPObjectIdentifier."); 89 90 longDigits[i] = digits[i]; 91 } 92 93 this.digits = longDigits; 94 } 95 96 97 98 99 104 105 public SNMPObjectIdentifier(long[] digits) 106 throws SNMPBadValueException 107 { 108 109 for (int i = 0; i < digits.length; i++) 110 { 111 if (digits[i] < 0) 112 throw new SNMPBadValueException("Negative value supplied for SNMPObjectIdentifier."); 113 } 114 115 this.digits = digits; 116 } 117 118 119 120 121 127 128 protected SNMPObjectIdentifier(byte[] enc) 129 throws SNMPBadValueException 130 { 131 extractFromBEREncoding(enc); 132 } 133 134 135 136 137 140 141 public Object getValue() 142 { 143 return digits; 144 } 145 146 147 148 149 150 156 157 public void setValue(Object digits) 158 throws SNMPBadValueException 159 { 160 if (digits instanceof long[]) 161 { 162 for (int i = 0; i < ((long[])digits).length; i++) 163 { 164 if (((long[])digits)[i] < 0) 165 throw new SNMPBadValueException("Negative value supplied for SNMPObjectIdentifier."); 166 } 167 168 this.digits = (long[])digits; 169 } 170 else if (digits instanceof int[]) 171 { 172 long[] longDigits = new long[((int[])digits).length]; 173 174 for (int i = 0; i < ((int[])digits).length; i++) 175 { 176 if (((int[])digits)[i] < 0) 177 throw new SNMPBadValueException("Negative value supplied for SNMPObjectIdentifier."); 178 179 longDigits[i] = ((int[])digits)[i]; 180 } 181 182 this.digits = longDigits; 183 } 184 else if (digits instanceof String ) 185 { 186 convertDigitString((String )digits); 187 } 188 else 189 throw new SNMPBadValueException(" Object Identifier: bad object supplied to set value "); 190 } 191 192 193 194 195 198 199 protected byte[] getBEREncoding() 200 { 201 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 202 203 byte type = SNMPBERCodec.SNMPOBJECTIDENTIFIER; 204 205 byte[] data = encodeArray(); 207 208 byte[] len = SNMPBERCodec.encodeLength(data.length); 210 211 outBytes.write(type); 213 outBytes.write(len, 0, len.length); 214 outBytes.write(data, 0, data.length); 215 216 return outBytes.toByteArray(); 217 } 218 219 220 221 private byte[] encodeArray() 222 { 223 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 224 225 int numElements = digits.length; 226 227 if (numElements >= 2) 230 { 231 outBytes.write((byte)(40*digits[0] + digits[1])); 232 } 233 else if (numElements ==1) 234 { 235 outBytes.write((byte)(40*digits[0])); 236 } 237 238 239 for (int i = 2; i < numElements; ++i) 240 { 241 byte[] nextBytes = encodeValue(digits[i]); 242 outBytes.write(nextBytes, 0, nextBytes.length); 243 } 244 245 246 return outBytes.toByteArray(); 247 } 248 249 250 251 private byte[] encodeValue(long v) 252 { 253 int numBytes = 0; 257 long temp = v; 258 259 do 260 { 261 ++numBytes; 262 temp = (long)Math.floor(temp / 128); 263 } 264 while (temp > 0); 265 266 267 byte[] enc = new byte[numBytes]; 268 enc[numBytes-1] = (byte)(v % 128); 270 v = (long)Math.floor(v / 128); 271 272 for (int i = numBytes-2; i >= 0; --i) 274 { 275 enc[i] = (byte)((v % 128) + 128); 276 v = (long)Math.floor(v / 128); 277 } 278 279 return enc; 280 } 281 282 283 284 private void convertDigitString(String digitString) 285 throws SNMPBadValueException 286 { 287 try 288 { 289 StringTokenizer st = new StringTokenizer(digitString, " ."); 290 int size = 0; 291 292 while (st.hasMoreTokens()) 293 { 294 size++; 296 st.nextToken(); 297 } 298 299 long[] returnDigits = new long[size]; 300 301 st = new StringTokenizer(digitString, " ."); 302 303 for (int i = 0; i < size; i++) 304 { 305 returnDigits[i] = Long.parseLong(st.nextToken()); 306 if (returnDigits[i] < 0) 307 throw new SNMPBadValueException(" Object Identifier: bad string supplied to set value "); 308 } 309 310 digits = returnDigits; 311 312 } 313 catch (NumberFormatException e) 314 { 315 throw new SNMPBadValueException(" Object Identifier: bad string supplied for object identifier value "); 316 } 317 318 319 } 320 321 322 323 324 private void extractFromBEREncoding(byte[] enc) 325 throws SNMPBadValueException 326 { 327 int bitTest = 0x80; int highBitMask = 0x7F; 331 int numInts = 0; 334 for (int i = 0; i < enc.length; i++) 335 { 336 if ((enc[i] & bitTest) == 0) numInts++; 338 } 339 340 341 if (numInts > 0) 342 { 343 digits = new long[numInts + 1]; 346 347 int currentByte = -1; 349 long value = 0; 350 351 do 353 { 354 currentByte++; 355 value = value*128 + (enc[currentByte] & highBitMask); 356 } 357 while ((enc[currentByte] & bitTest) > 0); 359 digits[0] = (long)Math.floor(value / 40); 361 digits[1] = value % 40; 362 363 for (int i = 2; i < numInts + 1; i++) 365 { 366 value = 0; 368 do 369 { 370 currentByte++; 371 value = value*128 + (enc[currentByte] & highBitMask); 372 } 373 while ((enc[currentByte] & bitTest) > 0); 374 375 digits[i] = value; 376 } 377 378 } 379 else 380 { 381 digits = new long[0]; 383 } 384 385 } 386 387 388 389 390 417 418 419 420 421 424 425 public boolean equals(Object other) 426 { 427 if (other == null) 429 { 430 return false; 431 } 432 433 if (!this.getClass().equals(other.getClass())) 435 { 436 return false; 437 } 438 439 SNMPObjectIdentifier otherSNMPObject = (SNMPObjectIdentifier)other; 440 441 if (java.util.Arrays.equals((long[])this.getValue(),(long[])otherSNMPObject.getValue())) 443 { 444 return true; 445 } 446 else 447 { 448 return false; 449 } 450 } 451 452 453 454 457 458 public int hashCode() 459 { 460 int hash = 0; 461 462 for (int i = 0; i < digits.length; i++) 464 { 465 hash += (int)(digits[i] ^ (digits[i] >> 32)); 466 hash += (hash << 10); 467 hash ^= (hash >> 6); 468 } 469 470 hash += (hash << 3); 471 hash ^= (hash >> 11); 472 hash += (hash << 15); 473 474 return hash; 475 } 476 477 478 479 482 483 public String toString() 484 { 485 StringBuffer valueStringBuffer = new StringBuffer (); 486 if (digits.length > 0) 487 { 488 valueStringBuffer.append(digits[0]); 489 490 for (int i = 1; i < digits.length; ++i) 491 { 492 valueStringBuffer.append("."); 493 valueStringBuffer.append(digits[i]); 494 } 495 } 496 497 498 return valueStringBuffer.toString(); 499 } 500 501 502 503 504 } | Popular Tags |