Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpString.java
38924 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package com.sun.jmx.snmp;2728import java.net.InetAddress;29import java.net.UnknownHostException;3031/**32* Represents an SNMP string.33*34* <p><b>This API is a Sun Microsystems internal API and is subject35* to change without notice.</b></p>36*/3738public class SnmpString extends SnmpValue {39private static final long serialVersionUID = -7011986973225194188L;4041// CONSTRUCTORS42//-------------43/**44* Constructs a new empty <CODE>SnmpString</CODE>.45*/46public SnmpString() {47value = new byte[0] ;48}4950/**51* Constructs a new <CODE>SnmpString</CODE> from the specified bytes array.52* @param v The bytes composing the string value.53*/54public SnmpString(byte[] v) {55value = v.clone() ;56}5758/**59* Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>Bytes</CODE> array.60* @param v The <CODE>Bytes</CODE> composing the string value.61*/62public SnmpString(Byte[] v) {63value = new byte[v.length] ;64for (int i = 0 ; i < v.length ; i++) {65value[i] = v[i].byteValue() ;66}67}6869/**70* Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>String</CODE> value.71* @param v The initialization value.72*/73public SnmpString(String v) {74value = v.getBytes() ;75}7677/**78* Constructs a new <CODE>SnmpString</CODE> from the specified <CODE> InetAddress </Code>.79* @param address The <CODE>InetAddress </CODE>.80*81* @since 1.582*/83public SnmpString(InetAddress address) {84value = address.getAddress();85}8687// PUBLIC METHODS88//---------------8990/**91* Converts the string value to its <CODE> InetAddress </CODE> form.92* @return an {@link InetAddress} defined by the string value.93* @exception UnknownHostException If string value is not a legal address format.94*95* @since 1.596*/97public InetAddress inetAddressValue() throws UnknownHostException {98return InetAddress.getByAddress(value);99}100101/**102* Converts the specified binary string into a character string.103* @param bin The binary string value to convert.104* @return The character string representation.105*/106public static String BinToChar(String bin) {107char value[] = new char[bin.length()/8];108int binLength = value.length;109for (int i = 0; i < binLength; i++)110value[i] = (char)Integer.parseInt(bin.substring(8*i, 8*i+8), 2);111return new String(value);112}113114/**115* Converts the specified hexadecimal string into a character string.116* @param hex The hexadecimal string value to convert.117* @return The character string representation.118*/119public static String HexToChar(String hex) {120char value[] = new char[hex.length()/2];121int hexLength = value.length;122for (int i = 0; i < hexLength; i++)123value[i] = (char)Integer.parseInt(hex.substring(2*i, 2*i+2), 16);124return new String(value);125}126127/**128* Returns the bytes array of this <CODE>SnmpString</CODE>.129* @return The value.130*/131public byte[] byteValue() {132return value.clone() ;133}134135/**136* Converts the string value to its array of <CODE>Bytes</CODE> form.137* @return The array of <CODE>Bytes</CODE> representation of the value.138*/139public Byte[] toByte() {140Byte[] result = new Byte[value.length] ;141for (int i = 0 ; i < value.length ; i++) {142result[i] = new Byte(value[i]) ;143}144return result ;145}146147/**148* Converts the string value to its <CODE>String</CODE> form.149* @return The <CODE>String</CODE> representation of the value.150*/151public String toString() {152return new String(value) ;153}154155/**156* Converts the string value to its <CODE>SnmpOid</CODE> form.157* @return The OID representation of the value.158*/159public SnmpOid toOid() {160long[] ids = new long[value.length] ;161for (int i = 0 ; i < value.length ; i++) {162ids[i] = (long)(value[i] & 0xFF) ;163}164return new SnmpOid(ids) ;165}166167/**168* Extracts the string from an index OID and returns its169* value converted as an <CODE>SnmpOid</CODE>.170* @param index The index array.171* @param start The position in the index array.172* @return The OID representing the string value.173* @exception SnmpStatusException There is no string value174* available at the start position.175*/176public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {177try {178if (index[start] > Integer.MAX_VALUE) {179throw new SnmpStatusException(SnmpStatusException.noSuchName) ;180}181int strLen = (int)index[start++] ;182long[] ids = new long[strLen] ;183for (int i = 0 ; i < strLen ; i++) {184ids[i] = index[start + i] ;185}186return new SnmpOid(ids) ;187}188catch(IndexOutOfBoundsException e) {189throw new SnmpStatusException(SnmpStatusException.noSuchName) ;190}191}192193/**194* Scans an index OID, skips the string value and returns the position195* of the next value.196* @param index The index array.197* @param start The position in the index array.198* @return The position of the next value.199* @exception SnmpStatusException There is no string value200* available at the start position.201*/202public static int nextOid(long[] index, int start) throws SnmpStatusException {203try {204if (index[start] > Integer.MAX_VALUE) {205throw new SnmpStatusException(SnmpStatusException.noSuchName) ;206}207int strLen = (int)index[start++] ;208start += strLen ;209if (start <= index.length) {210return start ;211}212else {213throw new SnmpStatusException(SnmpStatusException.noSuchName) ;214}215}216catch(IndexOutOfBoundsException e) {217throw new SnmpStatusException(SnmpStatusException.noSuchName) ;218}219}220221/**222* Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpString</CODE> to another OID.223* @param source An OID representing an <CODE>SnmpString</CODE> value.224* @param dest Where source should be appended.225*/226public static void appendToOid(SnmpOid source, SnmpOid dest) {227dest.append(source.getLength()) ;228dest.append(source) ;229}230231/**232* Performs a clone action. This provides a workaround for the233* <CODE>SnmpValue</CODE> interface.234* @return The SnmpValue clone.235*/236final synchronized public SnmpValue duplicate() {237return (SnmpValue) clone() ;238}239240/**241* Clones the <CODE>SnmpString</CODE> object, making a copy of its data.242* @return The object clone.243*/244synchronized public Object clone() {245SnmpString newclone = null ;246247try {248newclone = (SnmpString) super.clone() ;249newclone.value = new byte[value.length] ;250System.arraycopy(value, 0, newclone.value, 0, value.length) ;251} catch (CloneNotSupportedException e) {252throw new InternalError(e) ; // vm bug.253}254return newclone ;255}256257/**258* Returns a textual description of the type object.259* @return ASN.1 textual description.260*/261public String getTypeName() {262return name ;263}264265// VARIABLES266//----------267/**268* Name of the type.269*/270final static String name = "String" ;271272/**273* This is the bytes array of the string value.274* @serial275*/276protected byte[] value = null ;277}278279280