1 29 30 31 package snmp; 32 33 34 35 import java.math.*; 36 import java.io.*; 37 38 39 40 48 49 50 public class SNMPInteger extends SNMPObject 51 { 52 protected BigInteger value; 53 protected byte tag = SNMPBERCodec.SNMPINTEGER; 54 55 57 58 public SNMPInteger() 59 { 60 this(0); } 62 63 64 public SNMPInteger(long value) 65 { 66 this.value = new BigInteger(new Long (value).toString()); 67 } 68 69 70 public SNMPInteger(BigInteger value) 71 { 72 this.value = value; 73 } 74 75 76 77 78 79 84 85 protected SNMPInteger(byte[] enc) 86 throws SNMPBadValueException 87 { 88 extractValueFromBEREncoding(enc); 89 } 90 91 92 93 94 96 97 public Object getValue() 98 { 99 return value; 100 } 101 102 103 104 105 106 110 111 public void setValue(Object newValue) 112 throws SNMPBadValueException 113 { 114 if (newValue instanceof BigInteger) 115 value = (BigInteger)newValue; 116 else if (newValue instanceof Integer ) 117 value = new BigInteger(((Integer )newValue).toString()); 118 else if (newValue instanceof String ) 119 value = new BigInteger((String )newValue); 120 else 121 122 throw new SNMPBadValueException(" Integer: bad object supplied to set value "); 123 } 124 125 126 127 128 130 131 protected byte[] getBEREncoding() 132 { 133 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 134 135 byte[] data = value.toByteArray(); 138 139 byte[] len = SNMPBERCodec.encodeLength(data.length); 141 142 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 158 159 public void extractValueFromBEREncoding(byte[] enc) 160 throws SNMPBadValueException 161 { 162 try 163 { 164 value = new BigInteger(enc); 165 } 166 catch (NumberFormatException e) 167 { 168 throw new SNMPBadValueException(" Integer: bad BER encoding supplied to set value "); 169 } 170 } 171 172 173 174 public String toString() 175 { 176 return value.toString(); 177 } 179 180 181 182 public String toString(int radix) 183 { 184 return value.toString(radix); 185 } 187 188 189 190 } | Popular Tags |