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: import java.io.*;
034:
035: /** Defines an arbitrarily-sized integer value; there is no limit on size due to the use
036: * of Java.lang.BigInteger to store the value internally. For an indicator which "pegs" at its
037: * maximum value if initialized with a larger value, use SNMPGauge32; for a counter which wraps,
038: * use SNMPCounter32 or SNMPCounter64.
039: * @see snmp.SNMPCounter32
040: * @see snmp.SNMPGauge32
041: * @see snmp.SNMPCounter64
042: */
043:
044: public class SNMPInteger extends SNMPObject {
045: protected BigInteger value;
046: protected byte tag = SNMPBERCodec.SNMPINTEGER;
047:
048: /** Initialize value to 0.
049: */
050:
051: public SNMPInteger() {
052: this (0); // initialize value to 0
053: }
054:
055: public SNMPInteger(long value) {
056: this .value = new BigInteger(new Long(value).toString());
057: }
058:
059: public SNMPInteger(BigInteger value) {
060: this .value = value;
061: }
062:
063: /** Used to initialize from the BER encoding, usually received in a response from
064: * an SNMP device responding to an SNMPGetRequest.
065: * @throws SNMPBadValueException Indicates an invalid BER encoding supplied. Shouldn't
066: * occur in normal operation, i.e., when valid responses are received from devices.
067: */
068:
069: protected SNMPInteger(byte[] enc) throws SNMPBadValueException {
070: extractValueFromBEREncoding(enc);
071: }
072:
073: /** Returns a java.lang.BigInteger object with the current value.
074: */
075:
076: public Object getValue() {
077: return value;
078: }
079:
080: /** Used to set the value with an instance of java.lang.Integer or
081: * java.lang.BigInteger.
082: * @throws SNMPBadValueException Indicates an incorrect object type supplied.
083: */
084:
085: public void setValue(Object newValue) throws SNMPBadValueException {
086: if (newValue instanceof BigInteger)
087: value = (BigInteger) newValue;
088: else if (newValue instanceof Integer)
089: value = new BigInteger(((Integer) newValue).toString());
090: else if (newValue instanceof String)
091: value = new BigInteger((String) newValue);
092: else
093:
094: throw new SNMPBadValueException(
095: " Integer: bad object supplied to set value ");
096: }
097:
098: /** Returns the full BER encoding (type, length, value) of the SNMPInteger subclass.
099: */
100:
101: protected byte[] getBEREncoding() {
102: ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
103:
104: // write contents
105: // boy, was THIS easy! Love that Java!
106: byte[] data = value.toByteArray();
107:
108: // calculate encoding for length of data
109: byte[] len = SNMPBERCodec.encodeLength(data.length);
110:
111: // encode T,L,V info
112: outBytes.write(tag);
113: outBytes.write(len, 0, len.length);
114: outBytes.write(data, 0, data.length);
115:
116: return outBytes.toByteArray();
117: }
118:
119: /** Used to extract a value from the BER encoding of the value. Called in constructors for
120: * SNMPInteger subclasses.
121: * @throws SNMPBadValueException Indicates an invalid BER encoding supplied. Shouldn't
122: * occur in normal operation, i.e., when valid responses are received from devices.
123: */
124:
125: public void extractValueFromBEREncoding(byte[] enc)
126: throws SNMPBadValueException {
127: try {
128: value = new BigInteger(enc);
129: } catch (NumberFormatException e) {
130: throw new SNMPBadValueException(
131: " Integer: bad BER encoding supplied to set value ");
132: }
133: }
134:
135: public String toString() {
136: return value.toString();
137: // return new String(value.toString());
138: }
139:
140: public String toString(int radix) {
141: return value.toString(radix);
142: // return new String(value.toString());
143: }
144:
145: }
|