KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > SnmpStringFixed


1 /*
2  * @(#)file SnmpStringFixed.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.10
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp;
14
15
16 // @(#)SnmpStringFixed.java 4.10 03/12/19 SMI
17

18 // java imports
19
//
20
import java.lang.Math JavaDoc;
21
22 /**
23  * Represents an SNMP String defined with a fixed length.
24  * The class is mainly used when dealing with table indexes for which one of the keys
25  * is defined as a <CODE>String</CODE>.
26  *
27  * <p><b>This API is a Sun Microsystems internal API and is subject
28  * to change without notice.</b></p>
29  * @version 4.10 12/19/03
30  * @author Sun Microsystems, Inc
31  */

32
33 public class SnmpStringFixed extends SnmpString {
34
35     // CONSTRUCTORS
36
//-------------
37
/**
38      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified bytes array.
39      * @param v The bytes composing the fixed-string value.
40      */

41     public SnmpStringFixed(byte[] v) {
42         super(v) ;
43     }
44
45     /**
46      * Constructs a new <CODE>SnmpStringFixed</CODE> with the specified <CODE>Bytes</CODE> array.
47      * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
48      */

49     public SnmpStringFixed(Byte JavaDoc[] v) {
50         super(v) ;
51     }
52
53     /**
54      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE> value.
55      * @param v The initialization value.
56      */

57     public SnmpStringFixed(String JavaDoc v) {
58         super(v) ;
59     }
60
61     /**
62      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>bytes</CODE> array
63      * with the specified length.
64      * @param l The length of the fixed-string.
65      * @param v The <CODE>bytes</CODE> composing the fixed-string value.
66      * @exception IllegalArgumentException Either the length or the <CODE>byte</CODE> array is not valid.
67      */

68     public SnmpStringFixed(int l, byte[] v) throws IllegalArgumentException JavaDoc {
69         if ((l <= 0) || (v == null)) {
70             throw new IllegalArgumentException JavaDoc() ;
71         }
72         int length = Math.min(l, v.length);
73         value = new byte[l] ;
74         for (int i = 0 ; i < length ; i++) {
75             value[i] = v[i] ;
76         }
77         for (int i = length ; i < l ; i++) {
78             value[i] = 0 ;
79         }
80     }
81       
82     /**
83      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
84      * with the specified length.
85      * @param l The length of the fixed-string.
86      * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
87      * @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
88      */

89     public SnmpStringFixed(int l, Byte JavaDoc[] v) throws IllegalArgumentException JavaDoc {
90         if ((l <= 0) || (v == null)) {
91             throw new IllegalArgumentException JavaDoc() ;
92         }
93         int length = Math.min(l, v.length);
94         value = new byte[l] ;
95         for (int i = 0 ; i < length ; i++) {
96             value[i] = v[i].byteValue() ;
97         }
98         for (int i = length ; i < l ; i++) {
99             value[i] = 0 ;
100         }
101     }
102       
103     /**
104      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
105      * with the specified length.
106      * @param l The length of the fixed-string.
107      * @param s The <CODE>String</CODE> composing the fixed-string value.
108      * @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
109      */

110     public SnmpStringFixed(int l, String JavaDoc s) throws IllegalArgumentException JavaDoc {
111         if ((l <= 0) || (s == null)) {
112             throw new IllegalArgumentException JavaDoc() ;
113         }
114         byte[] v = s.getBytes();
115         int length = Math.min(l, v.length);
116         value = new byte[l] ;
117         for (int i = 0 ; i < length ; i++) {
118             value[i] = v[i] ;
119         }
120         for (int i = length ; i < l ; i++) {
121             value[i] = 0 ;
122         }
123     }
124       
125     // PUBLIC METHODS
126
//---------------
127
/**
128      * Extracts the fixed-string from an index OID and returns its
129      * value converted as an <CODE>SnmpOid</CODE>.
130      * @param l The number of successive array elements to be retreived
131      * in order to construct the OID.
132      * These elements are retreived starting at the <CODE>start</CODE> position.
133      * @param index The index array.
134      * @param start The position in the index array.
135      * @return The OID representing the fixed-string value.
136      * @exception SnmpStatusException There is no string value
137      * available at the start position.
138      */

139     public static SnmpOid toOid(int l, long[] index, int start) throws SnmpStatusException {
140         try {
141             long[] ids = new long[l] ;
142             for (int i = 0 ; i < l ; i++) {
143                 ids[i] = index[start + i] ;
144             }
145             return new SnmpOid(ids) ;
146         }
147         catch(IndexOutOfBoundsException JavaDoc e) {
148             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
149         }
150     }
151
152     /**
153      * Scans an index OID, skip the string value and returns the position
154      * of the next value.
155      * @param l The number of successive array elements to be passed
156      * in order to get the position of the next value.
157      * These elements are passed starting at the <CODE>start</CODE> position.
158      * @param index The index array.
159      * @param start The position in the index array.
160      * @return The position of the next value.
161      * @exception SnmpStatusException There is no string value
162      * available at the start position.
163      */

164     public static int nextOid(int l, long[] index, int start) throws SnmpStatusException {
165         int result = start + l ;
166         if (result > index.length) {
167             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
168         }
169         return result ;
170     }
171
172     /**
173      * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpStringFixed</CODE> to another OID.
174      * @param l Unused.
175      * @param source An OID representing an <CODE>SnmpStringFixed</CODE> value.
176      * @param dest Where source should be appended.
177      */

178     public static void appendToOid(int l, SnmpOid source, SnmpOid dest) {
179         dest.append(source) ;
180     }
181 }
182
Popular Tags