java.lang.Object
org.snmp4j.smi.AbstractVariable
org.snmp4j.smi.OID
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<Variable>
,BERSerializable
,AssignableFromIntArray
,AssignableFromString
,Variable
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:
- 3.0
- Author:
- Frank Fock
- See Also:
-
Field Summary
Fields inherited from class org.snmp4j.smi.AbstractVariable
SMISYNTAXES_PROPERTIES
-
Constructor Summary
ConstructorDescriptionOID()
Constructs a zero length OID.OID
(int[] rawOID) Constructs anOID
from an array of integer values.OID
(int[] prefixOID, int suffixID) Constructs anOID
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[] prefixOID, int[] suffixOID) Constructs anOID
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 anOID
from an array of integer values.Constructs anOID
from a dotted string.Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal OID
append
(int subID) Appends a sub-identifier to this OID.final OID
Appends a dotted String OID to thisOID
.final OID
Appends anOID
to this OID.final OID
appendUnsigned
(long subID) Appends an unsigned long sub-identifier value to this OID.clone()
Clones this variable.final int
void
decodeBER
(BERInputStream inputStream) Decodes aVariable
from anBERInputStream
.void
encodeBER
(OutputStream outputStream) Encodes aVariable
to anOutputStream
.final boolean
format()
Format the OID as text.final void
fromSubIndex
(OID subIndex, boolean impliedLength) Sets the value of thisVariable
from the supplied (sub-)index.final int
get
(int index) Gets the sub-identifier value at the specified position.int
Returns the length of thisVariable
in bytes when encoded according to the Basic Encoding Rules (BER).Get the suffix of this OID that exceeds the given OID.final int
Gets the ASN.1 syntax identifier value of this SNMP variable.final long
getUnsigned
(int index) Gets the unsigned sub-identifier value at the specified position.final int[]
getValue()
Gets all sub-identifiers as an int array.int
hashCode()
boolean
isValid()
Checks whether thisOID
can be BER encoded.final int
last()
Returns the last sub-identifier as an integer value.final long
Returns the last sub-identifier as an unsigned long value.int
leftMostCompare
(int n, OID other) Compares the n leftmost sub-identifiers with the givenOID
in left-to-right direction.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
Returns the greater of the two OID values.static OID
Returns the lesser of the two OID values.final OID
nextPeer()
Returns the next following OID with the same or lesser size (length).final OID
Returns the predecessor OID for this OID.int
Removes the last sub-identifier (if available) from thisOID
and returns it.int
rightMostCompare
(int n, OID other) Compares the n rightmost sub-identifiers in direction right-to-left with those of the givenOID
.final void
set
(int index, int value) Sets the sub-identifier at the specified position.final void
setValue
(int[] value) Sets the value from an array of integer values.void
final int
size()
Returns the number of sub-identifiers in thisOID
.boolean
startsWith
(OID other) Check if the OID starts with the given OID.subOID
(int beginIndex) Returns a new OID that is a sub-sequence of this OID starting atbeginIndex
until the end of this OID.subOID
(int beginIndex, int endIndex) Returns a new OID that is a sub-sequence of this OID.final OID
Returns the successor OID for this OID.byte[]
Returns the content of the as a byte array.Returns the OID as dotted string (e.g., "1.3.6.1.4.1") regardless of whatOIDTextFormat
instance is set inSNMP4JSettings
.int
toInt()
Returns an integer representation of this variable if such a representation exists.int[]
Returns the value of this object as an int array.long
toLong()
Returns a long representation of this variable if such a representation exists.toString()
Return a string representation that can be parsed again to thisOID
byOID(String)
.final OID
toSubIndex
(boolean impliedLength) Converts the value of thisVariable
to a (sub-)index value.long[]
Return this OID as array of unsigned long values.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, equal, getBERPayloadLength, getSyntaxFromString, getSyntaxString, getSyntaxString, isDynamic, isException
-
Field Details
-
MAX_OID_LEN
public static final int MAX_OID_LEN- See Also:
-
MAX_SUBID_VALUE
public static final int MAX_SUBID_VALUE- See Also:
-
-
Constructor Details
-
OID
public OID()Constructs a zero length OID. -
OID
Constructs anOID
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 anOID
from an array of integer values.- Parameters:
rawOID
- an array ofint
values. The array is copied. Later changes torawOID
will therefore not affect the OID's value.
-
OID
public OID(int[] prefixOID, int[] suffixOID) Constructs anOID
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 ofint
values. The array is copied. Later changes toprefixOID
will therefore not affect the OID's value.suffixOID
- an array ofint
values which will be appended to theprefixOID
OID. The array is copied. Later changes tosuffixOID
will therefore not affect the OID's value.- Since:
- 1.8
-
OID
public OID(int[] prefixOID, int suffixID) Constructs anOID
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 ofint
values. The array is copied. Later changes toprefixOID
will therefore not affect the OID's value.suffixID
- anint
value that will be appended to theprefixOID
OID. The array is copied. Later changes toprefixOID
will therefore not affect the OID's value.- Since:
- 2.2.6
-
OID
public OID(int[] rawOID, int offset, int length) Constructs anOID
from an array of integer values.- Parameters:
rawOID
- an array ofint
values. The array is copied. Later changes torawOID
will therefore not affect the OID's value.offset
- the zero based offset into therawOID
that points to the first sub-identifier of the new OID.length
- the length of the new OID, whereoffset + length
must be less or equal the length ofrawOID
. Otherwise anIndexOutOfBoundsException
is thrown.
-
OID
Copy constructor.- Parameters:
other
- OID
-
-
Method Details
-
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 interfaceVariable
- Specified by:
getSyntax
in classAbstractVariable
- Returns:
- an integer value less than 128 for regular SMI objects and a value greater or equal than 128 for exception values like noSuchObject, noSuchInstance, and endOfMibView.
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceVariable
- Specified by:
hashCode
in classAbstractVariable
-
equals
- Specified by:
equals
in interfaceVariable
- Specified by:
equals
in classAbstractVariable
-
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
- Specified by:
compareTo
in interfaceComparable<Variable>
- Specified by:
compareTo
in interfaceVariable
- Specified by:
compareTo
in classAbstractVariable
-
toString
Return a string representation that can be parsed again to thisOID
byOID(String)
.- Specified by:
toString
in interfaceVariable
- Specified by:
toString
in classAbstractVariable
- Returns:
- a formatted string representation of this OID (e.g. "ifDescr.1") that
can be parsed again as defined by
OIDTextFormat.formatForRoundTrip(int[])
inSNMP4JSettings
.
-
format
Format the OID as text. This could return to same result astoString()
but also fully converted index-to-text values likesnmp4jLogLoggerIndex.org.snmp4j.MessageDispatcherImpl
.- Returns:
- a string representation of this OID as defined by the
OIDTextFormat.format(int[])
inSNMP4JSettings
. - Since:
- 2.2
-
toDottedString
Returns the OID as dotted string (e.g., "1.3.6.1.4.1") regardless of whatOIDTextFormat
instance is set inSNMP4JSettings
.- Returns:
- a dotted string representation of this OID value.
- Since:
- 2.2
-
toByteArray
public byte[] toByteArray()Returns the content of the as a byte array. This method can be used to convert an index value to anOctetString
orIpAddress
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
Description copied from class:AbstractVariable
Encodes aVariable
to anOutputStream
.- Specified by:
encodeBER
in interfaceBERSerializable
- Specified by:
encodeBER
in classAbstractVariable
- Parameters:
outputStream
- anOutputStream
.- Throws:
IOException
- if an error occurs while writing to the stream.
-
getBERLength
public int getBERLength()Description copied from class:AbstractVariable
Returns the length of thisVariable
in bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERLength
in interfaceBERSerializable
- Specified by:
getBERLength
in classAbstractVariable
- Returns:
- the BER encoded length of this variable.
-
decodeBER
Description copied from class:AbstractVariable
Decodes aVariable
from anBERInputStream
.- Specified by:
decodeBER
in interfaceBERSerializable
- Specified by:
decodeBER
in classAbstractVariable
- Parameters:
inputStream
- anBERInputStream
containing a BER encoded byte stream.- Throws:
IOException
- if the stream could not be decoded by using BER rules.
-
setValue
- Specified by:
setValue
in interfaceAssignableFromString
-
setValue
public final void setValue(int[] value) Sets the value from an array of integer values.- Specified by:
setValue
in interfaceAssignableFromIntArray
- Parameters:
value
- The new value- Throws:
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 theOID
.- Returns:
- the sub-indentifier value at
index
. NOTE: The returned value may be negative if the sub-identifier value is greater than2^31
. - Throws:
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-identifier 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 theOID
.value
- a 32bit unsigned integer value.- Throws:
ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()).
-
append
Appends a dotted String OID to thisOID
.- Parameters:
oid
- a dotted String with numerical sub-identifiers.- Returns:
- a pointer to this OID instance (useful for chaining).
-
append
Appends anOID
to this OID.- Parameters:
oid
- anOID
instance.- Returns:
- a pointer to this OID instance (useful for chaining).
-
append
Appends a sub-identifier to this OID.- Parameters:
subID
- an integer value.- Returns:
- a pointer to this OID instance (useful for chaining).
-
appendUnsigned
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 thisOID
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 thisOID
.- Returns:
- an integer value between 0 and 128.
-
leftMostCompare
Compares the n leftmost sub-identifiers with the givenOID
in left-to-right direction.- Parameters:
n
- the number of sub-identifiers to compare.other
- anOID
to compare with thisOID
.- Returns:
- 0 if the first
n
sub-identifiers are the same. - <0 if the first
n
sub-identifiers of thisOID
are lexicographic less than those of the comparand. - >0 if the first
n
sub-identifiers of thisOID
are lexicographic greater than those of the comparand.
- 0 if the first
-
rightMostCompare
Compares the n rightmost sub-identifiers in direction right-to-left with those of the givenOID
.- Parameters:
n
- the number of sub-identifiers to compare.other
- anOID
to compare with thisOID
.- Returns:
- 0 if the first
n
sub-identifiers are the same. - <0 if the first
n
sub-identifiers of thisOID
are lexicographic less than those of the comparand. - >0 if the first
n
sub-identifiers of thisOID
are lexicographic greater than those of the comparand.
- 0 if the first
-
startsWith
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
Description copied from interface:Variable
Clones this variable. Cloning can be used by the SNMP4J API to better support concurrency by creating a clone for internal processing. The content of this object is independent to the content of the clone. Thus, changes to the clone will have no effect to this object.- Specified by:
clone
in interfaceVariable
- Specified by:
clone
in classAbstractVariable
- 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 aNoSuchElementException
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 aNoSuchElementException
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 thisOID
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. Ifn
is zero or negative then this OID will not be changed. Ifn
is greater thansize()
all subidentifiers will be removed from this 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 wheren
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 interfaceVariable
- Specified by:
toInt
in classAbstractVariable
- 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 interfaceVariable
- Specified by:
toLong
in classAbstractVariable
- Returns:
- a long value.
-
toSubIndex
Description copied from class:AbstractVariable
Converts the value of thisVariable
to a (sub-)index value.- Specified by:
toSubIndex
in interfaceVariable
- Specified by:
toSubIndex
in classAbstractVariable
- Parameters:
impliedLength
- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetString
andOID
). For other variables it has no effect.- Returns:
- an OID that represents this value as an (sub-)index.
-
fromSubIndex
Description copied from class:AbstractVariable
Sets the value of thisVariable
from the supplied (sub-)index.- Specified by:
fromSubIndex
in interfaceVariable
- Specified by:
fromSubIndex
in classAbstractVariable
- 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
andOID
). For other variables it has no effect.
-
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
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 tosize()
is set toMAX_SUBID_VALUE
. - Since:
- 1.7
-
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
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
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 interfaceAssignableFromIntArray
- Returns:
- an int array.
-
toUnsignedLongArray
public long[] toUnsignedLongArray()Return this OID as array of unsigned long values.- Returns:
- an array of unsigned long values.
- Since:
- 3.0
-
getSuffix
Get the suffix of this OID that exceeds the given OID. This operation can be used to determine the row index of a table cell. For examplegetSuffix(OID)
on the OIDifDescr.2
with the prefix parameterifDescr
will return the OID 2. Likewise,getSuffix(OID)
on the OID1.3.6.1.4
with prefix parameter1.3.6
will return1.4
.- Parameters:
prefix
- the prefix to left-most trim from this OID to form the result. This OID will not be modified though.- Returns:
- If this OID does not start with prefix,
null
is returned. Otherwise, the suffix that extends this OID compared withprefix
is returned. If both OIDs equal, a zero length OID is returned. - Since:
- 3.0
-
subOID
Returns a new OID that is a sub-sequence of this OID starting atbeginIndex
until the end of this OID.- Parameters:
beginIndex
- the begin index, inclusive.- Returns:
- the specified sub OID.
- Since:
- 3.4.1
-
subOID
Returns a new OID that is a sub-sequence of this OID.- Parameters:
beginIndex
- the begin index, inclusive.endIndex
- the end index, exclusive.- Returns:
- the specified sub OID.
- Since:
- 3.4.1
-