KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPNSAPAddress


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
34 import java.util.*;
35
36
37 /**
38 * Defines class for holding physical 6-byte addresses.
39 */

40
41 public class SNMPNSAPAddress extends SNMPOctetString
42 {
43     // length limited to 6 octets
44

45     
46     /**
47     * Initialize address to 0.0.0.0.0.0.
48     */

49     
50     public SNMPNSAPAddress()
51     {
52         tag = SNMPBERCodec.SNMPNSAPADDRESS;
53         
54         // initialize to 0.0.0.0.0.0
55
data = new byte[6];
56         for (int i = 0; i < 6; i++)
57             data[i] = 0;
58     }
59     
60     
61     
62     public SNMPNSAPAddress(String JavaDoc string)
63         throws SNMPBadValueException
64     {
65         tag = SNMPBERCodec.SNMPNSAPADDRESS;
66         
67         data = parseNSAPAddress(string);
68     }
69     
70     
71     
72     
73     /**
74     * Used to initialize from the BER encoding, as received in a response from
75     * an SNMP device responding to an SNMPGetRequest, or from a supplied byte array
76     * containing the address components.
77     * @throws SNMPBadValueException Indicates an invalid array supplied: must have length 6.
78     */

79     
80     public SNMPNSAPAddress(byte[] enc)
81         throws SNMPBadValueException
82     {
83         tag = SNMPBERCodec.SNMPNSAPADDRESS;
84         
85         if (enc.length == 6)
86         {
87             data = enc;
88         }
89         else // wrong size
90
{
91             throw new SNMPBadValueException(" NSAPAddress: bad BER encoding supplied to set value ");
92         }
93     }
94     
95     
96     
97     /**
98     * Used to set the value from a byte array containing the address.
99     * @throws SNMPBadValueException Indicates an incorrect object type supplied, or array of
100     * incorrect size.
101     */

102     
103     public void setValue(Object JavaDoc newAddress)
104         throws SNMPBadValueException
105     {
106         if ((newAddress instanceof byte[]) && (((byte[])newAddress).length == 6))
107             data = (byte[])newAddress;
108         else if (newAddress instanceof String JavaDoc)
109         {
110             data = parseNSAPAddress((String JavaDoc)newAddress);
111         }
112         else
113             throw new SNMPBadValueException(" NSAPAddress: bad length byte string supplied to set value ");
114     }
115     
116     
117     
118     
119     /**
120     * Return pretty-printed (dash-separated) address.
121     */

122     
123     public String JavaDoc toString()
124     {
125         StringBuffer JavaDoc returnStringBuffer = new StringBuffer JavaDoc();
126         
127         if (data.length > 0)
128         {
129             int convert = data[0];
130             if (convert < 0)
131                     convert += 256;
132                 returnStringBuffer.append(Integer.toHexString(convert));
133                     
134             for (int i = 1; i < data.length; i++)
135             {
136                 convert = data[i];
137                 if (convert < 0)
138                     convert += 256;
139                 returnStringBuffer.append("-");
140                 returnStringBuffer.append(Integer.toHexString(convert));
141             }
142         }
143         
144         return returnStringBuffer.toString();
145     }
146     
147     
148     
149     private byte[] parseNSAPAddress(String JavaDoc addressString)
150         throws SNMPBadValueException
151     {
152         try
153         {
154             StringTokenizer st = new StringTokenizer(addressString, " .-"); // break on spaces, dots or dashes
155
int size = 0;
156             
157             while (st.hasMoreTokens())
158             {
159                 // figure out how many values are in string
160
size++;
161                 st.nextToken();
162             }
163             
164             if (size != 6)
165             {
166                 throw new SNMPBadValueException(" NSAPAddress: wrong number of components supplied to set value ");
167             }
168             
169             byte[] returnBytes = new byte[size];
170             
171             st = new StringTokenizer(addressString, " .-");
172             
173             for (int i = 0; i < size; i++)
174             {
175                 int addressComponent = (Integer.parseInt(st.nextToken(), 16));
176                 if ((addressComponent < 0) || (addressComponent > 255))
177                     throw new SNMPBadValueException(" NSAPAddress: invalid component supplied to set value ");
178                 returnBytes[i] = (byte)addressComponent;
179             }
180             
181             return returnBytes;
182             
183         }
184         catch (NumberFormatException JavaDoc e)
185         {
186             throw new SNMPBadValueException(" NSAPAddress: invalid component supplied to set value ");
187         }
188         
189     }
190     
191     
192     
193 }
Popular Tags