Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpIpAddress.java
38924 views
/*1* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package com.sun.jmx.snmp;2728293031/**32* Represents an SNMP IpAddress.33*34* <p><b>This API is a Sun Microsystems internal API and is subject35* to change without notice.</b></p>36*/3738public class SnmpIpAddress extends SnmpOid {39private static final long serialVersionUID = 7204629998270874474L;4041// CONSTRUCTORS42//-------------43/**44* Constructs a new <CODE>SnmpIpAddress</CODE> from the specified bytes array.45* @param bytes The four bytes composing the address.46* @exception IllegalArgumentException The length of the array is not equal to four.47*/48public SnmpIpAddress(byte[] bytes) throws IllegalArgumentException {49buildFromByteArray(bytes);50}5152/**53* Constructs a new <CODE>SnmpIpAddress</CODE> from the specified long value.54* @param addr The initialization value.55*/56public SnmpIpAddress(long addr) {57int address = (int)addr ;58byte[] ipaddr = new byte[4];5960ipaddr[0] = (byte) ((address >>> 24) & 0xFF);61ipaddr[1] = (byte) ((address >>> 16) & 0xFF);62ipaddr[2] = (byte) ((address >>> 8) & 0xFF);63ipaddr[3] = (byte) (address & 0xFF);6465buildFromByteArray(ipaddr);66}6768/**69* Constructs a new <CODE>SnmpIpAddress</CODE> from a dot-formatted <CODE>String</CODE>.70* The dot-formatted <CODE>String</CODE> is formulated x.x.x.x .71* @param dotAddress The initialization value.72* @exception IllegalArgumentException The string does not correspond to an ip address.73*/74public SnmpIpAddress(String dotAddress) throws IllegalArgumentException {75super(dotAddress) ;76if ((componentCount > 4) ||77(components[0] > 255) ||78(components[1] > 255) ||79(components[2] > 255) ||80(components[3] > 255)) {81throw new IllegalArgumentException(dotAddress) ;82}83}8485/**86* Constructs a new <CODE>SnmpIpAddress</CODE> from four long values.87* @param b1 Byte 1.88* @param b2 Byte 2.89* @param b3 Byte 3.90* @param b4 Byte 4.91* @exception IllegalArgumentException A value is outside of [0-255].92*/93public SnmpIpAddress(long b1, long b2, long b3, long b4) {94super(b1, b2, b3, b4) ;95if ((components[0] > 255) ||96(components[1] > 255) ||97(components[2] > 255) ||98(components[3] > 255)) {99throw new IllegalArgumentException() ;100}101}102103// PUBLIC METHODS104//---------------105/**106* Converts the address value to its byte array form.107* @return The byte array representation of the value.108*/109public byte[] byteValue() {110byte[] result = new byte[4] ;111result[0] = (byte)components[0] ;112result[1] = (byte)components[1] ;113result[2] = (byte)components[2] ;114result[3] = (byte)components[3] ;115116return result ;117}118119/**120* Converts the address to its <CODE>String</CODE> form.121* Same as <CODE>toString()</CODE>. Exists only to follow a naming scheme.122* @return The <CODE>String</CODE> representation of the value.123*/124public String stringValue() {125return toString() ;126}127128/**129* Extracts the ip address from an index OID and returns its130* value converted as an <CODE>SnmpOid</CODE>.131* @param index The index array.132* @param start The position in the index array.133* @return The OID representing the ip address value.134* @exception SnmpStatusException There is no ip address value135* available at the start position.136*/137public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {138if (start + 4 <= index.length) {139try {140return new SnmpOid(141index[start],142index[start+1],143index[start+2],144index[start+3]) ;145}146catch(IllegalArgumentException e) {147throw new SnmpStatusException(SnmpStatusException.noSuchName) ;148}149}150else {151throw new SnmpStatusException(SnmpStatusException.noSuchName) ;152}153}154155/**156* Scans an index OID, skips the address value and returns the position157* of the next value.158* @param index The index array.159* @param start The position in the index array.160* @return The position of the next value.161* @exception SnmpStatusException There is no address value162* available at the start position.163*/164public static int nextOid(long[] index, int start) throws SnmpStatusException {165if (start + 4 <= index.length) {166return start + 4 ;167}168else {169throw new SnmpStatusException(SnmpStatusException.noSuchName) ;170}171}172173/**174* Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpIpAddress</CODE> to another OID.175* @param source An OID representing an <CODE>SnmpIpAddress</CODE> value.176* @param dest Where source should be appended.177*/178public static void appendToOid(SnmpOid source, SnmpOid dest) {179if (source.getLength() != 4) {180throw new IllegalArgumentException() ;181}182dest.append(source) ;183}184185/**186* Returns a textual description of the type object.187* @return ASN.1 textual description.188*/189final public String getTypeName() {190return name ;191}192193// PRIVATE METHODS194//----------------195/**196* Build Ip address from byte array.197*/198private void buildFromByteArray(byte[] bytes) {199if (bytes.length != 4) {200throw new IllegalArgumentException() ;201}202components = new long[4] ;203componentCount= 4;204components[0] = (bytes[0] >= 0) ? bytes[0] : bytes[0] + 256 ;205components[1] = (bytes[1] >= 0) ? bytes[1] : bytes[1] + 256 ;206components[2] = (bytes[2] >= 0) ? bytes[2] : bytes[2] + 256 ;207components[3] = (bytes[3] >= 0) ? bytes[3] : bytes[3] + 256 ;208}209210// VARIABLES211//----------212/**213* Name of the type.214*/215final static String name = "IpAddress" ;216}217218219