1 20 21 package org.snmp4j.smi; 22 23 import java.io.*; 24 import org.snmp4j.asn1.BER; 25 import org.snmp4j.asn1.BERInputStream; 26 27 34 public class UnsignedInteger32 extends AbstractVariable 35 implements AssignableFromLong, AssignableFromString { 36 37 private static final long serialVersionUID = -2155365655395258383L; 38 39 protected long value = 0; 40 41 public UnsignedInteger32() { 42 } 43 44 52 public UnsignedInteger32(long value) { 53 setValue(value); 54 } 55 56 63 public UnsignedInteger32(int signedIntValue) { 64 setValue(signedIntValue & 0xFFFFFFFFL); 65 } 66 67 74 public UnsignedInteger32(byte signedByteValue) { 75 setValue(signedByteValue & 0xFF); 76 } 77 78 public void encodeBER(OutputStream outputStream) throws java.io.IOException { 79 BER.encodeUnsignedInteger(outputStream, BER.GAUGE, value); 80 } 81 82 public void decodeBER(BERInputStream inputStream) throws java.io.IOException { 83 BER.MutableByte type = new BER.MutableByte(); 84 long newValue = BER.decodeUnsignedInteger(inputStream, type); 85 if (type.getValue() != BER.GAUGE) { 86 throw new IOException("Wrong type encountered when decoding Gauge: "+ 87 type.getValue()); 88 } 89 setValue(newValue); 90 } 91 92 public int getSyntax() { 93 return SMIConstants.SYNTAX_UNSIGNED_INTEGER32; 94 } 95 96 public int hashCode() { 97 return (int)value; 98 } 99 100 public int getBERLength() { 101 if (value < 0x80L) { 102 return 3; 103 } 104 else if (value < 0x8000L) { 105 return 4; 106 } 107 else if (value < 0x800000L) { 108 return 5; 109 } 110 else if (value < 0x80000000L) { 111 return 6; 112 } 113 return 7; 114 } 115 116 public boolean equals(Object o) { 117 if (o instanceof UnsignedInteger32) { 118 return (((UnsignedInteger32)o).value == value); 119 } 120 return false; 121 } 122 123 public int compareTo(Object o) { 124 long diff = (value - ((UnsignedInteger32)o).getValue()); 125 if (diff < 0) { 126 return -1; 127 } 128 else if (diff > 0) { 129 return 1; 130 } 131 return 0; 132 } 133 134 public String toString() { 135 return Long.toString(value); 136 } 137 138 public final void setValue(String value) { 139 setValue(Long.parseLong(value)); 140 } 141 142 public void setValue(long value) { 143 if ((value < 0) || (value > 4294967295L)) { 144 throw new IllegalArgumentException ( 145 "Argument must be an unsigned 32bit value"); 146 } 147 this.value = value; 148 } 149 150 public long getValue() { 151 return value; 152 } 153 154 public Object clone() { 155 return new UnsignedInteger32(value); 156 } 157 158 public final int toInt() { 159 return (int)getValue(); 160 } 161 162 public final long toLong() { 163 return getValue(); 164 } 165 166 public OID toSubIndex(boolean impliedLength) { 167 return new OID(new int[] { toInt() }); 168 } 169 170 public void fromSubIndex(OID subIndex, boolean impliedLength) { 171 setValue(subIndex.getUnsigned(0)); 172 } 173 174 } 175 176 | Popular Tags |