KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file SnmpIpAddress.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
17
18 /**
19  * Represents an SNMP IpAddress.
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.10 12/19/03
24  * @author Sun Microsystems, Inc
25  */

26
27 public class SnmpIpAddress extends SnmpOid {
28
29     // CONSTRUCTORS
30
//-------------
31
/**
32      * Constructs a new <CODE>SnmpIpAddress</CODE> from the specified bytes array.
33      * @param bytes The four bytes composing the address.
34      * @exception IllegalArgumentException The length of the array is not equal to four.
35      */

36     public SnmpIpAddress(byte[] bytes) throws IllegalArgumentException JavaDoc {
37     buildFromByteArray(bytes);
38     }
39
40     /**
41      * Constructs a new <CODE>SnmpIpAddress</CODE> from the specified long value.
42      * @param addr The initialization value.
43      */

44     public SnmpIpAddress(long addr) {
45     int address = (int)addr ;
46     byte[] ipaddr = new byte[4];
47
48     ipaddr[0] = (byte) ((address >>> 24) & 0xFF);
49     ipaddr[1] = (byte) ((address >>> 16) & 0xFF);
50     ipaddr[2] = (byte) ((address >>> 8) & 0xFF);
51     ipaddr[3] = (byte) (address & 0xFF);
52     
53     buildFromByteArray(ipaddr);
54     }
55
56     /**
57      * Constructs a new <CODE>SnmpIpAddress</CODE> from a dot-formatted <CODE>String</CODE>.
58      * The dot-formatted <CODE>String</CODE> is formulated x.x.x.x .
59      * @param dotAddress The initialization value.
60      * @exception IllegalArgumentException The string does not correspond to an ip address.
61      */

62     public SnmpIpAddress(String JavaDoc dotAddress) throws IllegalArgumentException JavaDoc {
63     super(dotAddress) ;
64     if ((componentCount > 4) ||
65         (components[0] > 255) ||
66         (components[1] > 255) ||
67         (components[2] > 255) ||
68         (components[3] > 255)) {
69         throw new IllegalArgumentException JavaDoc(dotAddress) ;
70     }
71     }
72
73     /**
74      * Constructs a new <CODE>SnmpIpAddress</CODE> from four long values.
75      * @param b1 Byte 1.
76      * @param b2 Byte 2.
77      * @param b3 Byte 3.
78      * @param b4 Byte 4.
79      * @exception IllegalArgumentException A value is outside of [0-255].
80      */

81     public SnmpIpAddress(long b1, long b2, long b3, long b4) {
82     super(b1, b2, b3, b4) ;
83     if ((components[0] > 255) ||
84         (components[1] > 255) ||
85         (components[2] > 255) ||
86         (components[3] > 255)) {
87         throw new IllegalArgumentException JavaDoc() ;
88     }
89     }
90   
91     // PUBLIC METHODS
92
//---------------
93
/**
94      * Converts the address value to its byte array form.
95      * @return The byte array representation of the value.
96      */

97     public byte[] byteValue() {
98     byte[] result = new byte[4] ;
99     result[0] = (byte)components[0] ;
100     result[1] = (byte)components[1] ;
101     result[2] = (byte)components[2] ;
102     result[3] = (byte)components[3] ;
103     
104     return result ;
105     }
106   
107     /**
108      * Converts the address to its <CODE>String</CODE> form.
109      * Same as <CODE>toString()</CODE>. Exists only to follow a naming scheme.
110      * @return The <CODE>String</CODE> representation of the value.
111      */

112     public String JavaDoc stringValue() {
113     return toString() ;
114     }
115
116     /**
117      * Extracts the ip address from an index OID and returns its
118      * value converted as an <CODE>SnmpOid</CODE>.
119      * @param index The index array.
120      * @param start The position in the index array.
121      * @return The OID representing the ip address value.
122      * @exception SnmpStatusException There is no ip address value
123      * available at the start position.
124      */

125     public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
126     if (start + 4 <= index.length) {
127         try {
128         return new SnmpOid(
129                    index[start],
130                    index[start+1],
131                    index[start+2],
132                    index[start+3]) ;
133         }
134         catch(IllegalArgumentException JavaDoc e) {
135         throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
136         }
137     }
138     else {
139         throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
140     }
141     }
142
143     /**
144      * Scans an index OID, skips the address value and returns the position
145      * of the next value.
146      * @param index The index array.
147      * @param start The position in the index array.
148      * @return The position of the next value.
149      * @exception SnmpStatusException There is no address value
150      * available at the start position.
151      */

152     public static int nextOid(long[] index, int start) throws SnmpStatusException {
153     if (start + 4 <= index.length) {
154         return start + 4 ;
155     }
156     else {
157         throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
158     }
159     }
160   
161     /**
162      * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpIpAddress</CODE> to another OID.
163      * @param source An OID representing an <CODE>SnmpIpAddress</CODE> value.
164      * @param dest Where source should be appended.
165      */

166     public static void appendToOid(SnmpOid source, SnmpOid dest) {
167     if (source.getLength() != 4) {
168         throw new IllegalArgumentException JavaDoc() ;
169     }
170     dest.append(source) ;
171     }
172
173     /**
174      * Returns a textual description of the type object.
175      * @return ASN.1 textual description.
176      */

177     final public String JavaDoc getTypeName() {
178     return name ;
179     }
180
181     // PRIVATE METHODS
182
//----------------
183
/**
184      * Build Ip address from byte array.
185      */

186     private void buildFromByteArray(byte[] bytes) {
187     if (bytes.length != 4) {
188         throw new IllegalArgumentException JavaDoc() ;
189     }
190     components = new long[4] ;
191     componentCount= 4;
192     components[0] = (bytes[0] >= 0) ? bytes[0] : bytes[0] + 256 ;
193     components[1] = (bytes[1] >= 0) ? bytes[1] : bytes[1] + 256 ;
194     components[2] = (bytes[2] >= 0) ? bytes[2] : bytes[2] + 256 ;
195     components[3] = (bytes[3] >= 0) ? bytes[3] : bytes[3] + 256 ;
196     }
197   
198     // VARIABLES
199
//----------
200
/**
201      * Name of the type.
202      */

203     final static String JavaDoc name = "IpAddress" ;
204 }
205
Popular Tags