1 11 12 13 package com.sun.jmx.snmp; 14 15 import java.net.InetAddress ; 16 import java.net.UnknownHostException ; 17 18 26 27 public class SnmpString extends SnmpValue { 28 29 34 public SnmpString() { 35 value = new byte[0] ; 36 } 37 38 42 public SnmpString(byte[] v) { 43 value = (byte[])v.clone() ; 44 } 45 46 50 public SnmpString(Byte [] v) { 51 value = new byte[v.length] ; 52 for (int i = 0 ; i < v.length ; i++) { 53 value[i] = v[i].byteValue() ; 54 } 55 } 56 57 61 public SnmpString(String v) { 62 value = v.getBytes() ; 63 } 64 65 71 public SnmpString(InetAddress address) { 72 value = address.getAddress(); 73 } 74 75 78 85 public InetAddress inetAddressValue() throws UnknownHostException { 86 return InetAddress.getByAddress(value); 87 } 88 89 94 public static String BinToChar(String bin) { 95 char value[] = new char[bin.length()/8]; 96 int binLength = value.length; 97 for (int i = 0; i < binLength; i++) 98 value[i] = (char)Integer.parseInt(bin.substring(8*i, 8*i+8), 2); 99 return new String (value); 100 } 101 102 107 public static String HexToChar(String hex) { 108 char value[] = new char[hex.length()/2]; 109 int hexLength = value.length; 110 for (int i = 0; i < hexLength; i++) 111 value[i] = (char)Integer.parseInt(hex.substring(2*i, 2*i+2), 16); 112 return new String (value); 113 } 114 115 119 public byte[] byteValue() { 120 return value ; 121 } 122 123 127 public Byte [] toByte() { 128 Byte [] result = new Byte [value.length] ; 129 for (int i = 0 ; i < value.length ; i++) { 130 result[i] = new Byte (value[i]) ; 131 } 132 return result ; 133 } 134 135 139 public String toString() { 140 return new String (value) ; 141 } 142 143 147 public SnmpOid toOid() { 148 long[] ids = new long[value.length] ; 149 for (int i = 0 ; i < value.length ; i++) { 150 ids[i] = (long)(value[i] & 0xFF) ; 151 } 152 return new SnmpOid(ids) ; 153 } 154 155 164 public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { 165 try { 166 if (index[start] > Integer.MAX_VALUE) { 167 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 168 } 169 int strLen = (int)index[start++] ; 170 long[] ids = new long[strLen] ; 171 for (int i = 0 ; i < strLen ; i++) { 172 ids[i] = index[start + i] ; 173 } 174 return new SnmpOid(ids) ; 175 } 176 catch(IndexOutOfBoundsException e) { 177 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 178 } 179 } 180 181 190 public static int nextOid(long[] index, int start) throws SnmpStatusException { 191 try { 192 if (index[start] > Integer.MAX_VALUE) { 193 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 194 } 195 int strLen = (int)index[start++] ; 196 start += strLen ; 197 if (start <= index.length) { 198 return start ; 199 } 200 else { 201 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 202 } 203 } 204 catch(IndexOutOfBoundsException e) { 205 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 206 } 207 } 208 209 214 public static void appendToOid(SnmpOid source, SnmpOid dest) { 215 dest.append(source.getLength()) ; 216 dest.append(source) ; 217 } 218 219 224 final synchronized public SnmpValue duplicate() { 225 return (SnmpValue) clone() ; 226 } 227 228 232 synchronized public Object clone() { 233 SnmpString newclone = null ; 234 235 try { 236 newclone = (SnmpString) super.clone() ; 237 newclone.value = new byte[value.length] ; 238 System.arraycopy(value, 0, newclone.value, 0, value.length) ; 239 } catch (CloneNotSupportedException e) { 240 throw new InternalError () ; } 242 return newclone ; 243 } 244 245 249 public String getTypeName() { 250 return name ; 251 } 252 253 258 final static String name = "String" ; 259 260 264 protected byte[] value = null ; 265 } 266 | Popular Tags |