📄 snmpoid.java
字号:
/** * Performs a clone action. This provides a workaround for the * <CODE>SnmpValue</CODE> interface. * @return The SnmpValue clone. */ final synchronized public SnmpValue duplicate() { return (SnmpValue)clone() ; } /** * Clones the <CODE>SnmpOid</CODE> object, making a copy of its data. * @return The object clone. */ public Object clone() { try { SnmpOid obj = (SnmpOid)super.clone() ; obj.components = new long[this.componentCount] ; System.arraycopy(this.components, 0, obj.components, 0, this.componentCount) ; return obj ; } catch (CloneNotSupportedException e) { throw new InternalError() ; // should never happen. VM bug. } } /** * Inserts a subid at the beginning of this <CODE>SnmpOid</CODE>. * @param id The long subid to insert. */ public void insert(long id) { enlargeIfNeeded(1) ; for (int i = componentCount - 1 ; i >= 0 ; i--) { components[i + 1] = components[i] ; } components[0] = id ; componentCount++ ; } /** * Inserts a subid at the beginning of this <CODE>SnmpOid</CODE>. * @param id The integer subid to insert. */ public void insert(int id) { insert((long)id) ; } /** * Appends the specified <CODE>SnmpOid</CODE> to the end of this <CODE>SnmpOid</CODE>. * @param oid The OID to append. */ public void append(SnmpOid oid) { enlargeIfNeeded(oid.componentCount) ; for (int i = 0 ; i < oid.componentCount ; i++) { components[componentCount + i] = oid.components[i] ; } componentCount += oid.componentCount ; } /** * Appends the specified long to the end of this <CODE>SnmpOid</CODE>. * @param id The long to append. */ public void append(long id) { enlargeIfNeeded(1) ; components[componentCount] = id ; componentCount++ ; } /** * Adds the specified dot-formatted OID <CODE>String</CODE> to the end of this <CODE>SnmpOid</CODE>. * The subidentifiers can be expressed as a dot-formatted <CODE>String</CODE> or a * MIB variable name. * @param s Variable name of the form .1.2.3 or 1.2.3 or * <CODE>ifInOctets</CODE>. * @exception SnmpStatusException An error occurred while accessing a MIB node. */ public void addToOid(String s) throws SnmpStatusException { SnmpOid suffix= new SnmpOid(s); this.append(suffix); } /** * Adds the specified array of longs to the end of this <CODE>SnmpOid</CODE>. * @param oid An array of longs. * @exception SnmpStatusException An error occurred while accessing a MIB node. */ public void addToOid(long []oid) throws SnmpStatusException { SnmpOid suffix= new SnmpOid(oid); this.append(suffix); } /** * Checks the validity of the OID. * @return <CODE>true</CODE> if the OID is valid, <CODE>false</CODE> otherwise. */ public boolean isValid() { return ((componentCount >= 2) && ((0 <= components[0]) && (components[0] < 3)) && ((0 <= components[1]) && (components[1] < 40))) ; } /** * Checks whether the specified <CODE>Object</CODE> is equal to this <CODE>SnmpOid</CODE>. * @param o The <CODE>Object</CODE> to be compared. * @return <CODE>true</CODE> if <CODE>o</CODE> is an <CODE>SnmpOid</CODE> instance and equal to this, <CODE>false</CODE> otherwise. */ public boolean equals(Object o) { boolean result = false ; if (o instanceof SnmpOid) { SnmpOid oid = (SnmpOid)o ; if (oid.componentCount == componentCount) { int i = 0 ; long[] objoid = oid.components; while ((i < componentCount) && (components[i] == objoid[i])) i++ ; result = (i == componentCount) ; } } return result ; } /** * The hashCode is computed from the OID components. * @return a hashCode for this SnmpOid. **/ public int hashCode() { long acc=0; for (int i=0;i<componentCount;i++) { acc = acc*31+components[i]; } return (int)acc; } /** * Compares two OIDs lexicographically. * @param other The OID to be compared. * @return * The value 0 if the parameter <CODE>other</CODE> is equal to this <CODE>SnmpOid</CODE>. * A value smaller than 0 if this <CODE>SnmpOid</CODE> is lexicographically smaller than <CODE>other</CODE>. * A value larger than 0 if this <CODE>SnmpOid</CODE> is lexicographically larger than <CODE>other</CODE>. */ public int compareTo(SnmpOid other) { int result = 0 ; int i = 0 ; int cmplen = Math.min(componentCount, other.componentCount) ; long[] otheroid = other.components; for (i = 0; i < cmplen; i++) { if (components[i] != otheroid[i]) { break ; } } if ((i == componentCount) && (i == other.componentCount)) { result = 0 ; } else if (i == componentCount) { result = -1 ; } else if (i == other.componentCount) { result = 1 ; } else { result = (components[i] < otheroid[i]) ? -1 : 1 ; } return result ; } /** * Resolves a MIB variable <CODE>String</CODE> with the MIB database. * @param s The variable name to resolve. * @exception SnmpStatusException If the variable is not found in the MIB database. */ public String resolveVarName(String s) throws SnmpStatusException { int index = s.indexOf('.') ; // First handle the case where oid is expressed as 1.2.3.4 // try { return handleLong(s, index); } catch(NumberFormatException e) {} // if we are here, it means we have something to resolve.. // if (meta == null) throw new SnmpStatusException(SnmpStatusException.noSuchName); // Ok assume there is a variable name to resolve ... // if (index <= 0) { SnmpOidRecord rec = meta.resolveVarName(s); return rec.getOid(); } else { SnmpOidRecord rec = meta.resolveVarName(s.substring(0, index)); return (rec.getOid()+ s.substring(index)); } } /** * Returns a textual description of the type object. * @return ASN.1 textual description. */ public String getTypeName() { return name ; } /** * Returns the MIB table used for resolving MIB variable names. * @return The MIB table. */ public static SnmpOidTable getSnmpOidTable() { return meta; } /** * Sets the MIB table to use for resolving MIB variable names. * If no mib table is available, the class will not be able to resolve * names contained in the Object Identifier. * @param db The MIB table to use. */ public static void setSnmpOidTable(SnmpOidTable db) { meta = db; } /** * Converts an OID index converted string back to a DisplayString * **/ public String toOctetString() { return new String(tobyte()) ; } // PRIVATE METHODS //------------------ /** * convert the components array into a byte array **/ private byte[] tobyte() { byte[] result = new byte[componentCount] ; for (int i =0 ; i < componentCount ; i++) { if (components[0] > 255) { throw new IllegalArgumentException() ; } result[i] = (byte)components[i] ; } return result ; } /** * Checks if there is enough space in the components * array to insert n new subids. If not, it increases the size of * the array. * In fact it reallocates a new array and copy the old one into the new one. * @param n The number of subids to insert. */ private void enlargeIfNeeded(int n) { int neededSize = components.length ; while (componentCount + n > neededSize) { neededSize = neededSize * 2 ; } if (neededSize > components.length) { long[] newComponents = new long[neededSize] ; for (int i = 0 ; i < components.length ; i++) { newComponents[i] = components[i] ; } components = newComponents ; } } // PRIVATE METHODS //---------------- private String handleLong(String oid, int index) throws NumberFormatException, SnmpStatusException { String str; if (index >0) { str= oid.substring(0, index); } else { str= oid ; } // just parse the element. // Long.parseLong(str); return oid; } // VARIABLES //---------- /** * The components' array. * @serial */ protected long components[] = null ; /** * The length of the components' array. * @serial */ protected int componentCount = 0 ; /** * The name of the type. */ final static String name = "Object Identifier"; /** * Reference to a mib table. If no mib table is available, * the class will not be able to resolve names contained in the Object Identifier. */ private static SnmpOidTable meta= null; /** * Ensure serialization compatibility with version 4.1 FCS * */ static final long serialVersionUID = 8956237235607885096L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -