1 20 21 22 23 24 25 package org.snmp4j.smi; 26 27 import java.io.*; 28 import java.math.BigInteger ; 29 import org.snmp4j.asn1.BER; 30 import org.snmp4j.asn1.BERInputStream; 31 32 43 public class Counter64 extends AbstractVariable 44 implements AssignableFromLong, AssignableFromString { 45 46 private static final long serialVersionUID = 8741539680564150071L; 47 48 private long value = 0; 49 50 public Counter64() { 51 } 52 53 public Counter64(long value) { 54 setValue(value); 55 } 56 57 public void encodeBER(OutputStream outputStream) throws java.io.IOException { 58 BER.encodeUnsignedInt64(outputStream, BER.COUNTER64, value); 59 } 60 61 public void decodeBER(BERInputStream inputStream) throws java.io.IOException { 62 BER.MutableByte type = new BER.MutableByte(); 63 long newValue = BER.decodeUnsignedInt64(inputStream, type); 64 if (type.getValue() != BER.COUNTER64) { 65 throw new IOException("Wrong type encountered when decoding Counter64: " + 66 type.getValue()); 67 } 68 setValue(newValue); 69 } 70 71 public int getSyntax() { 72 return SMIConstants.SYNTAX_COUNTER64; 73 } 74 75 public int hashCode() { 76 return (int) value; 77 } 78 79 public int getBERLength() { 80 if (value < 0L) { 81 return 11; 82 } 83 if (value < 0x80000000L) { 84 if (value < 0x8000L) { 85 return (value < 0x80L) ? 3 : 4; 86 } 87 return (value < 0x800000L) ? 5 : 6; 88 } 89 if (value < 0x800000000000L) { 90 return (value < 0x8000000000L) ? 7 : 8; 91 } 92 return (value < 0x80000000000000L) ? 9 : 10; 93 } 94 95 public boolean equals(Object o) { 96 if (o instanceof Counter64) { 97 return ((Counter64) o).value == value; 98 } 99 return false; 100 } 101 102 public int compareTo(Object o) { 103 long other = ((Counter64) o).value; 104 for (int i = 63; i >= 0; i--) { 105 if (((value >> i) & 1) != 106 ((other >> i) & 1)) { 107 if (((value >> i) & 1) != 0) { 108 return 1; 109 } 110 else { 111 return -1; 112 } 113 } 114 } 115 return 0; 116 } 117 118 public String toString() { 119 if ((value > 0) && (value < Long.MAX_VALUE)) { 120 return Long.toString(value); 121 } 122 byte[] bytes = new byte[8]; 123 for (int i = 0; i < 8; i++) { 124 bytes[i] = (byte) ((value >> ((7 - i) * 8)) & 0xFF); 125 } 126 BigInteger i64 = new BigInteger (1, bytes); 127 return i64.toString(); 128 } 129 130 public void setValue(String value) { 131 this.value = Long.parseLong(value); 132 } 133 134 public void setValue(long value) { 135 this.value = value; 136 } 137 138 public long getValue() { 139 return value; 140 } 141 142 public Object clone() { 143 return new Counter64(value); 144 } 145 146 151 public void increment() { 152 value++; 153 } 154 155 public final int toInt() { 156 return (int)getValue(); 157 } 158 159 public final long toLong() { 160 return getValue(); 161 } 162 163 public OID toSubIndex(boolean impliedLength) { 164 throw new UnsupportedOperationException (); 165 } 166 167 public void fromSubIndex(OID subIndex, boolean impliedLength) { 168 throw new UnsupportedOperationException (); 169 } 170 171 } 172 | Popular Tags |