Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpIpAddress.java
38924 views
1
/*
2
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
27
package com.sun.jmx.snmp;
28
29
30
31
32
/**
33
* Represents an SNMP IpAddress.
34
*
35
* <p><b>This API is a Sun Microsystems internal API and is subject
36
* to change without notice.</b></p>
37
*/
38
39
public class SnmpIpAddress extends SnmpOid {
40
private static final long serialVersionUID = 7204629998270874474L;
41
42
// CONSTRUCTORS
43
//-------------
44
/**
45
* Constructs a new <CODE>SnmpIpAddress</CODE> from the specified bytes array.
46
* @param bytes The four bytes composing the address.
47
* @exception IllegalArgumentException The length of the array is not equal to four.
48
*/
49
public SnmpIpAddress(byte[] bytes) throws IllegalArgumentException {
50
buildFromByteArray(bytes);
51
}
52
53
/**
54
* Constructs a new <CODE>SnmpIpAddress</CODE> from the specified long value.
55
* @param addr The initialization value.
56
*/
57
public SnmpIpAddress(long addr) {
58
int address = (int)addr ;
59
byte[] ipaddr = new byte[4];
60
61
ipaddr[0] = (byte) ((address >>> 24) & 0xFF);
62
ipaddr[1] = (byte) ((address >>> 16) & 0xFF);
63
ipaddr[2] = (byte) ((address >>> 8) & 0xFF);
64
ipaddr[3] = (byte) (address & 0xFF);
65
66
buildFromByteArray(ipaddr);
67
}
68
69
/**
70
* Constructs a new <CODE>SnmpIpAddress</CODE> from a dot-formatted <CODE>String</CODE>.
71
* The dot-formatted <CODE>String</CODE> is formulated x.x.x.x .
72
* @param dotAddress The initialization value.
73
* @exception IllegalArgumentException The string does not correspond to an ip address.
74
*/
75
public SnmpIpAddress(String dotAddress) throws IllegalArgumentException {
76
super(dotAddress) ;
77
if ((componentCount > 4) ||
78
(components[0] > 255) ||
79
(components[1] > 255) ||
80
(components[2] > 255) ||
81
(components[3] > 255)) {
82
throw new IllegalArgumentException(dotAddress) ;
83
}
84
}
85
86
/**
87
* Constructs a new <CODE>SnmpIpAddress</CODE> from four long values.
88
* @param b1 Byte 1.
89
* @param b2 Byte 2.
90
* @param b3 Byte 3.
91
* @param b4 Byte 4.
92
* @exception IllegalArgumentException A value is outside of [0-255].
93
*/
94
public SnmpIpAddress(long b1, long b2, long b3, long b4) {
95
super(b1, b2, b3, b4) ;
96
if ((components[0] > 255) ||
97
(components[1] > 255) ||
98
(components[2] > 255) ||
99
(components[3] > 255)) {
100
throw new IllegalArgumentException() ;
101
}
102
}
103
104
// PUBLIC METHODS
105
//---------------
106
/**
107
* Converts the address value to its byte array form.
108
* @return The byte array representation of the value.
109
*/
110
public byte[] byteValue() {
111
byte[] result = new byte[4] ;
112
result[0] = (byte)components[0] ;
113
result[1] = (byte)components[1] ;
114
result[2] = (byte)components[2] ;
115
result[3] = (byte)components[3] ;
116
117
return result ;
118
}
119
120
/**
121
* Converts the address to its <CODE>String</CODE> form.
122
* Same as <CODE>toString()</CODE>. Exists only to follow a naming scheme.
123
* @return The <CODE>String</CODE> representation of the value.
124
*/
125
public String stringValue() {
126
return toString() ;
127
}
128
129
/**
130
* Extracts the ip address from an index OID and returns its
131
* value converted as an <CODE>SnmpOid</CODE>.
132
* @param index The index array.
133
* @param start The position in the index array.
134
* @return The OID representing the ip address value.
135
* @exception SnmpStatusException There is no ip address value
136
* available at the start position.
137
*/
138
public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
139
if (start + 4 <= index.length) {
140
try {
141
return new SnmpOid(
142
index[start],
143
index[start+1],
144
index[start+2],
145
index[start+3]) ;
146
}
147
catch(IllegalArgumentException e) {
148
throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
149
}
150
}
151
else {
152
throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
153
}
154
}
155
156
/**
157
* Scans an index OID, skips the address value and returns the position
158
* of the next value.
159
* @param index The index array.
160
* @param start The position in the index array.
161
* @return The position of the next value.
162
* @exception SnmpStatusException There is no address value
163
* available at the start position.
164
*/
165
public static int nextOid(long[] index, int start) throws SnmpStatusException {
166
if (start + 4 <= index.length) {
167
return start + 4 ;
168
}
169
else {
170
throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
171
}
172
}
173
174
/**
175
* Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpIpAddress</CODE> to another OID.
176
* @param source An OID representing an <CODE>SnmpIpAddress</CODE> value.
177
* @param dest Where source should be appended.
178
*/
179
public static void appendToOid(SnmpOid source, SnmpOid dest) {
180
if (source.getLength() != 4) {
181
throw new IllegalArgumentException() ;
182
}
183
dest.append(source) ;
184
}
185
186
/**
187
* Returns a textual description of the type object.
188
* @return ASN.1 textual description.
189
*/
190
final public String getTypeName() {
191
return name ;
192
}
193
194
// PRIVATE METHODS
195
//----------------
196
/**
197
* Build Ip address from byte array.
198
*/
199
private void buildFromByteArray(byte[] bytes) {
200
if (bytes.length != 4) {
201
throw new IllegalArgumentException() ;
202
}
203
components = new long[4] ;
204
componentCount= 4;
205
components[0] = (bytes[0] >= 0) ? bytes[0] : bytes[0] + 256 ;
206
components[1] = (bytes[1] >= 0) ? bytes[1] : bytes[1] + 256 ;
207
components[2] = (bytes[2] >= 0) ? bytes[2] : bytes[2] + 256 ;
208
components[3] = (bytes[3] >= 0) ? bytes[3] : bytes[3] + 256 ;
209
}
210
211
// VARIABLES
212
//----------
213
/**
214
* Name of the type.
215
*/
216
final static String name = "IpAddress" ;
217
}
218
219