KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPOctetString


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.io.*;
34
35
36 /**
37 * Class representing a general string of octets.
38 */

39
40 public class SNMPOctetString extends SNMPObject
41 {
42     protected byte[] data;
43     protected byte tag = SNMPBERCodec.SNMPOCTETSTRING;
44         
45     
46     /**
47     * Create a zero-length octet string.
48     */

49     
50     public SNMPOctetString()
51     {
52         data = new byte[0];
53     }
54     
55     
56     /**
57     * Create an octet string from the bytes of the supplied String.
58     */

59     
60     public SNMPOctetString(String JavaDoc stringData)
61     {
62         this.data = stringData.getBytes();
63     }
64     
65     
66     
67     
68     /**
69     * Create an octet string from the supplied byte array. The array may be either
70     * user-supplied, or part of a retrieved BER encoding. Note that the BER encoding
71     * of the data of an octet string is just the raw bytes.
72     */

73     
74     public SNMPOctetString(byte[] enc)
75     {
76         extractFromBEREncoding(enc);
77     }
78     
79     
80     
81     
82     /**
83     * Return the array of raw bytes.
84     */

85     
86     public Object JavaDoc getValue()
87     {
88         return data;
89     }
90     
91     
92     
93     
94     /**
95     * Used to set the value from a byte array.
96     * @throws SNMPBadValueException Indicates an incorrect object type supplied.
97     */

98     
99     public void setValue(Object JavaDoc data)
100         throws SNMPBadValueException
101     {
102         if (data instanceof byte[])
103             this.data = (byte[])data;
104         else if (data instanceof String JavaDoc)
105             this.data = ((String JavaDoc)data).getBytes();
106         else
107             throw new SNMPBadValueException(" Octet String: bad object supplied to set value ");
108     }
109     
110     
111     
112     
113     
114     /**
115     * Returns the BER encoding for the octet string. Note the the "value" part of the
116     * BER type,length,value triple is just the sequence of raw bytes.
117     */

118     
119     protected byte[] getBEREncoding()
120     {
121         
122         ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
123         
124         // calculate encoding for length of data
125
byte[] len = SNMPBERCodec.encodeLength(data.length);
126         
127         // encode T,L,V info
128
outBytes.write(tag);
129         outBytes.write(len, 0, len.length);
130         outBytes.write(data, 0, data.length);
131     
132         return outBytes.toByteArray();
133     }
134     
135     
136     
137     
138     protected void extractFromBEREncoding(byte[] enc)
139     {
140         data = new byte[enc.length];
141         
142         // copy data
143
for (int i = 0; i < enc.length; i++)
144         {
145             data[i] = enc[i];
146         }
147     }
148     
149     
150     
151     /**
152     * Checks the embedded arrays for equality.
153     */

154     
155     public boolean equals(Object JavaDoc other)
156     {
157         // false if other is null
158
if (other == null)
159         {
160             return false;
161         }
162         
163         // check first to see that they're both of the same class
164
if (!this.getClass().equals(other.getClass()))
165         {
166             return false;
167         }
168         
169         SNMPOctetString otherSNMPObject = (SNMPOctetString)other;
170          
171         // see if their embedded arrays are equal
172
if (java.util.Arrays.equals((byte[])this.getValue(),(byte[])otherSNMPObject.getValue()))
173         {
174             return true;
175         }
176         else
177         {
178             return false;
179         }
180     }
181     
182     
183     
184     /**
185     * Generates a hash value so SNMP Octet String subclasses can be used in Hashtables.
186     */

187     
188     public int hashCode()
189     {
190         int hash = 0;
191         
192         // generate a hashcode from the embedded array
193
for (int i = 0; i < data.length; i++)
194         {
195             hash += data[i];
196             hash += (hash << 10);
197             hash ^= (hash >> 6);
198         }
199         
200         hash += (hash << 3);
201         hash ^= (hash >> 11);
202         hash += (hash << 15);
203          
204         return hash;
205     }
206     
207     
208     
209     
210     
211     /**
212     * Returns a String constructed from the raw bytes. If the bytes contain non-printable
213     * ASCII characters, tant pis! (Though it's fun when the bell rings!)
214     */

215     
216     public String JavaDoc toString()
217     {
218         String JavaDoc returnString;
219         
220         /*
221         if ((data.length == 4) || (data.length == 6))
222         {
223             returnString = new String();
224         
225             int convert = data[0];
226             if (convert < 0)
227                     convert += 256;
228                 returnString += convert;
229                     
230             for (int i = 1; i < data.length; i++)
231             {
232                 convert = data[i];
233                 if (convert < 0)
234                     convert += 256;
235                 returnString += "." + convert;
236             }
237         }
238         else
239             returnString = new String(data);
240         */

241         
242         /*
243         byte[] converted = new byte[data.length];
244         
245         for (int i = 0; i < data.length; i++)
246         {
247             if (data[i] == 0)
248                 converted[i] = 0x20; // space character
249             else
250                 converted[i] = data[i];
251         }
252         
253         returnString = new String(converted);
254         */

255         
256         returnString = new String JavaDoc(data);
257         
258         return returnString;
259         
260     }
261     
262     
263     
264     private String JavaDoc hexByte(byte b)
265     {
266         int pos = b;
267         if (pos < 0)
268             pos += 256;
269         String JavaDoc returnString = new String JavaDoc();
270         returnString += Integer.toHexString(pos/16);
271         returnString += Integer.toHexString(pos%16);
272         return returnString;
273     }
274     
275     
276     
277     /**
278     * Returns a space-separated hex string corresponding to the raw bytes.
279     */

280     
281     public String JavaDoc toHexString()
282     {
283         StringBuffer JavaDoc returnStringBuffer = new StringBuffer JavaDoc();
284         
285         
286         for (int i = 0; i < data.length; i++)
287         {
288             returnStringBuffer.append(hexByte(data[i]) + " ");
289         }
290         
291         return returnStringBuffer.toString();
292         
293     }
294     
295     
296     
297 }
Popular Tags