Showing the changes to java/pkg/com/sun/jmx/snmp/SnmpString.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,278 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | |||
| 27 | package com.sun.jmx.snmp; | ||
| 28 | |||
| 29 | import java.net.InetAddress; | ||
| 30 | import java.net.UnknownHostException; | ||
| 31 | |||
| 32 | /** | ||
| 33 | * Represents an SNMP string. | ||
| 34 | * | ||
| 35 | * <p><b>This API is a Sun Microsystems internal API and is subject | ||
| 36 | * to change without notice.</b></p> | ||
| 37 | */ | ||
| 38 | |||
| 39 | public class SnmpString extends SnmpValue { | ||
| 40 | private static final long serialVersionUID = -7011986973225194188L; | ||
| 41 | |||
| 42 | // CONSTRUCTORS | ||
| 43 | //------------- | ||
| 44 | /** | ||
| 45 | * Constructs a new empty <CODE>SnmpString</CODE>. | ||
| 46 | */ | ||
| 47 | public SnmpString() { | ||
| 48 | value = new byte[0] ; | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Constructs a new <CODE>SnmpString</CODE> from the specified bytes array. | ||
| 53 | * @param v The bytes composing the string value. | ||
| 54 | */ | ||
| 55 | public SnmpString(byte[] v) { | ||
| 56 | value = v.clone() ; | ||
| 57 | } | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>Bytes</CODE> array. | ||
| 61 | * @param v The <CODE>Bytes</CODE> composing the string value. | ||
| 62 | */ | ||
| 63 | public SnmpString(Byte[] v) { | ||
| 64 | value = new byte[v.length] ; | ||
| 65 | for (int i = 0 ; i < v.length ; i++) { | ||
| 66 | value[i] = v[i].byteValue() ; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | /** | ||
| 71 | * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>String</CODE> value. | ||
| 72 | * @param v The initialization value. | ||
| 73 | */ | ||
| 74 | public SnmpString(String v) { | ||
| 75 | value = v.getBytes() ; | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE> InetAddress </Code>. | ||
| 80 | * @param address The <CODE>InetAddress </CODE>. | ||
| 81 | * | ||
| 82 | * @since 1.5 | ||
| 83 | */ | ||
| 84 | public SnmpString(InetAddress address) { | ||
| 85 | value = address.getAddress(); | ||
| 86 | } | ||
| 87 | |||
| 88 | // PUBLIC METHODS | ||
| 89 | //--------------- | ||
| 90 | |||
| 91 | /** | ||
| 92 | * Converts the string value to its <CODE> InetAddress </CODE> form. | ||
| 93 | * @return an {@link InetAddress} defined by the string value. | ||
| 94 | * @exception UnknownHostException If string value is not a legal address format. | ||
| 95 | * | ||
| 96 | * @since 1.5 | ||
| 97 | */ | ||
| 98 | public InetAddress inetAddressValue() throws UnknownHostException { | ||
| 99 | return InetAddress.getByAddress(value); | ||
| 100 | } | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Converts the specified binary string into a character string. | ||
| 104 | * @param bin The binary string value to convert. | ||
| 105 | * @return The character string representation. | ||
| 106 | */ | ||
| 107 | public static String BinToChar(String bin) { | ||
| 108 | char value[] = new char[bin.length()/8]; | ||
| 109 | int binLength = value.length; | ||
| 110 | for (int i = 0; i < binLength; i++) | ||
| 111 | value[i] = (char)Integer.parseInt(bin.substring(8*i, 8*i+8), 2); | ||
| 112 | return new String(value); | ||
| 113 | } | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Converts the specified hexadecimal string into a character string. | ||
| 117 | * @param hex The hexadecimal string value to convert. | ||
| 118 | * @return The character string representation. | ||
| 119 | */ | ||
| 120 | public static String HexToChar(String hex) { | ||
| 121 | char value[] = new char[hex.length()/2]; | ||
| 122 | int hexLength = value.length; | ||
| 123 | for (int i = 0; i < hexLength; i++) | ||
| 124 | value[i] = (char)Integer.parseInt(hex.substring(2*i, 2*i+2), 16); | ||
| 125 | return new String(value); | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Returns the bytes array of this <CODE>SnmpString</CODE>. | ||
| 130 | * @return The value. | ||
| 131 | */ | ||
| 132 | public byte[] byteValue() { | ||
| 133 | return value ; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * Converts the string value to its array of <CODE>Bytes</CODE> form. | ||
| 138 | * @return The array of <CODE>Bytes</CODE> representation of the value. | ||
| 139 | */ | ||
| 140 | public Byte[] toByte() { | ||
| 141 | Byte[] result = new Byte[value.length] ; | ||
| 142 | for (int i = 0 ; i < value.length ; i++) { | ||
| 143 | result[i] = new Byte(value[i]) ; | ||
| 144 | } | ||
| 145 | return result ; | ||
| 146 | } | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Converts the string value to its <CODE>String</CODE> form. | ||
| 150 | * @return The <CODE>String</CODE> representation of the value. | ||
| 151 | */ | ||
| 152 | public String toString() { | ||
| 153 | return new String(value) ; | ||
| 154 | } | ||
| 155 | |||
| 156 | /** | ||
| 157 | * Converts the string value to its <CODE>SnmpOid</CODE> form. | ||
| 158 | * @return The OID representation of the value. | ||
| 159 | */ | ||
| 160 | public SnmpOid toOid() { | ||
| 161 | long[] ids = new long[value.length] ; | ||
| 162 | for (int i = 0 ; i < value.length ; i++) { | ||
| 163 | ids[i] = (long)(value[i] & 0xFF) ; | ||
| 164 | } | ||
| 165 | return new SnmpOid(ids) ; | ||
| 166 | } | ||
| 167 | |||
| 168 | /** | ||
| 169 | * Extracts the string from an index OID and returns its | ||
| 170 | * value converted as an <CODE>SnmpOid</CODE>. | ||
| 171 | * @param index The index array. | ||
| 172 | * @param start The position in the index array. | ||
| 173 | * @return The OID representing the string value. | ||
| 174 | * @exception SnmpStatusException There is no string value | ||
| 175 | * available at the start position. | ||
| 176 | */ | ||
| 177 | public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { | ||
| 178 | try { | ||
| 179 | if (index[start] > Integer.MAX_VALUE) { | ||
| 180 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; | ||
| 181 | } | ||
| 182 | int strLen = (int)index[start++] ; | ||
| 183 | long[] ids = new long[strLen] ; | ||
| 184 | for (int i = 0 ; i < strLen ; i++) { | ||
| 185 | ids[i] = index[start + i] ; | ||
| 186 | } | ||
| 187 | return new SnmpOid(ids) ; | ||
| 188 | } | ||
| 189 | catch(IndexOutOfBoundsException e) { | ||
| 190 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | /** | ||
| 195 | * Scans an index OID, skips the string value and returns the position | ||
| 196 | * of the next value. | ||
| 197 | * @param index The index array. | ||
| 198 | * @param start The position in the index array. | ||
| 199 | * @return The position of the next value. | ||
| 200 | * @exception SnmpStatusException There is no string value | ||
| 201 | * available at the start position. | ||
| 202 | */ | ||
| 203 | public static int nextOid(long[] index, int start) throws SnmpStatusException { | ||
| 204 | try { | ||
| 205 | if (index[start] > Integer.MAX_VALUE) { | ||
| 206 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; | ||
| 207 | } | ||
| 208 | int strLen = (int)index[start++] ; | ||
| 209 | start += strLen ; | ||
| 210 | if (start <= index.length) { | ||
| 211 | return start ; | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | catch(IndexOutOfBoundsException e) { | ||
| 218 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | /** | ||
| 223 | * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpString</CODE> to another OID. | ||
| 224 | * @param source An OID representing an <CODE>SnmpString</CODE> value. | ||
| 225 | * @param dest Where source should be appended. | ||
| 226 | */ | ||
| 227 | public static void appendToOid(SnmpOid source, SnmpOid dest) { | ||
| 228 | dest.append(source.getLength()) ; | ||
| 229 | dest.append(source) ; | ||
| 230 | } | ||
| 231 | |||
| 232 | /** | ||
| 233 | * Performs a clone action. This provides a workaround for the | ||
| 234 | * <CODE>SnmpValue</CODE> interface. | ||
| 235 | * @return The SnmpValue clone. | ||
| 236 | */ | ||
| 237 | final synchronized public SnmpValue duplicate() { | ||
| 238 | return (SnmpValue) clone() ; | ||
| 239 | } | ||
| 240 | |||
| 241 | /** | ||
| 242 | * Clones the <CODE>SnmpString</CODE> object, making a copy of its data. | ||
| 243 | * @return The object clone. | ||
| 244 | */ | ||
| 245 | synchronized public Object clone() { | ||
| 246 | SnmpString newclone = null ; | ||
| 247 | |||
| 248 | try { | ||
| 249 | newclone = (SnmpString) super.clone() ; | ||
| 250 | newclone.value = new byte[value.length] ; | ||
| 251 | System.arraycopy(value, 0, newclone.value, 0, value.length) ; | ||
| 252 | } catch (CloneNotSupportedException e) { | ||
| 253 | throw new InternalError() ; // vm bug. | ||
| 254 | } | ||
| 255 | return newclone ; | ||
| 256 | } | ||
| 257 | |||
| 258 | /** | ||
| 259 | * Returns a textual description of the type object. | ||
| 260 | * @return ASN.1 textual description. | ||
| 261 | */ | ||
| 262 | public String getTypeName() { | ||
| 263 | return name ; | ||
| 264 | } | ||
| 265 | |||
| 266 | // VARIABLES | ||
| 267 | //---------- | ||
| 268 | /** | ||
| 269 | * Name of the type. | ||
| 270 | */ | ||
| 271 | final static String name = "String" ; | ||
| 272 | |||
| 273 | /** | ||
| 274 | * This is the bytes array of the string value. | ||
| 275 | * @serial | ||
| 276 | */ | ||
| 277 | protected byte[] value = null ; | ||
| 278 | } | ||