KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPInteger


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33
34
35 import java.math.*;
36 import java.io.*;
37
38
39
40 /** Defines an arbitrarily-sized integer value; there is no limit on size due to the use
41 * of Java.lang.BigInteger to store the value internally. For an indicator which "pegs" at its
42 * maximum value if initialized with a larger value, use SNMPGauge32; for a counter which wraps,
43 * use SNMPCounter32 or SNMPCounter64.
44 * @see snmp.SNMPCounter32
45 * @see snmp.SNMPGauge32
46 * @see snmp.SNMPCounter64
47 */

48
49
50 public class SNMPInteger extends SNMPObject
51 {
52     protected BigInteger value;
53     protected byte tag = SNMPBERCodec.SNMPINTEGER;
54     
55     /** Initialize value to 0.
56     */

57     
58     public SNMPInteger()
59     {
60         this(0); // initialize value to 0
61
}
62     
63     
64     public SNMPInteger(long value)
65     {
66         this.value = new BigInteger(new Long JavaDoc(value).toString());
67     }
68     
69     
70     public SNMPInteger(BigInteger value)
71     {
72         this.value = value;
73     }
74     
75     
76     
77     
78     
79     /** Used to initialize from the BER encoding, usually received in a response from
80     * an SNMP device responding to an SNMPGetRequest.
81     * @throws SNMPBadValueException Indicates an invalid BER encoding supplied. Shouldn't
82     * occur in normal operation, i.e., when valid responses are received from devices.
83     */

84     
85     protected SNMPInteger(byte[] enc)
86         throws SNMPBadValueException
87     {
88         extractValueFromBEREncoding(enc);
89     }
90     
91     
92     
93     
94     /** Returns a java.lang.BigInteger object with the current value.
95     */

96     
97     public Object JavaDoc getValue()
98     {
99         return value;
100     }
101     
102     
103     
104     
105     
106     /** Used to set the value with an instance of java.lang.Integer or
107     * java.lang.BigInteger.
108     * @throws SNMPBadValueException Indicates an incorrect object type supplied.
109     */

110     
111     public void setValue(Object JavaDoc newValue)
112         throws SNMPBadValueException
113     {
114         if (newValue instanceof BigInteger)
115             value = (BigInteger)newValue;
116         else if (newValue instanceof Integer JavaDoc)
117             value = new BigInteger(((Integer JavaDoc)newValue).toString());
118         else if (newValue instanceof String JavaDoc)
119             value = new BigInteger((String JavaDoc)newValue);
120         else
121             
122             throw new SNMPBadValueException(" Integer: bad object supplied to set value ");
123     }
124     
125     
126     
127     
128     /** Returns the full BER encoding (type, length, value) of the SNMPInteger subclass.
129     */

130     
131     protected byte[] getBEREncoding()
132     {
133         ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
134         
135         // write contents
136
// boy, was THIS easy! Love that Java!
137
byte[] data = value.toByteArray();
138         
139         // calculate encoding for length of data
140
byte[] len = SNMPBERCodec.encodeLength(data.length);
141         
142         // encode T,L,V info
143
outBytes.write(tag);
144         outBytes.write(len, 0, len.length);
145         outBytes.write(data, 0, data.length);
146     
147         return outBytes.toByteArray();
148     }
149     
150     
151     
152     
153     /** Used to extract a value from the BER encoding of the value. Called in constructors for
154     * SNMPInteger subclasses.
155     * @throws SNMPBadValueException Indicates an invalid BER encoding supplied. Shouldn't
156     * occur in normal operation, i.e., when valid responses are received from devices.
157     */

158     
159     public void extractValueFromBEREncoding(byte[] enc)
160         throws SNMPBadValueException
161     {
162         try
163         {
164             value = new BigInteger(enc);
165         }
166         catch (NumberFormatException JavaDoc e)
167         {
168             throw new SNMPBadValueException(" Integer: bad BER encoding supplied to set value ");
169         }
170     }
171     
172     
173     
174     public String JavaDoc toString()
175     {
176         return value.toString();
177         // return new String(value.toString());
178
}
179     
180     
181     
182     public String JavaDoc toString(int radix)
183     {
184         return value.toString(radix);
185         // return new String(value.toString());
186
}
187     
188     
189     
190 }
Popular Tags