1. /*
  2. * @(#)file SnmpStringFixed.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.10
  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. // @(#)SnmpStringFixed.java 4.10 03/12/19 SMI
  13. // java imports
  14. //
  15. import java.lang.Math;
  16. /**
  17. * Represents an SNMP String defined with a fixed length.
  18. * The class is mainly used when dealing with table indexes for which one of the keys
  19. * is defined as a <CODE>String</CODE>.
  20. *
  21. * <p><b>This API is a Sun Microsystems internal API and is subject
  22. * to change without notice.</b></p>
  23. * @version 4.10 12/19/03
  24. * @author Sun Microsystems, Inc
  25. */
  26. public class SnmpStringFixed extends SnmpString {
  27. // CONSTRUCTORS
  28. //-------------
  29. /**
  30. * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified bytes array.
  31. * @param v The bytes composing the fixed-string value.
  32. */
  33. public SnmpStringFixed(byte[] v) {
  34. super(v) ;
  35. }
  36. /**
  37. * Constructs a new <CODE>SnmpStringFixed</CODE> with the specified <CODE>Bytes</CODE> array.
  38. * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
  39. */
  40. public SnmpStringFixed(Byte[] v) {
  41. super(v) ;
  42. }
  43. /**
  44. * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE> value.
  45. * @param v The initialization value.
  46. */
  47. public SnmpStringFixed(String v) {
  48. super(v) ;
  49. }
  50. /**
  51. * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>bytes</CODE> array
  52. * with the specified length.
  53. * @param l The length of the fixed-string.
  54. * @param v The <CODE>bytes</CODE> composing the fixed-string value.
  55. * @exception IllegalArgumentException Either the length or the <CODE>byte</CODE> array is not valid.
  56. */
  57. public SnmpStringFixed(int l, byte[] v) throws IllegalArgumentException {
  58. if ((l <= 0) || (v == null)) {
  59. throw new IllegalArgumentException() ;
  60. }
  61. int length = Math.min(l, v.length);
  62. value = new byte[l] ;
  63. for (int i = 0 ; i < length ; i++) {
  64. value[i] = v[i] ;
  65. }
  66. for (int i = length ; i < l ; i++) {
  67. value[i] = 0 ;
  68. }
  69. }
  70. /**
  71. * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
  72. * with the specified length.
  73. * @param l The length of the fixed-string.
  74. * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
  75. * @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
  76. */
  77. public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
  78. if ((l <= 0) || (v == null)) {
  79. throw new IllegalArgumentException() ;
  80. }
  81. int length = Math.min(l, v.length);
  82. value = new byte[l] ;
  83. for (int i = 0 ; i < length ; i++) {
  84. value[i] = v[i].byteValue() ;
  85. }
  86. for (int i = length ; i < l ; i++) {
  87. value[i] = 0 ;
  88. }
  89. }
  90. /**
  91. * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
  92. * with the specified length.
  93. * @param l The length of the fixed-string.
  94. * @param s The <CODE>String</CODE> composing the fixed-string value.
  95. * @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
  96. */
  97. public SnmpStringFixed(int l, String s) throws IllegalArgumentException {
  98. if ((l <= 0) || (s == null)) {
  99. throw new IllegalArgumentException() ;
  100. }
  101. byte[] v = s.getBytes();
  102. int length = Math.min(l, v.length);
  103. value = new byte[l] ;
  104. for (int i = 0 ; i < length ; i++) {
  105. value[i] = v[i] ;
  106. }
  107. for (int i = length ; i < l ; i++) {
  108. value[i] = 0 ;
  109. }
  110. }
  111. // PUBLIC METHODS
  112. //---------------
  113. /**
  114. * Extracts the fixed-string from an index OID and returns its
  115. * value converted as an <CODE>SnmpOid</CODE>.
  116. * @param l The number of successive array elements to be retreived
  117. * in order to construct the OID.
  118. * These elements are retreived starting at the <CODE>start</CODE> position.
  119. * @param index The index array.
  120. * @param start The position in the index array.
  121. * @return The OID representing the fixed-string value.
  122. * @exception SnmpStatusException There is no string value
  123. * available at the start position.
  124. */
  125. public static SnmpOid toOid(int l, long[] index, int start) throws SnmpStatusException {
  126. try {
  127. long[] ids = new long[l] ;
  128. for (int i = 0 ; i < l ; i++) {
  129. ids[i] = index[start + i] ;
  130. }
  131. return new SnmpOid(ids) ;
  132. }
  133. catch(IndexOutOfBoundsException e) {
  134. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  135. }
  136. }
  137. /**
  138. * Scans an index OID, skip the string value and returns the position
  139. * of the next value.
  140. * @param l The number of successive array elements to be passed
  141. * in order to get the position of the next value.
  142. * These elements are passed starting at the <CODE>start</CODE> position.
  143. * @param index The index array.
  144. * @param start The position in the index array.
  145. * @return The position of the next value.
  146. * @exception SnmpStatusException There is no string value
  147. * available at the start position.
  148. */
  149. public static int nextOid(int l, long[] index, int start) throws SnmpStatusException {
  150. int result = start + l ;
  151. if (result > index.length) {
  152. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  153. }
  154. return result ;
  155. }
  156. /**
  157. * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpStringFixed</CODE> to another OID.
  158. * @param l Unused.
  159. * @param source An OID representing an <CODE>SnmpStringFixed</CODE> value.
  160. * @param dest Where source should be appended.
  161. */
  162. public static void appendToOid(int l, SnmpOid source, SnmpOid dest) {
  163. dest.append(source) ;
  164. }
  165. }