1. /*
  2. * @(#)file SnmpString.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.15
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp;
  12. import java.net.InetAddress;
  13. import java.net.UnknownHostException;
  14. /**
  15. * Represents an SNMP string.
  16. *
  17. * <p><b>This API is a Sun Microsystems internal API and is subject
  18. * to change without notice.</b></p>
  19. * @version 4.15 12/19/03
  20. * @author Sun Microsystems, Inc
  21. */
  22. public class SnmpString extends SnmpValue {
  23. // CONSTRUCTORS
  24. //-------------
  25. /**
  26. * Constructs a new empty <CODE>SnmpString</CODE>.
  27. */
  28. public SnmpString() {
  29. value = new byte[0] ;
  30. }
  31. /**
  32. * Constructs a new <CODE>SnmpString</CODE> from the specified bytes array.
  33. * @param v The bytes composing the string value.
  34. */
  35. public SnmpString(byte[] v) {
  36. value = (byte[])v.clone() ;
  37. }
  38. /**
  39. * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>Bytes</CODE> array.
  40. * @param v The <CODE>Bytes</CODE> composing the string value.
  41. */
  42. public SnmpString(Byte[] v) {
  43. value = new byte[v.length] ;
  44. for (int i = 0 ; i < v.length ; i++) {
  45. value[i] = v[i].byteValue() ;
  46. }
  47. }
  48. /**
  49. * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>String</CODE> value.
  50. * @param v The initialization value.
  51. */
  52. public SnmpString(String v) {
  53. value = v.getBytes() ;
  54. }
  55. /**
  56. * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE> InetAddress </Code>.
  57. * @param address The <CODE>InetAddress </CODE>.
  58. *
  59. * @since 1.5
  60. */
  61. public SnmpString(InetAddress address) {
  62. value = address.getAddress();
  63. }
  64. // PUBLIC METHODS
  65. //---------------
  66. /**
  67. * Converts the string value to its <CODE> InetAddress </CODE> form.
  68. * @return an {@link InetAddress} defined by the string value.
  69. * @exception UnknownHostException If string value is not a legal address format.
  70. *
  71. * @since 1.5
  72. */
  73. public InetAddress inetAddressValue() throws UnknownHostException {
  74. return InetAddress.getByAddress(value);
  75. }
  76. /**
  77. * Converts the specified binary string into a character string.
  78. * @param bin The binary string value to convert.
  79. * @return The character string representation.
  80. */
  81. public static String BinToChar(String bin) {
  82. char value[] = new char[bin.length()/8];
  83. int binLength = value.length;
  84. for (int i = 0; i < binLength; i++)
  85. value[i] = (char)Integer.parseInt(bin.substring(8*i, 8*i+8), 2);
  86. return new String(value);
  87. }
  88. /**
  89. * Converts the specified hexadecimal string into a character string.
  90. * @param hex The hexadecimal string value to convert.
  91. * @return The character string representation.
  92. */
  93. public static String HexToChar(String hex) {
  94. char value[] = new char[hex.length()/2];
  95. int hexLength = value.length;
  96. for (int i = 0; i < hexLength; i++)
  97. value[i] = (char)Integer.parseInt(hex.substring(2*i, 2*i+2), 16);
  98. return new String(value);
  99. }
  100. /**
  101. * Returns the bytes array of this <CODE>SnmpString</CODE>.
  102. * @return The value.
  103. */
  104. public byte[] byteValue() {
  105. return value ;
  106. }
  107. /**
  108. * Converts the string value to its array of <CODE>Bytes</CODE> form.
  109. * @return The array of <CODE>Bytes</CODE> representation of the value.
  110. */
  111. public Byte[] toByte() {
  112. Byte[] result = new Byte[value.length] ;
  113. for (int i = 0 ; i < value.length ; i++) {
  114. result[i] = new Byte(value[i]) ;
  115. }
  116. return result ;
  117. }
  118. /**
  119. * Converts the string value to its <CODE>String</CODE> form.
  120. * @return The <CODE>String</CODE> representation of the value.
  121. */
  122. public String toString() {
  123. return new String(value) ;
  124. }
  125. /**
  126. * Converts the string value to its <CODE>SnmpOid</CODE> form.
  127. * @return The OID representation of the value.
  128. */
  129. public SnmpOid toOid() {
  130. long[] ids = new long[value.length] ;
  131. for (int i = 0 ; i < value.length ; i++) {
  132. ids[i] = (long)(value[i] & 0xFF) ;
  133. }
  134. return new SnmpOid(ids) ;
  135. }
  136. /**
  137. * Extracts the string from an index OID and returns its
  138. * value converted as an <CODE>SnmpOid</CODE>.
  139. * @param index The index array.
  140. * @param start The position in the index array.
  141. * @return The OID representing the string value.
  142. * @exception SnmpStatusException There is no string value
  143. * available at the start position.
  144. */
  145. public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
  146. try {
  147. if (index[start] > Integer.MAX_VALUE) {
  148. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  149. }
  150. int strLen = (int)index[start++] ;
  151. long[] ids = new long[strLen] ;
  152. for (int i = 0 ; i < strLen ; i++) {
  153. ids[i] = index[start + i] ;
  154. }
  155. return new SnmpOid(ids) ;
  156. }
  157. catch(IndexOutOfBoundsException e) {
  158. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  159. }
  160. }
  161. /**
  162. * Scans an index OID, skips the string value and returns the position
  163. * of the next value.
  164. * @param index The index array.
  165. * @param start The position in the index array.
  166. * @return The position of the next value.
  167. * @exception SnmpStatusException There is no string value
  168. * available at the start position.
  169. */
  170. public static int nextOid(long[] index, int start) throws SnmpStatusException {
  171. try {
  172. if (index[start] > Integer.MAX_VALUE) {
  173. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  174. }
  175. int strLen = (int)index[start++] ;
  176. start += strLen ;
  177. if (start <= index.length) {
  178. return start ;
  179. }
  180. else {
  181. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  182. }
  183. }
  184. catch(IndexOutOfBoundsException e) {
  185. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  186. }
  187. }
  188. /**
  189. * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpString</CODE> to another OID.
  190. * @param source An OID representing an <CODE>SnmpString</CODE> value.
  191. * @param dest Where source should be appended.
  192. */
  193. public static void appendToOid(SnmpOid source, SnmpOid dest) {
  194. dest.append(source.getLength()) ;
  195. dest.append(source) ;
  196. }
  197. /**
  198. * Performs a clone action. This provides a workaround for the
  199. * <CODE>SnmpValue</CODE> interface.
  200. * @return The SnmpValue clone.
  201. */
  202. final synchronized public SnmpValue duplicate() {
  203. return (SnmpValue) clone() ;
  204. }
  205. /**
  206. * Clones the <CODE>SnmpString</CODE> object, making a copy of its data.
  207. * @return The object clone.
  208. */
  209. synchronized public Object clone() {
  210. SnmpString newclone = null ;
  211. try {
  212. newclone = (SnmpString) super.clone() ;
  213. newclone.value = new byte[value.length] ;
  214. System.arraycopy(value, 0, newclone.value, 0, value.length) ;
  215. } catch (CloneNotSupportedException e) {
  216. throw new InternalError() ; // vm bug.
  217. }
  218. return newclone ;
  219. }
  220. /**
  221. * Returns a textual description of the type object.
  222. * @return ASN.1 textual description.
  223. */
  224. public String getTypeName() {
  225. return name ;
  226. }
  227. // VARIABLES
  228. //----------
  229. /**
  230. * Name of the type.
  231. */
  232. final static String name = "String" ;
  233. /**
  234. * This is the bytes array of the string value.
  235. * @serial
  236. */
  237. protected byte[] value = null ;
  238. }