KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file SnmpString.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.15
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 import java.net.InetAddress JavaDoc;
16 import java.net.UnknownHostException JavaDoc;
17
18 /**
19  * Represents an SNMP string.
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.15 12/19/03
24  * @author Sun Microsystems, Inc
25  */

26
27 public class SnmpString extends SnmpValue {
28
29     // CONSTRUCTORS
30
//-------------
31
/**
32      * Constructs a new empty <CODE>SnmpString</CODE>.
33      */

34     public SnmpString() {
35         value = new byte[0] ;
36     }
37
38     /**
39      * Constructs a new <CODE>SnmpString</CODE> from the specified bytes array.
40      * @param v The bytes composing the string value.
41      */

42     public SnmpString(byte[] v) {
43         value = (byte[])v.clone() ;
44     }
45
46     /**
47      * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>Bytes</CODE> array.
48      * @param v The <CODE>Bytes</CODE> composing the string value.
49      */

50     public SnmpString(Byte JavaDoc[] 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     /**
58      * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE>String</CODE> value.
59      * @param v The initialization value.
60      */

61     public SnmpString(String JavaDoc v) {
62         value = v.getBytes() ;
63     }
64
65     /**
66      * Constructs a new <CODE>SnmpString</CODE> from the specified <CODE> InetAddress </Code>.
67      * @param address The <CODE>InetAddress </CODE>.
68      *
69      * @since 1.5
70      */

71     public SnmpString(InetAddress JavaDoc address) {
72     value = address.getAddress();
73     }
74
75     // PUBLIC METHODS
76
//---------------
77

78     /**
79      * Converts the string value to its <CODE> InetAddress </CODE> form.
80      * @return an {@link InetAddress} defined by the string value.
81      * @exception UnknownHostException If string value is not a legal address format.
82      *
83      * @since 1.5
84      */

85     public InetAddress JavaDoc inetAddressValue() throws UnknownHostException JavaDoc {
86     return InetAddress.getByAddress(value);
87     }
88     
89     /**
90      * Converts the specified binary string into a character string.
91      * @param bin The binary string value to convert.
92      * @return The character string representation.
93      */

94     public static String JavaDoc BinToChar(String JavaDoc 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 JavaDoc(value);
100     }
101
102     /**
103      * Converts the specified hexadecimal string into a character string.
104      * @param hex The hexadecimal string value to convert.
105      * @return The character string representation.
106      */

107     public static String JavaDoc HexToChar(String JavaDoc 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 JavaDoc(value);
113     }
114     
115     /**
116      * Returns the bytes array of this <CODE>SnmpString</CODE>.
117      * @return The value.
118      */

119     public byte[] byteValue() {
120         return value ;
121     }
122   
123     /**
124      * Converts the string value to its array of <CODE>Bytes</CODE> form.
125      * @return The array of <CODE>Bytes</CODE> representation of the value.
126      */

127     public Byte JavaDoc[] toByte() {
128         Byte JavaDoc[] result = new Byte JavaDoc[value.length] ;
129         for (int i = 0 ; i < value.length ; i++) {
130             result[i] = new Byte JavaDoc(value[i]) ;
131         }
132         return result ;
133     }
134   
135     /**
136      * Converts the string value to its <CODE>String</CODE> form.
137      * @return The <CODE>String</CODE> representation of the value.
138      */

139     public String JavaDoc toString() {
140         return new String JavaDoc(value) ;
141     }
142
143     /**
144      * Converts the string value to its <CODE>SnmpOid</CODE> form.
145      * @return The OID representation of the value.
146      */

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     /**
156      * Extracts the string from an index OID and returns its
157      * value converted as an <CODE>SnmpOid</CODE>.
158      * @param index The index array.
159      * @param start The position in the index array.
160      * @return The OID representing the string value.
161      * @exception SnmpStatusException There is no string value
162      * available at the start position.
163      */

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 JavaDoc e) {
177             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
178         }
179     }
180
181     /**
182      * Scans an index OID, skips the string value and returns the position
183      * of the next value.
184      * @param index The index array.
185      * @param start The position in the index array.
186      * @return The position of the next value.
187      * @exception SnmpStatusException There is no string value
188      * available at the start position.
189      */

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 JavaDoc e) {
205             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
206         }
207     }
208   
209     /**
210      * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpString</CODE> to another OID.
211      * @param source An OID representing an <CODE>SnmpString</CODE> value.
212      * @param dest Where source should be appended.
213      */

214     public static void appendToOid(SnmpOid source, SnmpOid dest) {
215         dest.append(source.getLength()) ;
216         dest.append(source) ;
217     }
218
219     /**
220      * Performs a clone action. This provides a workaround for the
221      * <CODE>SnmpValue</CODE> interface.
222      * @return The SnmpValue clone.
223      */

224     final synchronized public SnmpValue duplicate() {
225         return (SnmpValue) clone() ;
226     }
227
228     /**
229      * Clones the <CODE>SnmpString</CODE> object, making a copy of its data.
230      * @return The object clone.
231      */

232     synchronized public Object JavaDoc 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 JavaDoc e) {
240             throw new InternalError JavaDoc() ; // vm bug.
241
}
242         return newclone ;
243     }
244
245     /**
246      * Returns a textual description of the type object.
247      * @return ASN.1 textual description.
248      */

249     public String JavaDoc getTypeName() {
250         return name ;
251     }
252
253     // VARIABLES
254
//----------
255
/**
256      * Name of the type.
257      */

258     final static String JavaDoc name = "String" ;
259
260     /**
261      * This is the bytes array of the string value.
262      * @serial
263      */

264     protected byte[] value = null ;
265 }
266
Popular Tags