📄 snmpoid.java
字号:
/* * @(#)file SnmpOid.java * @(#)author Sun Microsystems, Inc. * @(#)version 4.23 * @(#)date 08/07/21 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */// Copyright (c) 1995-96 by Cisco Systems, Inc.package com.sun.jmx.snmp;// java imports//import java.util.StringTokenizer;import java.util.NoSuchElementException;/** * Represents an SNMP Object Identifier (OID). * * <p><b>This API is a Sun Microsystems internal API and is subject * to change without notice.</b></p> * @version 4.23 11/17/05 * @author Sun Microsystems, Inc * @author Cisco Systems, Inc. */public class SnmpOid extends SnmpValue { // CONSTRUCTORS //------------- /** * Constructs a new <CODE>SnmpOid</CODE> with no components. */ public SnmpOid() { components = new long[15] ; componentCount = 0 ; } /** * Constructs a new <CODE>SnmpOid</CODE> from the specified component array. * @param oidComponents The initialization component array. */ public SnmpOid(long[] oidComponents) { components = (long[])oidComponents.clone() ; componentCount = components.length ; } /** * Constructs a new <CODE>SnmpOid</CODE> containing one component with the * specified value. * @param id The initialization component value. */ public SnmpOid(long id) { components = new long[1] ; components[0] = id ; componentCount = components.length ; } /** * Constructs a new <CODE>SnmpOid</CODE> containing four components * with the specified values. * @param id1 The first component value. * @param id2 The second component values. * @param id3 The third component values. * @param id4 The fourth component values. */ public SnmpOid(long id1, long id2, long id3, long id4) { components = new long[4] ; components[0] = id1 ; components[1] = id2 ; components[2] = id3 ; components[3] = id4 ; componentCount = components.length ; } /** * Constructs a new <CODE>SnmpOid</CODE> from a dot-formatted <CODE>String</CODE> or a MIB variable * name. It generates an exception if the variable name cannot be resolved, or * if the dot-formatted <CODE>String</CODE> has an invalid subidentifier. * This constructor helps build an OID object with a <CODE>String</CODE> like .1.2.3.4 or 1.2.3.4 * or <CODE>ifInOctets</CODE> or <CODE>ifInOctets</CODE>.0. * @param s <CODE>String</CODE> or MIB variable of the form .1.2.3 or 1.2.3 or <CODE>ifInOctets</CODE>. * @exception IllegalArgumentException The subidentifier is neither a numeric <CODE>String</CODE> * nor a <CODE>String</CODE> of the MIB database. */ public SnmpOid(String s) throws IllegalArgumentException { String dotString = s ; if (s.startsWith(".") == false) { try { dotString = resolveVarName(s); } catch(SnmpStatusException e) { throw new IllegalArgumentException(e.getMessage()); } } StringTokenizer st = new StringTokenizer(dotString, ".", false) ; componentCount= st.countTokens(); // Now extract the ids // if (componentCount == 0) { components = new long[15] ; } else { components = new long[componentCount] ; try { for (int i = 0 ; i < componentCount ; i++) { try { components[i] = Long.parseLong(st.nextToken()) ; } catch(NoSuchElementException e) {} } } catch(NumberFormatException e) { throw new IllegalArgumentException(s) ; } } } // PUBLIC METHODS //--------------- /** * Gets the number of components in this OID. * @return The number of components. */ public int getLength() { return componentCount ; } /** * Returns a copy of the components array of this <CODE>SnmpOid</CODE>. * @return The copy of the components array. */ public long[] longValue() { long[] result = new long[componentCount] ; System.arraycopy(components,0,result,0,componentCount); return result ; } /** * Returns the components array of this <CODE>SnmpOid</CODE>. * If <code>duplicate</code> is true, a copy is returned. * Otherwise, a reference to the internal array is returned, * in which case the caller <b>shall not</b> modify this array. * This method is provided to optimize processing in those cases * where the caller needs only to read the components array. * * @param duplicate Indicates whether a copy or a reference must * be returned: * <li><code>true</code> if a copy must be returned,</li> * <li><code>false</code> if a reference on the internal data * can be returned.</li> * @return A copy of (or a reference on) the components array. */ public final long[] longValue(boolean duplicate) { if (duplicate) return longValue(); if (componentCount == components.length) return components ; components = longValue(); componentCount = components.length; return components ; } /** * Returns the value of the OID arc found at the requested position * in the <CODE>components</CODE> array. The first element is at * position <code>0</code>. * * @param pos The position at which the OID arc should be peeked. * * @return The OID arc found at the requested position. * * @exception SnmpStatusException No OID arc was found at the requested * position. */ public final long getOidArc(int pos) throws SnmpStatusException { try { return components[pos]; } catch(Exception e) { throw new SnmpStatusException(SnmpStatusException.noAccess); } } /** * Converts the OID value to its <CODE>Long</CODE> form. * @return The <CODE>Long</CODE> representation of the value. */ public Long toLong() { if (componentCount != 1) { throw new IllegalArgumentException() ; } return new Long(components[0]) ; } /** * Converts the OID value to its <CODE>Integer</CODE> form. * @return The <CODE>Integer</CODE> representation of the value. */ public Integer toInteger() { if ((componentCount != 1) || (components[0] > Integer.MAX_VALUE)) { throw new IllegalArgumentException() ; } return new Integer((int)components[0]) ; } /** * Converts the OID value to its <CODE>String</CODE> form. * @return The <CODE>String</CODE> representation of the value. */ public String toString() { String result = "" ; if (componentCount >= 1) { for (int i = 0 ; i < componentCount - 1 ; i++) { result = result + components[i] + "." ; } result = result + components[componentCount - 1] ; } return result ; } /** * Converts the OID value to its <CODE>Boolean</CODE> form. * @return The <CODE>Boolean</CODE> representation of the value. */ public Boolean toBoolean() { if ((componentCount != 1) && (components[0] != 1) && (components[0] != 2)) { throw new IllegalArgumentException() ; } return new Boolean(components[0] == 1) ; } /** * Converts the OID value to its array of <CODE>Bytes</CODE> form. * @return The array of <CODE>Bytes</CODE> representation of the value. */ public Byte[] toByte() { Byte[] result = new Byte[componentCount] ; for (int i =0 ; i < componentCount ; i++) { if (components[0] > 255) { throw new IllegalArgumentException() ; } result[i] = new Byte((byte)components[i]) ; } return result ; } /** * Converts the OID value to its <CODE>SnmpOid</CODE> form. * @return The OID representation of the value. */ public SnmpOid toOid() { long[] ids = new long[componentCount] ; for (int i = 0 ; i < componentCount ; i++) { ids[i] = components[i] ; } return new SnmpOid(ids) ; } /** * Extracts the OID from an index OID and returns its * value converted as an <CODE>SnmpOid</CODE>. * @param index The index array. * @param start The position in the index array. * @return The OID representing the OID value. * @exception SnmpStatusException There is no OID value * available at the start position. */ public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { try { if (index[start] > Integer.MAX_VALUE) { throw new SnmpStatusException(SnmpStatusException.noSuchName) ; } int idCount = (int)index[start++] ; long[] ids = new long[idCount] ; for (int i = 0 ; i < idCount ; i++) { ids[i] = index[start + i] ; } return new SnmpOid(ids) ; } catch(IndexOutOfBoundsException e) { throw new SnmpStatusException(SnmpStatusException.noSuchName) ; } } /** * Scans an index OID, skips the OID value and returns the position * of the next value. * @param index The index array. * @param start The position in the index array. * @return The position of the next value. * @exception SnmpStatusException There is no OID value * available at the start position. */ public static int nextOid(long[] index, int start) throws SnmpStatusException { try { if (index[start] > Integer.MAX_VALUE) { throw new SnmpStatusException(SnmpStatusException.noSuchName) ; } int idCount = (int)index[start++] ; start += idCount ; if (start <= index.length) { return start ; } else { throw new SnmpStatusException(SnmpStatusException.noSuchName) ; } } catch(IndexOutOfBoundsException e) { throw new SnmpStatusException(SnmpStatusException.noSuchName) ; } } /** * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpOid</CODE> to another OID. * @param source An OID representing an <CODE>SnmpOid</CODE> value. * @param dest Where source should be appended. */ public static void appendToOid(SnmpOid source, SnmpOid dest) { dest.append(source.getLength()) ; dest.append(source) ; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -