snmpobjectid.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 711 行 · 第 1/2 页

JAVA
711
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.// // Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Size = 8//package org.opennms.protocols.snmp;import java.io.Serializable;import org.opennms.protocols.snmp.asn1.AsnDecodingException;import org.opennms.protocols.snmp.asn1.AsnEncoder;import org.opennms.protocols.snmp.asn1.AsnEncodingException;/** * Defines the SNMP object identifier class for naming variables. An object * identifier is a sequence of numbers that correspond to branches in the * Management Information Base (MIB). Each vendor is free to define their own * branch of the tree. The SnmpObjectId class provides an interface for naming * those tree instances. *  * @author <a href="mailto:weave@oculan.com">Brian Weaver </a> * @author <A HREF="mailto:naz@personalgenie.com">Nazario Irizarry, Jr. </A> * @version 1.1.1.1 *  */public class SnmpObjectId extends Object implements SnmpSyntax, Cloneable, Serializable {    /**     * Deifnes the version of the serialization format.     *      */    static final long serialVersionUID = 2633631219460364065L;    /**     * The array of object identifiers minimum length for a valid object id is 2     * (.0.0)     */    private int[] m_data;    /**     * Converts a textual object identifier to an array of integer values.     *      * @param idstr     *            An object identifier string     *      * @return Returns an array of integers converted from the string. If an     *         error occurs then a null is returned.     */    private static int[] convert(String idstr) {        //        // ids is the counter        // idArray is the array of characters        //        int numIds = 0;        char[] idArray = idstr.toCharArray();        //        // if the length is equal to zero then        // the number of ids is equal to zero :)        //        if (idArray.length == 0) {            int[] tmp = new int[2];            tmp[0] = tmp[1] = 0;            return tmp;        }        //        // if the object string does not start        // with a dot then we need to increment        // the id count        //        if (idArray[0] != '.')            numIds++;        //        // count the number of objects        //        int x = 0;        while (x < idArray.length) {            if (idArray[x++] == '.')                ++numIds;        }        //        // check for bad strings        //        if (numIds == 0) {            int[] tmp = new int[2];            tmp[0] = tmp[1] = 0;            return tmp;        }        //        // get an array to store objects into        //        int[] objects = new int[numIds];        int objectsNdx = 0; // reset the ids counter        int idArrayNdx = 0; // set the objects ndx counter        //        // if the string begins with a dot(.) then        // increment the ndx        //        if (idArray[0] == '.')            ++idArrayNdx;        //        // create an object id variable and        // set it equal to zero        //        int oid = 0;        while (idArrayNdx < idArray.length) {            //            // if there is a dot(.) then            // store the id            //            if (idArray[idArrayNdx] == '.') {                objects[objectsNdx++] = oid;                oid = 0;            } else {                //                // multiply the object id by 10                //                oid *= 10;                switch (idArray[idArrayNdx]) {                case '1':                    oid += 1;                    break;                case '2':                    oid += 2;                    break;                case '3':                    oid += 3;                    break;                case '4':                    oid += 4;                    break;                case '5':                    oid += 5;                    break;                case '6':                    oid += 6;                    break;                case '7':                    oid += 7;                    break;                case '8':                    oid += 8;                    break;                case '9':                    oid += 9;                    break;                }            }            ++idArrayNdx;        }        //        // save the last object id        //        objects[objectsNdx++] = oid;        return objects;    }    /**     * Defines the SNMP SMI type for this particular object.     */    public static final byte ASNTYPE = SnmpSMI.SMI_OBJECTID;    /**     * Creates a default empty object identifier.     *      */    public SnmpObjectId() {        m_data = new int[2];        m_data[0] = m_data[1] = 0;    }    /**     * Creates an object identifier from the passed array of identifiers. If the     * passed argument is null then a default object id (.0.0) is created for     * the instance.     *      * @param data     *            The array of object identifiers     *      */    public SnmpObjectId(int[] data) {        this();        if (data != null) {            m_data = new int[data.length];            System.arraycopy(data, 0, m_data, 0, data.length);        }    }    /**     * Creates a duplicate object. The passed object identifier is copied into     * the newly created object.     *      * @param second     *            The object to copy     *      */    public SnmpObjectId(SnmpObjectId second) {        this(second.m_data);    }    /**     * Creates an object identifier from the pased dotted decimal object     * identifier string. The string is converted to the internal     * representation. If the conversion fails then a default (.0.0) object     * identifier is assigned to the object.     *      * @param strOid     *            The dotted decimal object identifier string     *      */    public SnmpObjectId(String strOid) {        m_data = convert(strOid);        if (m_data == null) {            m_data = new int[2];            m_data[0] = m_data[1] = 0;        }    }    /**     * Gets the number of object identifiers in the object.     *      * @return Returns the number of object identifiers     *      */    public int getLength() {        return m_data.length;    }        /**     * Returns the value of the last object identifier component value     */    public int getLastIdentifier() {        return m_data[m_data.length-1];    }    /**     * Gets the array of object identifiers from the object. The instance is     * returned as a reference. The caller should not make any modifications to     * the returned list.     *      * @return Returns the list of identifiers     */    public int[] getIdentifiers() {        return m_data;    }    /**     * Sets the object to the passed object identifier     *      * @param data     *            The new object identifier     *      */    public void setIdentifiers(int[] data) {        if (data != null) {            m_data = new int[data.length];            System.arraycopy(data, 0, m_data, 0, data.length);        } else {            m_data = new int[2];            m_data[0] = m_data[1] = 0;        }    }    /**     * Sets the object to the passed dotted decimal object identifier string.     *      * @param strOid     *            The dotted decimal object identifier.     *      */    public void setIdentifiers(String strOid) {        m_data = null;        if (strOid != null) {            m_data = convert(strOid);        }        if (m_data == null) {            m_data = new int[2];            m_data[0] = m_data[1] = 0;        }    }    /**     * Appends the specified identifiers to the current object.     *      * @param ids     *            The array of identifiers to append     *      */    public void append(int[] ids) {        if (ids != null && ids.length != 0) {            int[] tmp = new int[m_data.length + ids.length];            System.arraycopy(m_data, 0, tmp, 0, m_data.length);            System.arraycopy(ids, 0, tmp, m_data.length, ids.length);            m_data = tmp;        }    }    /**     * Converts the passed string to an object identifier and appends them to     * the current object.     *      * @param strOids     *            The dotted decimal identifiers to append     *      */    public void append(String strOids) {        int[] tmp = convert(strOids);        append(tmp);    }    /**     * Appends the passed SnmpObjectId object to self.     *      * @param second     *            The object to append to self     *      */    public void append(SnmpObjectId second) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?