KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPIPAddress


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 * Class to hold IP addresses; special case of SNMP Octet String.
39 */

40
41
42 public class SNMPIPAddress extends SNMPOctetString
43 {
44     // length limited to 4 octets
45

46     
47     /**
48     * Initialize to 0.0.0.0
49     */

50         
51     public SNMPIPAddress()
52     {
53         // initialize to 0.0.0.0
54
tag = SNMPBERCodec.SNMPIPADDRESS;
55         data = new byte[4];
56         for (int i = 0; i < 4; i++)
57             data[i] = 0;
58     }
59     
60     
61     /**
62     * Used to initialize from a string containing a standard "dotted" IP address.
63     * @throws SNMPBadValueException Indicates an invalid string supplied: more than 4 components,
64     * component values not between 0 and 255, etc.
65     */

66     
67     public SNMPIPAddress(String JavaDoc string)
68         throws SNMPBadValueException
69     {
70         tag = SNMPBERCodec.SNMPIPADDRESS;
71         this.data = parseIPAddress(string);
72     }
73     
74     
75     
76     
77     
78     /**
79     * Used to initialize from the BER encoding, as received in a response from
80     * an SNMP device responding to an SNMPGetRequest, or from a supplied byte array
81     * containing the address components.
82     * @throws SNMPBadValueException Indicates an invalid array supplied: must have length 4.
83     */

84     
85     public SNMPIPAddress(byte[] enc)
86         throws SNMPBadValueException
87     {
88         
89         tag = SNMPBERCodec.SNMPIPADDRESS;
90         
91         if (enc.length == 4)
92         {
93             data = enc;
94         }
95         else // wrong size
96
{
97             throw new SNMPBadValueException(" IPAddress: bad BER encoding supplied to set value ");
98         }
99     }
100     
101     
102     
103     
104     
105     /**
106     * Used to set the value from a byte array containing the address.
107     * @throws SNMPBadValueException Indicates an incorrect object type supplied, or array of
108     * incorrect size.
109     */

110     
111     public void setValue(Object JavaDoc newAddress)
112         throws SNMPBadValueException
113     {
114         if ((newAddress instanceof byte[]) && (((byte[])newAddress).length == 4))
115             data = (byte[])newAddress;
116         else if (newAddress instanceof String JavaDoc)
117         {
118             data = parseIPAddress((String JavaDoc)newAddress);
119         }
120         else
121             throw new SNMPBadValueException(" IPAddress: bad data supplied to set value ");
122     }
123     
124     
125     
126     /**
127     * Return pretty-printed IP address.
128     */

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