001: /*_############################################################################
002: _##
003: _## SNMP4J - Counter64.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.smi;
022:
023: import java.io.*;
024: import java.math.BigInteger;
025: import org.snmp4j.asn1.BER;
026: import org.snmp4j.asn1.BERInputStream;
027:
028: /**
029: * The <code>Counter64</code> class represents a 64bit unsigned integer type.
030: * It is used for monotonically increasing values that wrap around at
031: * 2^64-1 (18446744073709551615).
032: * <p>
033: * The unsigned 64bit value is represented by a signed 64bit long value
034: * internally.
035: *
036: * @author Frank Fock
037: * @version 1.8
038: */
039: public class Counter64 extends AbstractVariable implements
040: AssignableFromLong, AssignableFromString {
041:
042: private static final long serialVersionUID = 8741539680564150071L;
043:
044: private long value = 0;
045:
046: public Counter64() {
047: }
048:
049: public Counter64(long value) {
050: setValue(value);
051: }
052:
053: public void encodeBER(OutputStream outputStream)
054: throws java.io.IOException {
055: BER.encodeUnsignedInt64(outputStream, BER.COUNTER64, value);
056: }
057:
058: public void decodeBER(BERInputStream inputStream)
059: throws java.io.IOException {
060: BER.MutableByte type = new BER.MutableByte();
061: long newValue = BER.decodeUnsignedInt64(inputStream, type);
062: if (type.getValue() != BER.COUNTER64) {
063: throw new IOException(
064: "Wrong type encountered when decoding Counter64: "
065: + type.getValue());
066: }
067: setValue(newValue);
068: }
069:
070: public int getSyntax() {
071: return SMIConstants.SYNTAX_COUNTER64;
072: }
073:
074: public int hashCode() {
075: return (int) value;
076: }
077:
078: public int getBERLength() {
079: if (value < 0L) {
080: return 11;
081: }
082: if (value < 0x80000000L) {
083: if (value < 0x8000L) {
084: return (value < 0x80L) ? 3 : 4;
085: }
086: return (value < 0x800000L) ? 5 : 6;
087: }
088: if (value < 0x800000000000L) {
089: return (value < 0x8000000000L) ? 7 : 8;
090: }
091: return (value < 0x80000000000000L) ? 9 : 10;
092: }
093:
094: public boolean equals(Object o) {
095: if (o instanceof Counter64) {
096: return ((Counter64) o).value == value;
097: }
098: return false;
099: }
100:
101: public int compareTo(Object o) {
102: long other = ((Counter64) o).value;
103: for (int i = 63; i >= 0; i--) {
104: if (((value >> i) & 1) != ((other >> i) & 1)) {
105: if (((value >> i) & 1) != 0) {
106: return 1;
107: } else {
108: return -1;
109: }
110: }
111: }
112: return 0;
113: }
114:
115: public String toString() {
116: if ((value > 0) && (value < Long.MAX_VALUE)) {
117: return Long.toString(value);
118: }
119: byte[] bytes = new byte[8];
120: for (int i = 0; i < 8; i++) {
121: bytes[i] = (byte) ((value >> ((7 - i) * 8)) & 0xFF);
122: }
123: BigInteger i64 = new BigInteger(1, bytes);
124: return i64.toString();
125: }
126:
127: public void setValue(String value) {
128: this .value = Long.parseLong(value);
129: }
130:
131: public void setValue(long value) {
132: this .value = value;
133: }
134:
135: public long getValue() {
136: return value;
137: }
138:
139: public Object clone() {
140: return new Counter64(value);
141: }
142:
143: /**
144: * Increment the value of the counter by one. If the current value is
145: * 2^63-1 (9223372036854775807) then value will be set to -2^63. Nevertheless,
146: * the BER encoded value of this counter will always be unsigned!
147: */
148: public void increment() {
149: value++;
150: }
151:
152: public final int toInt() {
153: return (int) getValue();
154: }
155:
156: public final long toLong() {
157: return getValue();
158: }
159:
160: public OID toSubIndex(boolean impliedLength) {
161: throw new UnsupportedOperationException();
162: }
163:
164: public void fromSubIndex(OID subIndex, boolean impliedLength) {
165: throw new UnsupportedOperationException();
166: }
167:
168: }
|