001: /*
002: * SNMP Package
003: *
004: * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
005: *
006: * This is free software. Redistribution and use in source and binary forms, with
007: * or without modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright notice, this
011: * list of conditions and the following disclaimer.
012: * 2. Redistributions in binary form must reproduce the above copyright notice,
013: * this list of conditions and the following disclaimer in the documentation
014: * and/or other materials provided with the distribution.
015: * 3. The name of the author may not be used to endorse or promote products
016: * derived from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
019: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
023: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
025: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
026: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: */
029:
030: package snmp;
031:
032: import java.math.*;
033:
034: /**
035: * Defines a 32-bit unsigned integer value; wraps if initialized with a larger value.
036: * @see snmp.SNMPInteger
037: */
038: public class SNMPUInteger32 extends SNMPInteger {
039: // maximum value is 2^32 - 1
040: private static BigInteger maxValue = new BigInteger("4294967295");
041:
042: /**
043: * Initialize value to 0.
044: */
045:
046: public SNMPUInteger32() {
047: this (0); // initialize value to 0
048: }
049:
050: /**
051: * Initialize value to newValue; wrap if newValue too big for 32 bits.
052: */
053:
054: public SNMPUInteger32(long newValue) {
055: tag = SNMPBERCodec.SNMPUINTEGER32;
056:
057: value = new BigInteger(new Long(newValue).toString());
058:
059: // wrap if value > maxValue
060: value = value.mod(maxValue);
061: }
062:
063: /**
064: * Used to initialize from the BER encoding, usually received in a response from
065: * an SNMP device responding to an SNMPGetRequest.
066: * @throws SNMPBadValueException Indicates an invalid BER encoding supplied. Shouldn't
067: * occur in normal operation, i.e., when valid responses are received from devices.
068: */
069:
070: protected SNMPUInteger32(byte[] enc) throws SNMPBadValueException {
071: tag = SNMPBERCodec.SNMPUINTEGER32;
072:
073: extractValueFromBEREncoding(enc);
074:
075: // wrap if value > maxValue
076: value = value.mod(maxValue);
077: }
078:
079: /**
080: * Used to set the value with an instance of java.lang.Integer or
081: * java.lang.BigInteger. The value of the constructed SNMPUInteger32 object is the
082: * supplied value mod 2^32.
083: * @throws SNMPBadValueException Indicates an incorrect object type supplied.
084: */
085:
086: public void setValue(Object newValue) throws SNMPBadValueException {
087: if (newValue instanceof BigInteger) {
088: value = (BigInteger) newValue;
089: value = value.mod(maxValue); // wrap when value exceeds 2^32
090: } else if (newValue instanceof Integer) {
091: value = value = new BigInteger(newValue.toString());
092: value = value.mod(maxValue); // wrap when value exceeds 2^32
093: } else if (newValue instanceof String) {
094: value = value = new BigInteger((String) newValue);
095: value = value.mod(maxValue); // wrap when value exceeds 2^32
096: } else
097: throw new SNMPBadValueException(
098: " Unsigned Integer: bad object supplied to set value ");
099: }
100:
101: }
|