org.snmp4j.smi
Class OID

java.lang.Object
  extended by org.snmp4j.smi.AbstractVariable
      extended by org.snmp4j.smi.OID
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, BERSerializable, AssignableFromIntArray, AssignableFromString, Variable

public class OID
extends AbstractVariable
implements AssignableFromString, AssignableFromIntArray

The Object Identifier Class. The Object Identifier (OID) class is the encapsulation of an SMI object identifier. The SMI object is a data identifier for a data element found in a Management Information Base (MIB), as defined by a MIB definition. The OID class allows definition and manipulation of object identifiers.

Version:
1.10.1
Author:
Frank Fock
See Also:
Serialized Form

Field Summary
static int MAX_OID_LEN
           
static int MAX_SUBID_VALUE
           
 
Fields inherited from class org.snmp4j.smi.AbstractVariable
SMISYNTAXES_PROPERTIES
 
Constructor Summary
OID()
          Constructs a zero length OID.
OID(int[] rawOID)
          Constructs an OID from an array of integer values.
OID(int[] prefixOID, int[] suffixOID)
          Constructs an OID from two arrays of integer values where the first represents the OID prefix (i.e., the object class ID) and the second one represents the OID suffix (i.e., the instance identifier).
OID(int[] rawOID, int offset, int length)
          Constructs an OID from an array of integer values.
OID(OID other)
          Copy constructor.
OID(java.lang.String oid)
          Constructs an OID from a dotted string.
 
Method Summary
 OID append(int subID)
          Appends a sub-identifier to this OID.
 OID append(OID oid)
          Appends an OID to this OID.
 OID append(java.lang.String oid)
          Appends a dotted String OID to this OID.
 OID appendUnsigned(long subID)
          Appends an unsigned long sub-identifier value to this OID.
 java.lang.Object clone()
          Clones this variable.
 int compareTo(java.lang.Object o)
           
 void decodeBER(BERInputStream inputStream)
          Decodes a Variable from an InputStream.
 void encodeBER(java.io.OutputStream outputStream)
          Encodes a Variable to an OutputStream.
 boolean equals(java.lang.Object o)
           
 void fromSubIndex(OID subIndex, boolean impliedLength)
          Sets the value of this Variable from the supplied (sub-)index.
 int get(int index)
          Gets the sub-identifier value at the specified position.
 int getBERLength()
          Returns the length of this Variable in bytes when encoded according to the Basic Encoding Rules (BER).
 int getSyntax()
          Gets the ASN.1 syntax identifier value of this SNMP variable.
 long getUnsigned(int index)
          Gets the unsigned sub-identifier value at the specified position.
 int[] getValue()
          Gets all sub-identifiers as an int array.
 int hashCode()
           
 boolean isValid()
          Checks whether this OID can be BER encoded.
 int last()
          Returns the last sub-identifier as an integer value.
 long lastUnsigned()
          Returns the last sub-identifier as an unsigned long value.
 int leftMostCompare(int n, OID other)
          Compares the n leftmost sub-identifiers with the given OID in left-to-right direction.
 OID mask(OctetString mask)
          Returns a copy of this OID where sub-identifiers have been set to zero for all n-th sub-identifier where the n-th bit of mask is zero.
static OID max(OID a, OID b)
          Returns the greater of the two OID values.
static OID min(OID a, OID b)
          Returns the lesser of the two OID values.
 OID nextPeer()
          Returns the next following OID with the same or lesser size (length).
 OID predecessor()
          Returns the predecessor OID for this OID.
 int removeLast()
          Removes the last sub-identifier (if available) from this OID and returns it.
 int rightMostCompare(int n, OID other)
          Compares the n rightmost sub-identifiers in direction right-to-left with those of the given OID.
 void set(int index, int value)
          Sets the sub-identifier at the specified position.
 void setValue(int[] value)
          Sets the value from an array of integer values.
 void setValue(java.lang.String value)
           
 int size()
          Returns the number of sub-identifiers in this OID.
 boolean startsWith(OID other)
          Check if the OID starts with the given OID.
 OID successor()
          Returns the successor OID for this OID.
 byte[] toByteArray()
          Returns the content of the as a byte array.
 int toInt()
          Returns an integer representation of this variable if such a representation exists.
 int[] toIntArray()
          Returns the value of this object as an int array.
 long toLong()
          Returns a long representation of this variable if such a representation exists.
 java.lang.String toString()
          Gets a string representation of the variable.
 OID toSubIndex(boolean impliedLength)
          Converts the value of this Variable to a (sub-)index value.
 OID trim()
          Returns a new copy of this OID with the last sub-indentifier removed.
 void trim(int n)
          Remove the n rightmost subidentifiers from this OID.
 
Methods inherited from class org.snmp4j.smi.AbstractVariable
createFromBER, createFromSyntax, getBERPayloadLength, getSyntaxFromString, getSyntaxString, getSyntaxString, isDynamic, isException
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_OID_LEN

public static final int MAX_OID_LEN
See Also:
Constant Field Values

MAX_SUBID_VALUE

public static final int MAX_SUBID_VALUE
See Also:
Constant Field Values
Constructor Detail

OID

public OID()
Constructs a zero length OID.


OID

public OID(java.lang.String oid)
Constructs an OID from a dotted string. The string can contain embedded strings enclosed by a single quote (') that are converted to the corresponding OIO value. For example the following OID pairs are equal:
     OID a = new OID("1.3.6.2.1.5.'hallo'.1");
     OID b = new OID("1.3.6.2.1.5.104.97.108.108.111.1");
     assertEquals(a, b);
     a = new OID("1.3.6.2.1.5.'hal.lo'.1");
     b = new OID("1.3.6.2.1.5.104.97.108.46.108.111.1");
     assertEquals(a, b);
     a = new OID("1.3.6.2.1.5.'hal.'.'''.'lo'.1");
     b = new OID("1.3.6.2.1.5.104.97.108.46.39.108.111.1");
 

Parameters:
oid - a dotted OID String, for example "1.3.6.1.2.2.1.0"

OID

public OID(int[] rawOID)
Constructs an OID from an array of integer values.

Parameters:
rawOID - an array of int values. The array is copied. Later changes to rawOID will therefore not affect the OID's value.

OID

public OID(int[] prefixOID,
           int[] suffixOID)
Constructs an OID from two arrays of integer values where the first represents the OID prefix (i.e., the object class ID) and the second one represents the OID suffix (i.e., the instance identifier).

Parameters:
prefixOID - an array of int values. The array is copied. Later changes to prefixOID will therefore not affect the OID's value.
suffixOID - an array of int values which will be appended to the prefixOID OID. The array is copied. Later changes to suffixOID will therefore not affect the OID's value.
Since:
1.8

OID

public OID(int[] rawOID,
           int offset,
           int length)
Constructs an OID from an array of integer values.

Parameters:
rawOID - an array of int values. The array is copied. Later changes to rawOID will therefore not affect the OID's value.
offset - the zero based offset into the rawOID that points to the first sub-identifier of the new OID.
length - the length of the new OID, where offset + length must be less or equal the length of rawOID. Otherwise an IndexOutOfBoundsException is thrown.

OID

public OID(OID other)
Copy constructor.

Parameters:
other - OID
Method Detail

getSyntax

public final int getSyntax()
Description copied from class: AbstractVariable
Gets the ASN.1 syntax identifier value of this SNMP variable.

Specified by:
getSyntax in interface Variable
Specified by:
getSyntax in class AbstractVariable
Returns:
an integer value < 128 for regular SMI objects and a value >= 128 for exception values like noSuchObject, noSuchInstance, and endOfMibView.

hashCode

public int hashCode()
Specified by:
hashCode in interface Variable
Specified by:
hashCode in class AbstractVariable

equals

public final boolean equals(java.lang.Object o)
Specified by:
equals in interface Variable
Specified by:
equals in class AbstractVariable

mask

public OID mask(OctetString mask)
Returns a copy of this OID where sub-identifiers have been set to zero for all n-th sub-identifier where the n-th bit of mask is zero.

Parameters:
mask - a mask where the n-th bit corresponds to the n-th sub-identifier.
Returns:
the masked OID.
Since:
1.5

compareTo

public final int compareTo(java.lang.Object o)
Specified by:
compareTo in interface java.lang.Comparable
Specified by:
compareTo in interface Variable
Specified by:
compareTo in class AbstractVariable

toString

public java.lang.String toString()
Description copied from class: AbstractVariable
Gets a string representation of the variable.

Specified by:
toString in interface Variable
Specified by:
toString in class AbstractVariable
Returns:
a string representation of the variable's value.

toByteArray

public byte[] toByteArray()
Returns the content of the as a byte array. This method can be used to convert an index value to an OctetString or IpAddress instance.

Returns:
the sub-identifies of this OID as a byte array. Each sub-identifier value is masked with 0xFF to form a byte value.
Since:
1.2

encodeBER

public void encodeBER(java.io.OutputStream outputStream)
               throws java.io.IOException
Description copied from class: AbstractVariable
Encodes a Variable to an OutputStream.

Specified by:
encodeBER in interface BERSerializable
Specified by:
encodeBER in class AbstractVariable
Parameters:
outputStream - an OutputStream.
Throws:
java.io.IOException - if an error occurs while writing to the stream.

getBERLength

public int getBERLength()
Description copied from class: AbstractVariable
Returns the length of this Variable in bytes when encoded according to the Basic Encoding Rules (BER).

Specified by:
getBERLength in interface BERSerializable
Specified by:
getBERLength in class AbstractVariable
Returns:
the BER encoded length of this variable.

decodeBER

public void decodeBER(BERInputStream inputStream)
               throws java.io.IOException
Description copied from class: AbstractVariable
Decodes a Variable from an InputStream.

Specified by:
decodeBER in interface BERSerializable
Specified by:
decodeBER in class AbstractVariable
Parameters:
inputStream - an InputStream containing a BER encoded byte stream.
Throws:
java.io.IOException - if the stream could not be decoded by using BER rules.

setValue

public void setValue(java.lang.String value)
Specified by:
setValue in interface AssignableFromString

setValue

public final void setValue(int[] value)
Sets the value from an array of integer values.

Specified by:
setValue in interface AssignableFromIntArray
Parameters:
value - The new value
Throws:
java.lang.IllegalArgumentException - if value == null.

getValue

public final int[] getValue()
Gets all sub-identifiers as an int array.

Returns:
int arry of all sub-identifiers

get

public final int get(int index)
Gets the sub-identifier value at the specified position.

Parameters:
index - a zero-based index into the OID.
Returns:
the sub-indentifier value at index. NOTE: The returned value may be negative if the sub-identifier value is greater than 2^31.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

getUnsigned

public final long getUnsigned(int index)
Gets the unsigned sub-identifier value at the specified position.

Parameters:
index - int
Returns:
the sub-indentifier value at index as an unsigned long value.

set

public final void set(int index,
                      int value)
Sets the sub-identifier at the specified position.

Parameters:
index - a zero-based index into the OID.
value - a 32bit unsigned integer value.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

append

public final OID append(java.lang.String oid)
Appends a dotted String OID to this OID.

Parameters:
oid - a dotted String with numerical sub-identifiers.
Returns:
a pointer to this OID instance (useful for chaining).

append

public final OID append(OID oid)
Appends an OID to this OID.

Parameters:
oid - an OID instance.
Returns:
a pointer to this OID instance (useful for chaining).

append

public final OID append(int subID)
Appends a sub-identifier to this OID.

Parameters:
subID - an integer value.
Returns:
a pointer to this OID instance (useful for chaining).

appendUnsigned

public final OID appendUnsigned(long subID)
Appends an unsigned long sub-identifier value to this OID.

Parameters:
subID - an unsigned long value less or equal to 2^32-1.
Returns:
a pointer to this OID instance (useful for chaining).
Since:
1.2

isValid

public boolean isValid()
Checks whether this OID can be BER encoded.

Returns:
true if size() >= 2 and size() <= 128 and if the first two sub-identifiers are less than 3 and 40 respectively.

size

public final int size()
Returns the number of sub-identifiers in this OID.

Returns:
an integer value between 0 and 128.

leftMostCompare

public int leftMostCompare(int n,
                           OID other)
Compares the n leftmost sub-identifiers with the given OID in left-to-right direction.

Parameters:
n - the number of sub-identifiers to compare.
other - an OID to compare with this OID.
Returns:
  • 0 if the first n sub-identifiers are the same.
  • <0 if the first n sub-identifiers of this OID are lexicographic less than those of the comparand.
  • >0 if the first n sub-identifiers of this OID are lexicographic greater than those of the comparand.

rightMostCompare

public int rightMostCompare(int n,
                            OID other)
Compares the n rightmost sub-identifiers in direction right-to-left with those of the given OID.

Parameters:
n - the number of sub-identifiers to compare.
other - an OID to compare with this OID.
Returns:
  • 0 if the first n sub-identifiers are the same.
  • <0 if the first n sub-identifiers of this OID are lexicographic less than those of the comparand.
  • >0 if the first n sub-identifiers of this OID are lexicographic greater than those of the comparand.

startsWith

public boolean startsWith(OID other)
Check if the OID starts with the given OID.

Parameters:
other - the OID to compare to
Returns:
false if the sub-identifiers do not match.

clone

public java.lang.Object clone()
Description copied from interface: Variable
Clones this variable. Cloning can be used by the SNMP4J API to better support concurrency by creating a immutable clone for internal processing.

Specified by:
clone in interface Variable
Specified by:
clone in class AbstractVariable
Returns:
a new instance of this Variable with the same value.

last

public final int last()
Returns the last sub-identifier as an integer value. If this OID is empty (i.e. has no sub-identifiers) then a NoSuchElementException is thrown

Returns:
the value of the last sub-identifier of this OID as an integer value. Sub-identifier values greater than 2^31-1 will be returned as negative values!
Since:
1.2

lastUnsigned

public final long lastUnsigned()
Returns the last sub-identifier as an unsigned long value. If this OID is empty (i.e. has no sub-identifiers) then a NoSuchElementException is thrown

Returns:
the value of the last sub-identifier of this OID as an unsigned long.
Since:
1.2

removeLast

public int removeLast()
Removes the last sub-identifier (if available) from this OID and returns it.

Returns:
the last sub-identifier or -1 if there is no sub-identifier left in this OID.

trim

public void trim(int n)
Remove the n rightmost subidentifiers from this OID.

Parameters:
n - the number of subidentifiers to remove. If n is zero or negative then this OID will not be changed. If n is greater than size() all subidentifiers will be removed from this OID.

trim

public OID trim()
Returns a new copy of this OID with the last sub-indentifier removed.

Returns:
a copy of this OID with n-1 sub-identifiers where n is the size of this OID and greater than zero, otherwise a zero length OID is returned.
Since:
1.11

toInt

public int toInt()
Description copied from class: AbstractVariable
Returns an integer representation of this variable if such a representation exists.

Specified by:
toInt in interface Variable
Specified by:
toInt in class AbstractVariable
Returns:
an integer value (if the native representation of this variable would be a long, then the long value will be casted to int).

toLong

public long toLong()
Description copied from class: AbstractVariable
Returns a long representation of this variable if such a representation exists.

Specified by:
toLong in interface Variable
Specified by:
toLong in class AbstractVariable
Returns:
a long value.

toSubIndex

public final OID toSubIndex(boolean impliedLength)
Description copied from class: AbstractVariable
Converts the value of this Variable to a (sub-)index value.

Specified by:
toSubIndex in interface Variable
Specified by:
toSubIndex in class AbstractVariable
Parameters:
impliedLength - specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g. OctetString and OID). For other variables it has no effect.
Returns:
an OID that represents this value as an (sub-)index.

fromSubIndex

public final void fromSubIndex(OID subIndex,
                               boolean impliedLength)
Description copied from class: AbstractVariable
Sets the value of this Variable from the supplied (sub-)index.

Specified by:
fromSubIndex in interface Variable
Specified by:
fromSubIndex in class AbstractVariable
Parameters:
subIndex - the sub-index OID.
impliedLength - specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g. OctetString and OID). For other variables it has no effect.

successor

public final OID successor()
Returns the successor OID for this OID.

Returns:
an OID clone of this OID with a zero sub-identifier appended.
Since:
1.7

predecessor

public final OID predecessor()
Returns the predecessor OID for this OID.

Returns:
if this OID ends on 0, then a MAX_OID_LEN sub-identifier OID is returned where each sub-ID for index greater or equal to size() is set to MAX_SUBID_VALUE.
Since:
1.7

nextPeer

public final OID nextPeer()
Returns the next following OID with the same or lesser size (length).

Returns:
OID the next OID on the same or upper level or a clone of this OID, if it has a zero length or is 2^32-1.
Since:
1.7

max

public static final OID max(OID a,
                            OID b)
Returns the greater of the two OID values.

Parameters:
a - an OID.
b - an OID.
Returns:
a if a >= b, b otherwise.
Since:
1.7

min

public static final OID min(OID a,
                            OID b)
Returns the lesser of the two OID values.

Parameters:
a - an OID.
b - an OID.
Returns:
a if a <= b, b otherwise.
Since:
1.7

toIntArray

public int[] toIntArray()
Description copied from interface: AssignableFromIntArray
Returns the value of this object as an int array.

Specified by:
toIntArray in interface AssignableFromIntArray
Returns:
an int array.

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.