?? asnoctets.java
字號:
// NAME// $RCSfile: AsnOctets.java,v $// DESCRIPTION// [given below in javadoc format]// DELTA// $Revision: 3.39 $// CREATED// $Date: 2006/03/23 14:54:10 $// COPYRIGHT// Westhawk Ltd// TO DO///* * Copyright (C) 1995, 1996 by West Consulting BV * * Permission to use, copy, modify, and distribute this software * for any purpose and without fee is hereby granted, provided * that the above copyright notices appear in all copies and that * both the copyright notice and this permission notice appear in * supporting documentation. * This software is provided "as is" without express or implied * warranty. * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> * original version by hargrave@dellgate.us.dell.com (Jordan Hargrave) *//* * Copyright (C) 1996 - 2006 by Westhawk Ltd * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a> * * Permission to use, copy, modify, and distribute this software * for any purpose and without fee is hereby granted, provided * that the above copyright notices appear in all copies and that * both the copyright notice and this permission notice appear in * supporting documentation. * This software is provided "as is" without express or implied * warranty. * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> */package uk.co.westhawk.snmp.stack;import uk.co.westhawk.snmp.util.*;import java.io.*;import java.util.*;import java.net.InetAddress;import java.text.SimpleDateFormat;/** * 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. * <br/> * 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>. * * @see SnmpConstants#ASN_OCTET_STR * @see SnmpConstants#IPADDRESS * @see SnmpConstants#OPAQUE * * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> * @version $Revision: 3.39 $ $Date: 2006/03/23 14:54:10 $ */public class AsnOctets extends AsnObject{ private static final String version_id = "@(#)$Id: AsnOctets.java,v 3.39 2006/03/23 14:54:10 birgit Exp $ Copyright Westhawk Ltd"; /** * The hexadecimal prefix that is used when printing a hexadecimal * number in toString(). By default this is "0x". */ public static String HEX_PREFIX = "0x"; /** * The object that is used in toCalendar() to format the calendar * representation of the Octets according to the DateAndTime text * convension. * The pattern is "yyyy-M-d,HH:mm:ss.SS,z". * * @see #getCalendar() * @see #toCalendar() * @see java.text.SimpleDateFormat */ public static SimpleDateFormat CALFORMAT = new SimpleDateFormat("yyyy-M-d,HH:mm:ss.SS,z"); /** * The default AsnOctetsPrintableFace object. * * @see #setPrintable * @see DefaultAsnOctetsPrintable */ public static AsnOctetsPrintableFace printableObject = new DefaultAsnOctetsPrintable(); byte value[]; /** Cache the hash code for the OID */ private int hash = 0; /** * Constructor. The type of the AsnOctets defaults to ASN_OCTET_STR. * * @param s The byte array representing the AsnOctets * @see SnmpConstants#ASN_OCTET_STR */ public AsnOctets(byte s[]) throws IllegalArgumentException { this(s, ASN_OCTET_STR); } /** * Constructor to create a specific type of AsnOctets. * * @param s The byte array representing the AsnOctets * @param t The type of the AsnOctets * @see SnmpConstants#ASN_OCTET_STR * @see SnmpConstants#IPADDRESS * @see SnmpConstants#OPAQUE */ public AsnOctets(byte s[], byte t) throws IllegalArgumentException { value = s; type = t; if (value == null) { throw new IllegalArgumentException("Value is null"); } } /** * Constructor. The type of the AsnOctets defaults to ASN_OCTET_STR. * * @param s The character array representing the AsnOctets * @see SnmpConstants#ASN_OCTET_STR */ public AsnOctets(char s[]) { int idx; value = new byte[s.length]; type = ASN_OCTET_STR; for (idx=0; idx<s.length; idx++) { value[idx] = (byte)s[idx]; } } /** * Constructor. The type of the AsnOctets defaults to ASN_OCTET_STR. * * @param s The string representing the AsnOctets * @see SnmpConstants#ASN_OCTET_STR */ public AsnOctets(String s) { this(s.toCharArray()); } /** * Constructor to create an ASN IP Address. * If the address represents an IPv4 address, the asn type will be * set to IPADDRESS. If it represents an IPv6 address, the asn type * will be set to ASN_OCTET_STR. * * <br/> * 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>. * * @param iad The Inet Address * * @see #AsnOctets(Inet4Address, byte) */ public AsnOctets(java.net.InetAddress iad) throws IllegalArgumentException { this(iad.getAddress(), ASN_OCTET_STR); if (iad instanceof java.net.Inet4Address) { // IPv4 type = IPADDRESS; } else { // IPv6 is ASN_OCTET_STR, so do nothing } } /** * Constructor to create an ASN IPv4 Address. * If the address is an IPv4 address, it can either be represented * by IPADDRESS or as ASN_OCTET_STR. * * 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>. * * @param iad The IPv4 Inet Address * @param t The type of the AsnOctets * * @see #AsnOctets(InetAddress) * @see SnmpConstants#IPADDRESS * @see SnmpConstants#ASN_OCTET_STR * @since 4_14 */ public AsnOctets(java.net.Inet4Address iad, byte t) throws IllegalArgumentException { this(iad.getAddress(), t); } /** * Constructor for DateAndTime text convension. * See * <a href="http://www.ietf.org/rfc/rfc2579.txt">SNMPv2-TC</a> * * <pre> * field octets contents range * ----- ------ -------- ----- * 1 1-2 year* 0..65536 * 2 3 month 1..12 * 3 4 day 1..31 * 4 5 hour 0..23 * 5 6 minutes 0..59 * 6 7 seconds 0..60 * (use 60 for leap-second) * 7 8 deci-seconds 0..9 * * 8 9 direction from UTC '+' / '-' * 9 10 hours from UTC* 0..13 * 10 11 minutes from UTC 0..59 * * SYNTAX OCTET STRING (SIZE (8 | 11)) * </pre> * * @since 4_14 */ public AsnOctets(java.util.Calendar cal) { value = new byte[11]; type = ASN_OCTET_STR; int year = cal.get(Calendar.YEAR); // Calendar: 0=January int month = cal.get(Calendar.MONTH)+1; int day = cal.get(Calendar.DAY_OF_MONTH); int hour = cal.get(Calendar.HOUR_OF_DAY); int min = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int msec = cal.get(Calendar.MILLISECOND); int msecGMT = cal.get(Calendar.ZONE_OFFSET); // The value of year is in network-byte order // Is this correct? value[0] = (byte) ((year / 256) % 256); value[1] = (byte) (year % 256); value[2] = (byte) (month & 0xFF); value[3] = (byte) (day & 0xFF); value[4] = (byte) (hour & 0xFF); value[5] = (byte) (min & 0xFF); value[6] = (byte) (sec & 0xFF); value[7] = (byte) ((msec / 100) & 0xFF); char dir = '\0'; if (msecGMT < 0) { dir = '-'; msecGMT = msecGMT * -1; } else { dir = '+'; } value[8] = (byte) dir; if (msecGMT == 0) { value[9] = 0x00; value[10] = 0x00; } else { int minGMT = (int) (((double) msecGMT) / 1000.0 / 60.0); if (minGMT == 0) { value[9] = 0x00; value[10] = 0x00; } else { int hourGMT = (int) (minGMT / 60.0); minGMT = minGMT - (hourGMT * 60); value[9] = (byte) (hourGMT & 0xFF); value[10] = (byte) (minGMT & 0xFF); } } } /** * Constructor. * * @param in The input stream from which the value should be read * @param len The length of the AsnOctets */ public AsnOctets(InputStream in, int len) throws IOException { value = new byte[len]; if (len != 0) { if (len == in.read(value,0,len)) { String str = ""; //str = new String(value); } else { throw new IOException("AsnOctets(): Not enough data"); } } else { // if len is zero, the in.read will return -1 // a length of zero is a valid case. ; } } /** * 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 #toString() * @see #toHex() * @see #HEX_PREFIX */ public static void setHexPrefix(String newPrefix) { HEX_PREFIX = newPrefix; } /** * Sets the global AsnOctetsPrintableFace printableObject. This * object will be used in the toString() and the * toInternationalDisplayString() methods. * * @see #toString * @see #toInternationalDisplayString * @since 4_14 */ public static void setPrintable(AsnOctetsPrintableFace obj) { if (obj != null) { printableObject = obj; } } /** * Returns the String value. Calls toString(). * * @return The value of the AsnOctets * @see #toString() */ public String getValue() { return toString(); } /** * Returns the bytes. This returns a copy of the internal byte array. * * @return The bytes of the AsnOctets */ public byte[] getBytes() { int len = value.length; byte [] bytea = new byte[len]; System.arraycopy(value, 0, bytea, 0, len); return bytea; } /** * Returns the string representation of the AsnOctets. * <p> * The string will have one of the following formats: * <ul> * <li>if this class represents an IP Address (v4), it will call * toIpAddress()</li> * <li><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</li> * <li>a printable string, if this class seems printable</li> * </ul> * </p> * * <p> * 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. * </p> * * <br/> * 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>. * * @see #HEX_PREFIX * @see #setHexPrefix(String) * @see #toHex * @see #toIpAddress * @see AsnOctetsPrintableFace#isPrintable * @see AsnOctetsPrintableFace#toInternationalDisplayString * @return The string representation of the AsnOctets */ public String toString() { return toString(printableObject); } /** * As toString(), but this methods will use this specific, one-off * AsnOctetsPrintableFace object. * * @see #toString() * @since 4_14 */ public String toString(AsnOctetsPrintableFace face) { String str = ""; if (type == IPADDRESS) { // for IPv4 only str = toIpAddress(); } else if (type == OPAQUE) { str = HEX_PREFIX + toHex(); } else { boolean isPrintable = face.isPrintable(value); if (isPrintable) { str = face.toInternationalDisplayString(value); } else { str = HEX_PREFIX + toHex(); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -