Class AsnOctets

java.lang.Object
uk.co.westhawk.snmp.stack.AsnObject
uk.co.westhawk.snmp.stack.AsnOctets
All Implemented Interfaces:
SnmpConstants

public class AsnOctets extends AsnObject
This class represents the ASN.1 Octet class. It can be used for Octets, Ip Addresses and Opaque primitive types. The Octets type (ASN_OCTET_STR) is used for some text convensions. This class supports the DateAndTime, DisplayString and InternationalDisplayString and Ipv6Address text convensions.
Note, the SNMP representation of IPv4 and IPv6 is different:
  • IPv4: IPADDRESS (or ASN_OCTET_STR, see rfc 4001)
  • IPv6: ASN_OCTET_STR
See also IPV6-TC, SNMPv2-PDU, INET-ADDRESS-MIB.
Version:
$Revision: 3.39 $ $Date: 2006/03/23 14:54:10 $
Author:
Tim Panton
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • setHexPrefix

      public static void setHexPrefix(String newPrefix)
      Sets the global hexadecimal prefix. This prefix will be used in toString() when it prints out a hexadecimal number. It is not used in toHex(). The default is "0x".
      See Also:
    • setPrintable

      public static void setPrintable(AsnOctetsPrintableFace obj)
      Sets the global AsnOctetsPrintableFace printableObject. This object will be used in the toString() and the toInternationalDisplayString() methods.
      Since:
      4_14
      See Also:
    • getValue

      public String getValue()
      Returns the String value. Calls toString().
      Returns:
      The value of the AsnOctets
      See Also:
    • getBytes

      public byte[] getBytes()
      Returns the bytes. This returns a copy of the internal byte array.
      Returns:
      The bytes of the AsnOctets
    • toString

      public String toString()
      Returns the string representation of the AsnOctets.

      The string will have one of the following formats:

      • if this class represents an IP Address (v4), it will call toIpAddress()
      • <prefix>aa[:bb]*, if this class represents a non-printable string or has type OPAQUE. The output will be in hexadecimal numbers (see toHex()). It will be prefixed according to the hex. prefix value
      • a printable string, if this class seems printable

      When the type is ASN_OCTET_STR, this method uses the AsnOctetsPrintableFace.isPrintable() to determine whether or not the string is printable. If it is printable, it will use AsnOctetsPrintableFace.toInternationalDisplayString() to transform the Octets to a String.


      Note, the SNMP representation of IPv4 and IPv6 is different:
      • IPv4: IPADDRESS (or ASN_OCTET_STR, see rfc 4001)
      • IPv6: ASN_OCTET_STR
      See also IPV6-TC, SNMPv2-PDU, INET-ADDRESS-MIB.
      Specified by:
      toString in class AsnObject
      Returns:
      The string representation of the AsnOctets
      See Also:
    • toString

      public String toString(AsnOctetsPrintableFace face)
      As toString(), but this methods will use this specific, one-off AsnOctetsPrintableFace object.
      Since:
      4_14
      See Also:
    • toIpAddress

      public String toIpAddress()
      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:
      • IPv4: IPADDRESS (or ASN_OCTET_STR, see rfc 4001)
      • IPv6: ASN_OCTET_STR
      See also IPV6-TC, SNMPv2-PDU, INET-ADDRESS-MIB.
      Returns:
      The IP Address representation.
      See Also:
    • getIpAddress

      public InetAddress getIpAddress() throws RuntimeException
      Returns this Octet as an IP Address. Note, the SNMP representation of IPv4 and IPv6 is different:
      • IPv4: IPADDRESS (or ASN_OCTET_STR, see rfc 4001)
      • IPv6: ASN_OCTET_STR
      See also IPV6-TC, SNMPv2-PDU, INET-ADDRESS-MIB.
      Returns:
      The IP Address representation.
      Throws:
      RuntimeException - Thrown when the Octets does not represent an InetAddress or when the method internally throws an java.net.UnknownHostException
      Since:
      4_14
      See Also:
    • toHex

      public String toHex()
      Returns this Octet as an hexadecimal String, without any prefix.
      Returns:
      The hex representation.
      See Also:
    • toDisplayString

      public String toDisplayString()
      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.

      DisplayString represents textual information taken from the NVT ASCII character set, as defined in pages 4, 10-11 of RFC 854. Any object defined using this syntax may not exceed 255 characters in length. Basicly it is US-ASCII with some changes.

      Returns:
      The string representation.
      See Also:
    • toInternationalDisplayString

      public String toInternationalDisplayString()
      Returns this Octet as an international display string (text convension). It calls AsnOctetsPrintableFace.toInternationalDisplayString(). See HOST-RESOURCES-MIB
      Since:
      4_14
      See Also:
    • toInternationalDisplayString

      public String toInternationalDisplayString(AsnOctetsPrintableFace face)
      As toInternationalDisplayString(), but this methods will use this specific, one-off AsnOctetsPrintableFace object.
      Since:
      4_14
      See Also:
    • toCalendar

      public String toCalendar() throws RuntimeException
      Returns the String representation according to the DateAndTime convension. This string it returns is not exactly the same as the DISPLAY-HINT indicates. See SNMPv2-TC
      Throws:
      RuntimeException - Thrown when the number of Octets does not represent the DateAndTime length.
      Since:
      4_14
      See Also:
    • getCalendar

      public Calendar getCalendar() throws RuntimeException
      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.
      Throws:
      RuntimeException - Thrown when the number of Octets does not represent the DateAndTime length.
      Since:
      4_14
      See Also:
    • toSubOid

      public long[] toSubOid(boolean length_implied)
      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.

      Use this method when building an OID when this Octet specifies a conceptual row. For example ipNetToMediaEntry, see IP-MIB or SnmpCommunityEntry, see SNMP-COMMUNITY-MIB

      The variable length_implied indicates that this MIB variable is preceded by the IMPLIED keyword:

      • 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).
      • The IMPLIED keyword can only be associated with the last object in the INDEX clause.
      • The IMPLIED keyword may not be used on a variable-length string Octet if that string might have a value of zero-length.

      If the length is implied, no extra sub-identifier will be created to indicate its length.
      If the length is not implied, the first sub-identifier will be the length of the Octet.

      If this Octet is of type IPADDRESS, length_implied should be false.

      The mapping of the INDEX clause is explained in SNMPv2-SMI, section 7.7.

      Parameters:
      length_implied - Indicates if the length of this octet is implied.
      See Also:
    • equals

      public boolean equals(Object anObject)
      Compares this Octet to the specified object. The result is true if and only if the argument is not null and is an AsnOctets object that represents the same sequence of octets as this Octet.
      Overrides:
      equals in class Object
      Parameters:
      anObject - the object to compare this AsnOctets against.
      Returns:
      true if the AsnOctets are equal; false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code for this Octet. The hash code for a AsnOctets object is computed as
       s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
       
      using int arithmetic, where s[i] is the ith character of the Octet, n is the length of the Octet, and ^ indicates exponentiation. (The hash value of the empty Octet is zero.)
      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this Octet.