⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asnoctets.java

📁 snmp zip 包开发snmp协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }        return str;    }    int size()     {         return value.length;     }    void write(OutputStream out, int pos) throws IOException     {        int idx;        // Output header        AsnBuildHeader(out, type, value.length);        if (debug > 10)        {            System.out.println("\tAsnOctets(): value = " + toString()                + ", pos = " + pos);        }                // Output data        for (idx=0; idx<value.length; idx++)         {            out.write(value[idx]);        }    }    /**     * Returns this Octet as an IP Address string. The format is     * aaa.bbb.ccc.ddd (IPv4) or a:b:c:d:e:f:g:h (IPv6).     *     * Note, the SNMP representation of IPv4 and IPv6 is different:     * <ul>     *    <li>IPv4: IPADDRESS (or ASN_OCTET_STR, see rfc 4001)</li>     *    <li>IPv6: ASN_OCTET_STR</li>     * </ul>     * See also      * <a href="http://www.ietf.org/rfc/rfc2465.txt">IPV6-TC</a>,     * <a href="http://www.ietf.org/rfc/rfc3416.txt">SNMPv2-PDU</a>,     * <a href="http://www.ietf.org/rfc/rfc4001.txt">INET-ADDRESS-MIB</a>.     *     * @return The IP Address representation.     * @see #toString     * @see #getIpAddress     * @see SnmpConstants#ASN_OCTET_STR     * @see SnmpConstants#IPADDRESS     */    /*     TODO: use Java's java.net.InetAddress, so it can be used for IPv4, and IPv6.     */    public String toIpAddress()    {        /* TODO: Does this work? Yes, but will not compile in jdk 1.2.X, and          * in order to load (a part of) the stack in Oracle, I need         * 1.2.X         */        /*        String str = "";        try        {            InetAddress iad = InetAddress.getByAddress(value);            str = iad.getHostAddress();        }        catch (java.net.UnknownHostException exc) { }        */        /*        */        StringBuffer sb = new StringBuffer(39);        int length;        long val;        length = value.length;        if (length > 0)        {            if (length > 4)            {                // IPv6                // Nicked this code from Inet6Address.numericToTextFormat                for (int i=0; i<length/2; i++)                 {                    sb.append(Integer.toHexString(((value[i<<1]<<8) & 0xff00)                                                 | (value[(i<<1)+1] & 0xff)));                    if (i < ((length/2)-1))                    {                        sb.append(":");                    }                }            }            else            {                // IPv4                for (int i=0; i<length-1; i++)                {                    val = getPositiveValue(i);                    sb.append(String.valueOf(val)).append(".");                }                val = getPositiveValue(length-1);                sb.append(String.valueOf(val));            }        }        return sb.toString();    }    /**     * Returns this Octet as an IP Address.      *     * Note, the SNMP representation of IPv4 and IPv6 is different:     * <ul>     *    <li>IPv4: IPADDRESS (or ASN_OCTET_STR, see rfc 4001)</li>     *    <li>IPv6: ASN_OCTET_STR</li>     * </ul>     * See also      * <a href="http://www.ietf.org/rfc/rfc2465.txt">IPV6-TC</a>,     * <a href="http://www.ietf.org/rfc/rfc3416.txt">SNMPv2-PDU</a>,     * <a href="http://www.ietf.org/rfc/rfc4001.txt">INET-ADDRESS-MIB</a>.     *     * @return The IP Address representation.     * @exception java.lang.RuntimeException Thrown when the Octets does     * not represent an InetAddress or when the method internally throws     * an java.net.UnknownHostException     *     * @see #toString     * @see #toIpAddress     * @see SnmpConstants#ASN_OCTET_STR     * @see SnmpConstants#IPADDRESS     * @since 4_14     */    public InetAddress getIpAddress()    throws java.lang.RuntimeException    {        InetAddress iad = null;        try        {            iad = InetAddress.getByAddress(value);        }        catch (java.net.UnknownHostException exc)         {             throw new java.lang.RuntimeException(exc);        }        return iad;    }    /**     * Returns the positive long for an octet. Only if type is IPADDRESS     * can the value be negative anyway.     */    private long getPositiveValue(int index)    {        long val = (long) value[index];        if (val <0)        {            val += 256;         }        return val;    }        /**     * Returns this Octet as an hexadecimal String, without any prefix.      *     * @return The hex representation.     * @see #toString     */    public String toHex()    {        int length;         StringBuffer buffer = new StringBuffer("");        length = value.length;        if (length > 0)        {            for (int i=0; i<length-1; i++)            {                buffer.append(SnmpUtilities.toHex(value[i])).append(":");            }            buffer.append(SnmpUtilities.toHex(value[length-1]));        }        return buffer.toString();    }    /**     * Returns this Octet as a display string (text convension). In contrast to the     * method toString(), this method does not try to guess whether or     * not this string is printable, it just converts it to a     * String, using "US-ASCII" character set.      *     * <p>     * DisplayString     * represents textual information taken from the NVT     * ASCII character set, as defined in pages 4, 10-11     * of <a href="http://www.ietf.org/rfc/rfc854.txt">RFC 854</a>.      * Any object defined using this syntax     * may not exceed 255 characters in length.     * Basicly it is US-ASCII with some changes.     * </p>     *     * @return The string representation.     * @see #toString     */    public String toDisplayString()    {        String str = "";        int length;         length = value.length;        if (length > 0)        {            try            {                str = new String(value, "US-ASCII");            }            catch (java.io.UnsupportedEncodingException exc)            {                str = new String(value);            }            str = str.trim();        }        return str;    }    /**     * Returns this Octet as an international display string (text     * convension).      * It calls AsnOctetsPrintableFace.toInternationalDisplayString().     *     * See     * <a href="http://www.ietf.org/rfc/rfc2790.txt">HOST-RESOURCES-MIB</a>     *      * @see AsnOctetsPrintableFace#toInternationalDisplayString     * @since 4_14     */    public String toInternationalDisplayString()    {        return toInternationalDisplayString(printableObject);    }    /**     * As toInternationalDisplayString(), but this methods will use this      * specific, one-off AsnOctetsPrintableFace object.     *      * @see #toInternationalDisplayString     * @since 4_14     */    public String toInternationalDisplayString(AsnOctetsPrintableFace face)    {        return face.toInternationalDisplayString(value);    }    /**     * Returns the String representation according to the DateAndTime     * convension.     * This string it returns is not exactly the same as the     * DISPLAY-HINT indicates.     * See      * <a href="http://www.ietf.org/rfc/rfc2579.txt">SNMPv2-TC</a>     *     * @since 4_14     * @exception java.lang.RuntimeException Thrown when the number of     * Octets does not represent the DateAndTime length.      * @see #CALFORMAT     */    public String toCalendar()    throws java.lang.RuntimeException    {        Calendar cal = this.getCalendar();        Date date = cal.getTime();        return CALFORMAT.format(date);    }    /**     * Returns the Octets as Calendar according to the DateAndTime text     * convension. You can only call this method if     * the syntax of this Octet is the DateAndTime text convension.     *     * @exception java.lang.RuntimeException Thrown when the number of     * Octets does not represent the DateAndTime length.      *     * @since 4_14     * @see #AsnOctets(java.util.Calendar)     */    public java.util.Calendar getCalendar()    throws java.lang.RuntimeException    {        Calendar cal = Calendar.getInstance();        if (value.length == 8 || value.length == 11)        {            int year = (int) ((getPositiveValue(0) * 256) + getPositiveValue(1));            // Calendar: 0=January            int month = value[2]-1;             int day = value[3];            int hour = value[4];            int min = value[5];            int sec = value[6];            int msec = value[7] * 100;            cal.set(year, month, day, hour, min, sec);             cal.set(Calendar.MILLISECOND, msec);            if (value.length == 11)            {                char dir = (char) value[8];                int hourUTC = value[9];                int minUTC = value[10];                int secUTC = (hourUTC * 60) * 60;                int msecGMT = secUTC * 1000;                if (dir == '-')                {                    msecGMT = msecGMT * -1;                }                cal.set(Calendar.ZONE_OFFSET, msecGMT);            }        }        else        {            throw new java.lang.RuntimeException("AsnOctets is not DateAndTime");        }        return cal;    }/** * Converts this Octet to its corresponding sub-identifiers. * Each octet will be encoded in a separate sub-identifier, by * converting the octet into a positive long. *  * <p> * Use this method when building an OID when this Octet specifies a * conceptual row. For example ipNetToMediaEntry, see  * <a href="http://www.ietf.org/rfc/rfc2011.txt">IP-MIB</a> * or SnmpCommunityEntry, see * <a href="http://www.ietf.org/rfc/rfc3584.txt">SNMP-COMMUNITY-MIB</a> * </p> * * <p> * The variable <code>length_implied</code> indicates that this MIB variable  * is preceded by the IMPLIED keyword: * </p> * <ul> * <li> * The IMPLIED keyword can only be present for an Octet having  * a variable-length syntax (e.g., variable-length strings or object  * identifier-valued objects).  * </li> * <li> * The IMPLIED keyword can only be associated with the last * object in the INDEX clause.   * </li> * <li> * The IMPLIED keyword may not be used on a variable-length * string Octet if that string might have a value of zero-length.  * </li> * </ul> * * <p> * If the length is implied, no extra sub-identifier will be created to * indicate its length. <br/> * If the length is not implied, the first * sub-identifier will be the length of the Octet. * </p> * * <p> * If this Octet is of type IPADDRESS, length_implied should be false. * </p> * * <p> * The mapping of the INDEX clause is * explained in <a href="http://www.ietf.org/rfc/rfc2578.txt">SNMPv2-SMI</a>, * section 7.7. * </p> * * @param length_implied Indicates if the length of this octet is * implied.  * * @see AsnObjectId#add(long[]) */public long [] toSubOid(boolean length_implied){    long sub_oid[];    int index = 0;    int length = value.length;    if (length_implied)    {        sub_oid = new long[length];    }    else    {        sub_oid = new long[length+1];        sub_oid[0] = length;        index++;    }    for (int i=0; i<length; i++)    {        sub_oid[index] = getPositiveValue(i);        index++;    }    return sub_oid;}/** * Compares this Octet to the specified object. * The result is <code>true</code> if and only if the argument is not * <code>null</code> and is an <code>AsnOctets</code> object that represents * the same sequence of octets as this Octet. * * @param anObject the object to compare this <code>AsnOctets</code>  *                 against. * @return <code>true</code> if the <code>AsnOctets </code>are equal; *         <code>false</code> otherwise. */public boolean equals(Object anObject) {    if (this == anObject)     {        return true;    }    if (anObject instanceof AsnOctets)     {        AsnOctets anotherOctet = (AsnOctets)anObject;        int n = value.length;        if (n == anotherOctet.value.length)         {            byte v1[] = value;            byte v2[] = anotherOctet.value;            int i = 0;            int j = 0;            while (n-- != 0)             {                if (v1[i++] != v2[j++])                {                    return false;                }            }            return true;        }    }    return false;}/** * Returns a hash code for this Octet. The hash code for a * <code>AsnOctets</code> object is computed as * <blockquote><pre> * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre></blockquote> * using <code>int</code> arithmetic, where <code>s[i]</code> is the * <i>i</i>th character of the Octet, <code>n</code> is the length of * the Octet, and <code>^</code> indicates exponentiation. * (The hash value of the empty Octet is zero.) * * @return  a hash code value for this Octet. */public int hashCode() {    int h = hash;    if (h == 0)     {        int off = 0;        byte val[] = value;        int len = value.length;        for (int i=0; i<len; i++)         {            h = 31*h + val[off++];        }        hash = h;    }    return h;}}

⌨️ 快捷键说明

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